Interfacing HC-RS04 Ultrasonic Sensor with Arduino

samathi sapumana
4 min readSep 7, 2021

Hello everyone! Hope you all doing great… Today I’m here with another interesting topic that is how to interface an HC-RS04 Ultrasonic Sensor with Arduino. First, let’s go through basic information about the sensor.

Pin Configuration of Ultrasonic Sensor.

The module has only 4 pins, Vcc, Gnd, Trig, and Echo

HC-SR04 Ultrasonic Sensor

01. VCC — The VCC pin powers the sensor, typically with +5V

02. Trigger- The trigger pin is an input pin. This pin has to be kept high for 10us to initialize measurement by sending a US wave.

03. Echo- The echo pin is an output pin. This pin goes high for a period that will be equal to the time taken for the US wave to return to the sensor.

04. Ground- This pin is connected to the Ground of the system.

The working mechanism of the Sensor.

HC-SR-04 module has an ultrasonic transmitter, receiver, and control circuit on a single board. Here is the mechanism of the sensor. Trigger pins send sound waves above 40KHz in 8 cycles of the burst for 10 microseconds. After this, the Echo pin is made high by the control circuit in the module. The echo pin remains high till it gets an echo signal of the transmitted pulses back. If an object is detected, the wave will hit the object and bounce back. Then the echo pin will send the signal back. The system will start to measure the time when the sound wave is sent and stop measuring the time when the wave returns. You can calculate the distance using the formula,

Distance = Velocity of sound in air * Time.

Now let us see the coding part of the sensor on Arduino IDE. Open your IDE and let's begin coding.

First, let me define the trigger pin and echo pin. Here I'm using pin numbers 9 and 10 for it.

const int trigger_pin = 9;
const int echo_pin = 10;

Next, I’m defining 2 variables to store the distance to the object and the time duration which is given by the sensor.

long time_duration;
long distance_to_object;

In the setup let's define trigger pin as output and echo pin as input and start serial communication.

void setup(){
pinMode (trigger_pin,OUTPUT);
pinMode (echo_pin , INPUT);
pinMode (buzzer , OUTPUT);
Serial.begin (9600);
}

In the loop first, you have to clear the trig pin then make the trig pin high for 10 microseconds. pulseIn(); is the function that helps to measure the time duration that takes a sound wave to hit an object and bounce back. I'm assigning that value to the variable time_duration. After that distance to the object is calculated in centimeters. I have taken velocity of sound in air is 340 ms-1

distance_to_object = (time_duration*0.034)/2;

Here is the full coding part for the loop.

void loop()
{
digitalWrite (trigger_pin , LOW);
delayMicroseconds (2);

digitalWrite (trigger_pin , HIGH);
delayMicroseconds (10);

digitalWrite (trigger_pin , LOW);
time_duration = pulseIn (echo_pin ,HIGH);
distance_to_object = (time_duration*0.034)/2;
Serial.print ("Distance: ");
Serial.println ( distance_to_object);
delayMicroseconds (10);

}

Let's see how to stimulate this in proteus.

You can connect a variable resistor to the test pin of the Ultrasonic Sensor to change the distance. Virtual terminal is added to demonstrate the distance as you change it.

Let's do something extra with this. Let me add a Buzzer to the circuit and make it active when the distance is measured less than 20cm. For this, I’m adding a new function called buzzerFunction();. Here is the modified code with the buzzer.

/* code for Ultrasonic sensor - To calculate distance between Object         and the sensor
By : Sapumana S.k
*/
const int trigger_pin = 9;
const int echo_pin = 10;
int buzzer = 21;
long time_duration;
long distance_to_object;
void setup(){
pinMode (trigger_pin,OUTPUT);
pinMode (echo_pin , INPUT);
pinMode (buzzer , OUTPUT);
Serial.begin (9600);
}
void loop(){
digitalWrite (trigger_pin , LOW);
delayMicroseconds (2);

digitalWrite (trigger_pin , HIGH);
delayMicroseconds (10);

digitalWrite (trigger_pin , LOW);
time_duration = pulseIn (echo_pin ,HIGH);
distance_to_object = (time_duration*0.034)/2;
Serial.print ("Distance: ");
Serial.println ( distance_to_object);
delayMicroseconds (10);
buzzerFunction(distance_to_object);

}

void buzzerFunction( long d){
if (d < 20){
digitalWrite (buzzer , HIGH);
delay (2000);
digitalWrite (buzzer , LOW);
delay (2000);
}

}

here is the proteus setup for it. Make sure to set the voltage of your buzzer between 3V — 6V before stimulating.

I hope you enjoy it. Try it out. See you soon with a new blog.

--

--

samathi sapumana

A passionate IT Undergraduate from University of Moratuwa