Ultrasonic Sensor Distance Measuring Module HC SR-04 Interfacing with Arduino

The HC-SR04 is a long range ultrasonic sensor module for measuring the distance or we can also use it for detecting the objects. The range of the ultrasonic sensor is around 2cm to 400cm. The sensor module has an ultrasonic transmitter and receiver. Ultrasonic sensor working principle is similar to bats and dolphins.

A sensor module transmits ultrasound signal at 40 000 Hz and if there is any object in the path the signal bounce back to the module. It can be used as long distance measurement, range finder, and water level detector.


Ultrasonic sensor price is very low and can be used easily by doing some arduino programming. By getting the travel time and speed of sound we can calculate the distance.

Pinout of HC-SR04 Ultrasonic Module
TrigTrigger(Input) digital pin 12
EchoEcho(Output) digital pin 11

Hardware Required

  • Arduino Uno
  • Ultrasonic Sensor HC-SR04
  • Breadboard

How to use Ultrasound Sensor HC-SR04

Connect the ultrasonic module as given in the above table pinout. For generating the ultrasound signal make Trig pin HIGH for 10us. The HC-SR04 module then sends a sonic burst of 8 cycles and travels the speed of sound and echo back if there is an object in the path. The Echo pin output the time traveled in microseconds.

Ultrasonic Sensor Distance Calculation

The speed of sound is 340.29 m/s or 0.034 cm/µs.
Formula for:
Distance = Time * Speed 
As we know the ultrasound signal travels to the object and bounce back so the travel path, as well as time, will be double. So to get the exact calculation we use this formula:
Distance = Time * Speed / 2 
After getting the distance we are sending this data to the computer using serial monitor.


HC-sr04 Ultrasonic Sensor Arduino Connection

Ultrasonic Sensor Arduino Circuit – How to Wire HC SR04

HC-SR04 Arduino Code

We are sending a HIGH signal of 10us to trigger the sensor module. The total time traveled by ultrasound signal is received by the function pulseIn() in microseconds.

/* HC-SR04 Ultrasound Sensor Connection with Arduino:
VCC  => Arduino +5v 
GND  => Arduino GND
Echo => Digital Pin 11 
Trig => Digital Pin 12

#define echo 11 // Echo Pin
#define trig 12 // Trigger Pin

long travel_time, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trig, OUTPUT);
 pinMode(echo, INPUT);

void loop() {
 digitalWrite(trig, LOW); 
//Sending a high pulse to trigger the Ultrasound Module
 digitalWrite(trig, HIGH);
 digitalWrite(trig, LOW);
 travel_time = pulseIn(echo, HIGH);
 //Calculating the distance
 distance = (travel_time*0.034)/2;
 // Sending the distance to computer 
 //Delay for next reading.

Leave a Reply

Your email address will not be published. Required fields are marked *