Distance measurement using ATMEGA328p and HC-SR04

abytom87's picture
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1



Distance measurement using HC-SR04 and ATMEGA328p. In this project i have measured distance in centimeters, with the help of HC-SR04 Ultrasound sensor, ATMEGA328p micro-controller, LCD Display via I2C bus.


Timer2 of ATMEGA328p is used to generate a Trigger pulse of 20uS, the ultrasonic module sends out a 8cycle burst of 40khz which hits the object surface and returns back to raise an echo pulse. The pulse-width of this pulse is proportional to the distance between the module and Object.

Input capture module of the ATMEGA was used to capture the time between rising and falling edges of the echo pulse. The prescaler of this unit was chosen, such that the resolution of pulse-width is 16uS.

The display in use is LCD1604 which has an integrated PC8574 chip that converts serial data (via I2C bus) into parallel stream of bits for the LCD



Hardware used:

  • Arduino-UNO
  • HC-SR04 Ultrasonic sensor
  • LCD Display with integrated PCf8574T chip enabling serial (I2C from uC) to parallel conversion.
  • 2.2k Pull-up resistors for I2C bus.

SW and Tool-chain:

  • Software was written entirely in C,
  • Developed in Eclipse CDT,
  • AVR-GCC compiler was used,
  • AVRDUDE was used to program the hex onto the target,
  • Operating System - Ubuntu(Linux)