Interface Ultrasonic With AVR


Ultrasonic sensors act like a bat. The HC-SR04 ultrasonic sensor uses sonar to determine distance.

  • It offers excellent range detection with high accuracy and stable reading.
  • It offers range detection from 2cm to 400 cm or 1 to 13 feet.
  • Ultrasonic sensor consists of a Transmitter and Receiver module.


  • The sensor trigger pin must receive a pulse of high (5V) for 10us.
  • Then the sensor will transmit out 8 cycles of 40kHz ultrasonic burst.
  • Sensor waits for the reflected ultrasonic burst.
  • When the sensor detected ultrasonic from the receiver, it will set the Echo pin to high (5V) and delay.
  • The delay period is a width which proportion to distance.
  • To obtain the distance, measure the width (Ton) of the Echo pin.

Timing Diagram

Distance Calculation

  • Time = Width of Echo pulse, in uS (microsecond).
  • Distance in centimeters = Time/ 58.
  • Distance in inches = Time /148.


  • Power Supply: +5V DC.
  • Quiescent Current : <2mA.
  • Working Current: 15mA.
  • Effectual Angle: <15 Degree.
  • Ranging Distance : 2cm to 400 cm/1 to 13ft.
  • Resolution : 0.3 cm.
  • Measuring Angle: 30 degrees.
  • Trigger Input Pulse width: 10uS.
  • Dimension: 45mm x 20mm x 15mm.

Hardware Required

  • Ultrasonic HC-SR04.
  • Atmega16 MicroController

Pin Diagram

  • VCC: +5VDC.
  • Trig: Trigger (INPUT).
  • Echo: Echo (OUTPUT).
  • GND: GND.