How to interface HC-SR04 ultrasonic sensor with Arduino

What is the ultrasonic sensor? How ultrasonic sensor works & how to use an ultrasonic sensor with Arduino.

Hello guys! Welcome back to my blog. Today we are going to see about the HC-SR04 module.

What is Ultrasonic Sound & Ultrasonic Sensor??

ultrasonic distance sensor

Have you all noticed how BATS fly and hunt in dark at night time very easily even if we human beings are not able to walk in dark? Also, how do whales & dolphins see under deep water where sunlight is limited? , the answer is they use ultrasonic frequency waves. Ultrasonic frequencies are higher frequencies above 20 kHz. We humans can only hear sounds of frequency between 20 Hz to 20 kHz. The ultrasonic sensor is the device that is used to measure the distance of an object using ultrasonic sound frequency. Ultrasonic sound waves get reflected back after hitting a surface just like echo signals.

ultrasonic

Humans cannot hear the ultrasonic sound as it has a higher frequency above 20 kHz. Animal-like bats, dolphin whales, and other animals use ultrasound to find their path, navigate, and for hunting which maximizes their ability to see in the dark and under deep water. Ultrasonic sensor uses time parameter to calculate the distance of a target by using sending and receiving pulse reflected from the target (except for soft surfaces as they absorbed ultrasonic waves). Ultrasonic sensors are types of non-contact detection sensors.


sonar sensor

how ultrasonic sensor works??

First, we have to give a pulse of at least 10 microseconds (10 µsduration to trigger the pin. This encourages the transmitter to send eight pulses of 40 kHz, the transmitter is a transducer that converts an electrical signal into ultrasound pulses and the receiver listens for transmitted pulses that are sent by the transmitter. The width of receive pulse is used to determine the distance of the object.

ultrasonic level transmitter

If there is no object present then the pulse goes high for 38 milliseconds (38ms) and gets back to low. If there is an object present within the range then pulse width varies according to the proximity of the object. For example, microseconds (20 µs) are present.


The closer the object minimum the width of pulse reflected from the object. 

ultrasonic sensor range

First, we have to send a pulse of 10 microseconds (10 µs) to the trigger pin, after the transmitter transmits 8-pulse of 40 kHz and receiver starts listening to this signal. When there is no object present then no signal will be reflected back so by default echo pins gets high only for 38 milliseconds (38ms) for searching reflected signal. If still there is no signal detected then the echo pin will get low after 38 milliseconds (38ms).

ultrasonic transducer

If there is an object present. The signal will get reflected after hitting that object and gets detected by the receiver. After receiving a signal at the receiver end, the echo pin will soon get low. The width of the received signal is used to determine the distance of the object. Closer the object lesser the width because less time will be required for the signal to reach the object and return back to the receiver.
We use the pulseIn() function to calculate the width of the reflected signal. the signal from pulseIn() determines how much time period the signal received was high. So using this time we can calculate distance.

By knowing the formula - 

                                                     Distance = speed x time

By knowing this parameter of this equation. We can calculate distance. We know the speed of sound is
                                                  Distance = 0.034 cm/µs x Time
We have to calculate the time which we will get after the reflected signal receives at the receiver end
Also, we have to divide the equation by half because the time which we will get will be required to reach the object and get reflected back. So the equation will be
                                                 Distance = (0.034 cm/µs x Time) / 2  
 

340 m/s. we have converted the speed of sound into cm/µs to calculate distance. So we get 0.034 cm/µs.

Application of ultrasonic sensor

  • Distance Measuring
  • Ultrasonic Level Sensors
  • Obstacle Detection
  • People counting detection
  • On Drones for avoiding collision with the ground, and crashing.

Parts Required

  • HC-SR04 (ultrasonic sensor module)
  • Arduino
  • Jumper wires
  • Breadboard

Schematic Diagram

ultrasonic distance sensor Arduino

Connect the ground pin of the ultrasonic sensor to the ground pin of Arduino, VCC pin to 5v, trigger pin to 8 & echo pin to 9 of the Arduino board.

Code

int trigger = 8;    // Set trigger pin at Arduino pin 8
int echo = 9;       // Set echo pin at Arduino pin 9
int ref_signal;     // declare variable to store width of pulse
int distance;       // declare variable to calculate distance
void setup() {
  Serial.begin(115200);      // set baud rate for Serial communication
  pinMode(trigger,OUTPUT);  // trigger pin set to output
  pinMode(echo,INPUT);      // echo pin set to output
}
void loop() {
   digitalWrite(trigger, LOW);   // make trigger pin low-to-high-low to generate pulse of 10µs
   delayMicroseconds(2);
   digitalWrite(trigger,HIGH);
   delayMicroseconds(10);
   digitalWrite(trigger,LOW);
   ref_signal=pulseIn(echo,HIGH); // use pulse In function to calculate amount of time pulse was high 
  // Distance = speed x time 
  distance = (0.034*ref_signal)/2;     // speed of sound in cm/µs 
  
  // divide the equation by half because the time which we will get will be required to reach the object and get reflected back.
  
  Serial.print("Distance:");       // print the output 
  Serial.print(distance);      
  Serial.print("cm");  
  Serial.println(" ");
  delay(100);
}

First, initialize pins of ultrasonic sensor and variable. We have declared variables to read values from sensors. In the void setup, we configure pins as INPUT or OUTPUT. We want to read values (calculate the time of echo pulse being HIGH) from the sensor pin so we will declare it as INPUT. Also, we configure the baud rate to 115200.

In a void loop, we continuously read values from the ultrasonic sensor. we make the trigger pin low-to-high-low to generate a pulse of 10µs. so that transmitter transmits 8-pulse of the signal. the signal is transmitted by the transmitter and the receiver tries to listen to the signal if the object is present and reflected back. The width of the received signal is used to determine the distance of the object. The closer the object lesser is the width because less time will be required for the signal to reach the object and return back to the receiver.

We use the pulseIn() function to calculate the width of the reflected signal. 

 We have introduced a delay of 100 milliseconds.

OUTPUT -

ultrasonic detector

Now we used the serial monitor to observe the output. . To open up the serial monitor, click on Tools > Serial Monitor or use the shortcut (SHIFT + CONTROL + M). You can see output on the Serial monitor. When we keep an object or hand in front of the sensor the output will vary according to the position of the object.

 Conclusion - 

Today we learn about what is an ultrasonic sensor, how to use an ultrasonic sensor with Arduino, & working principle of ultrasonic sensors.

 

"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."


Close Menu