Arduino Tutorials

How to measure distances with the HC-SR04

Ultrasonic sensor HC SR04 mounted on the Arduino

With the affordable HC-SR04 ultrasonic sensor you can easily measure smaller distances and detect obstacles. The advantages of this sensor are obvious: It is inexpensive, installed in no time at all and delivers reliable results.

Ultrasonic sensor HC SR04 mounted on the Arduino

Set up & Function

The HC-SR04 sensor is quickly installed and connected:

Supply it with 5V at pin VCC from your Arduino and connect the ground to Gnd.

Then you connect one digital output to the trigger (Trig at the sensor) and another one to the pin Echo.

The ultrasonic sensor works very simply: It receives a signal (or trigger) and sends out an ultrasonic wave. This wave is then reflected by the object or obstacle in front of the sensor and received back by the HC-SR04.

Your Arduino only measures the time that has passed between sending and receiving. In your code you then convert this time span into distance. Done!

By the way: With the sensor you can measure distances between 2cm and about 4 meters – quite a distance.

Here you can find a datasheet for the HC SR04.

The code for the HC-SR04

You don’t have to program much to get the HC-SR04 running.

First define a few variables:

int trigger=7; 
int echo=6; 
long time=0; 
long distance=0; 

Here you first define that the sensor pins Trig and Echo are connected to digital pins 6 and 7 on the Arduino.

You will also need a variable for the time elapsed between sending and receiving and a second one for the distance you calculate from this.

In the setup function you start the serial monitor and set the pinModes: The trigger is an OUTPUT, the echo is an INPUT:

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

Measure the distance

Here we go. You start the measurement with a sound wave, take the time until it returns to the sensor and calculate the distance.

void loop(){

digitalWrite(trigger, HIGH); 
delay(10);
digitalWrite(trigger, LOW);

time = pulseIn(echo, HIGH); 

distance = (time/2) * 0.03432;

Serial.print(distance); 
Serial.println(" cm"); 

delay(1000);}

First you set the trigger to HIGH for 10 milliseconds and then back to LOW. With this you send out the sound wave.

With the function pulseIn() you measure the time until the sound wave reaches the sensor again and store it in the variable time.

With the variable distance you do the following: First you divide the elapsed time by 2 – because you only want to measure the distance from the sensor to the obstacle. Then you multiply this time by the speed of sound in centimeters per microsecond.

Now you only display the calculated distance in your serial monitor and limit the measurements by delay() to one per second.

Here comes the complete code:

int trigger=7; 
int echo=6; 
long time=0; 
long distance=0; 

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

void loop(){
digitalWrite(trigger, HIGH); 
delay(10);
digitalWrite(trigger, LOW);

time = pulseIn(echo, HIGH); 

distance = (time/2) * 0.03432; 

Serial.print(distance); 
Serial.println(" cm"); 

delay(1000);
}

In which projects can you use the ultrasonic sensor HC SR04? For example in a vehicle that can avoid obstacles. Or simply to measure the distance to an object. 🙂

In this project you will learn how to create your own Arduino Theremin. Or maybe a different sensor? How about using temperature instead of distance?

You may also like

Comments are closed.