Introduction: AVR Microcontroller. Ultrasonic Distance Sensor. HC-SR04 on LCD NOKIA 5110

About: Welcome to FOG channel! "Hobby" "DIY" "Electronics" "Programming" "Video Games" "Media Design" "Digital Art" and more...…

Hello everyone!

In this section I make simple electronic device to sensor the distance and these parameters are displayed on LCD NOKIA 5110. The parameters are displayed as a diagram and numbers. The device is based on the microcontroller AVR ATMEGA328P. Device is equipped ultrasonic sensor distance HC-SR04.

Step 1: Description Components

Basic components of the device:

  • Microcontroller AVR «ATMEGA328P»
  • Monochrome Graphic LCD «NOKIA 5110»
  • Ultrasonic distance sensor «HC-SR04»

Microcontroller AVR «ATMEGA328P»

Involved features:

  1. 16-bit Timer/Counter interrupt
  2. External Interrupts
  3. Master/slave SPI serial interface

Monochrome Graphic LCD «NOKIA 5110»


  1. 48 x 84 Dot LCD Display
  2. Serial Bus Interface with maximum high speed 4 Mbits/s
  3. Internal Controller/Driver «PCD8544»
  4. LED Back-Light
  5. Run at Voltage 2.7V-5V, low power consumption, it is suitable for battery applications
  6. Temperature range from -25˚C to +70˚C
  7. Support Signal CMOS Input

Ultrasonic distance sensor «HC-SR04»

Features and specs:

  1. Power Supply: +5V DC
  2. Quiescent current: < 2mA, working current: 15mA
  3. Ranging distance: 2cm - 400cm / 1" - 13 ft, resolution: 0.3cm
  4. Measuring angle: 30 degree
  5. Trigger Input Pulse width: 10uS
  6. Dimension: 45mm x 20mm x 15mm

Step 2: How Does It Work?

The ultrasonic sensor works on the principle of SONAR and RADAR system which is used to determine the distance to an object.

An ultrasonic sensor generates the high-frequency sound (ultrasound) waves. When this ultrasound hits the object, it reflects as echo which is sensed by the receiver as shown in below figure.

By measuring the time required for the echo to reach to the receiver, we can calculate the distance.

This is the basic working principle of Ultrasonic module to measure distance.

In ultrasonic module HCSR04, we have to give trigger pulse, so that it will generate ultrasound of frequency 40 kHz.

After generating ultrasound i.e. 8 pulses of 40 kHz, it makes echo pin high. Echo pin remains high until it does not get the echo sound back. So the width of echo pin will be the time for sound to travel to the object and return back. Once we get the time we can calculate distance, as we know the speed of sound.

HC-SR04 can measure up to range from 2 cm - 400 cm.

Now how to calculate distance: Distance = Speed x Time

The speed of sound waves is 343 m/s

Total Distance = 343 x Time of High(Echo) \ 2

Total distance is divided by 2 because signal travels from HC-SR04 to object and returns to the module HC-SR04

Step 3: Microcontroller Firmware Programming

Download the С-code program of firmware microcontroller with comments.

Then compiling it to HEX file and uploading to the flash memory of microcontroller.

Flashing Firmware to Microcontroller:

Uploading HEX file into the microcontroller flash memory. Watch the video with a detailed description of the microcontroller flash memory burning: Microcontroller flash memory burning...


Step 4: Ultrasonic Distance Sensor Circuit Assembly

Connect components in accordance with schematic diagram.

Plug power and it is working!