Ultrasonic sensor is very popular electronic sensor in automation and IoT platform which is mainly used to measure the distance. Ultrasonic sensor works based on sending ultrasonic sound signal to the target. It sends ultrasonic pulses to the target at the speed of sound and receives the echo signal. The distance is measured based on the delay in receiving the echo signal. In this work, Distance Measurement by Ultrasonic Sensor (HC-SR04) is discussed and for which HC-SR04 sensor module is interfaced with Arduino controller.
In this project, we are going to make a variable distance measurement system with LED indicators. We have used HC-SR04 ultrasonic sensor for this project. This sensor has four terminals which are, VCC, GND, Trigger, and Echo. Images of the sensor is shown in Figure 1 and 2. VCC and GND are the supply and ground terminals of the sensor respectively. Trigger is the input through which pulse input is applied to the sensor by Arduino. Echo is the output pin of the sensor. The width of the output pulse through Echo pin varies based on the time taken by the transmitted signals to return back to the sensor. This sensor is used here as a scale. We will set the distance value from 7 cm to 49 cm. A buzzer is included in this project to indicate of the lowest voltage.
The left-most LED is used to indicate the farthest distance range which is 49 cm and the right most LED is used to indicate the nearest distance which is 7 cm. It can detect any object between this range and indicate through the LEDs. We are using seven different color LEDs for measuring the distance. Our first LED range is 0 cm to 07 cm (starting from right to left), 2nd <14 cm, 3rd < 21 cm, 4th <28 cm & 7th < 35cm, 6th=42Ccm,7th=49cm. If you need, you may extend the range of the sensor using coding.
Connection between Ultrasonic sensor and Arduino Uno
The Proteus schematic for Ultrasonic sensor based distance measurement is shown in Figure 3.
The physical connection between HC-SR04 and Arduino is done as
- Trigger pin is connected with the 12-number pin of the Arduino.
- Echo pin is connected with 13-number pin of the Arduino.
- VCC is connected with the 5-volt supply pin of the Arduino and GND is connected with the ground pin of the Arduino board.
- As we are using SEVEN LEDs, so we have connected them to 2, 3, 4, 7, 6, 7 and 8 number pins of the Arduino. Colorful LED indicators are good for this purpose.
Distance Calculation
The distance is calculated using this formula.
The Tx of the sensor is Transmitting the ultrasonic sound wave and the receiver is receiving the reflected sound. Now, if the distance from Tx to the object is covered in a particular duration, then it will take 2X time to go out and back to the receiver.
Now, we know that the speed of sound is 343 m/s.
This calculation is used in writing the code for interfacing Ultrasonic sensor with Arduino.
Code for Distance Measurement by Ultrasonic Sensor
const int trig = 12;
const int echo = 13;
const int LED1 = 8;
const int LED2 = 7;
const int LED3 = 6;
const int LED4 = 5;
const int LED5 = 4;
const int LED6 = 3;
const int LED7 = 2;
int duration = 0;
int distance = 0;
void setup()
{
pinMode(trig , OUTPUT);
pinMode(echo , INPUT);
pinMode(LED1 , OUTPUT);
pinMode(LED2 , OUTPUT);
pinMode(LED3 , OUTPUT);
pinMode(LED4 , OUTPUT);
pinMode(LED5 , OUTPUT);
pinMode(LED6 , OUTPUT);
pinMode(LED7 , OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig , HIGH);
delayMicroseconds(1000);
digitalWrite(trig , LOW);
duration = pulseIn(echo , HIGH);
distance = (duration/2) / 28.5 ;
Serial.println(distance);
if ( distance <= 7 )
Experimental Setup for Distance Measurement by Ultrasonic Sensor
The physical connection and experimental setup is shown by Figure 5 and 6.
Conclusion
The project is very useful for making many kinds of DIY projects. We can easily use it for measuring the distance of any objects like a water level indicator for tank, safe distance car parking, social distancing DIY projects, project related to Lift, even it can be used in Submarine also. It is very useful project where we can easily implement this smart device. At the end, the project is very easy, economical, very less time consuming. It may help to explore all DIY ideas those are helpful for our daily life.