Introduction
In this tutorial, we are going to make Measure Distance using HC-SR04 Ultrasonic Sensor with Arduino The HC-SR04 Ultrasonic Sensor is a widely used distance-measuring sensor that works based on sound waves. It has a range of about 2-400 cm and works by sending an ultrasonic pulse and then measuring the time it takes for the pulse to bounce back after hitting an obstacle. It is a low-cost, easy-to-use sensor that can be interfaced with microcontrollers like Arduino and Raspberry Pi.
Hardware Required
You will require the following Hardware Components for interfacing the HC-SR04 Ultrasonic Sensor with Arduino.
Components | # | Buy From Amazon |
---|---|---|
Arduino UNO | 1 | Buy Now |
16×2 LCD display | 1 | Buy Now |
HC-SR04 Ultrasonic Sensor | 1 | Buy Now |
Potentiometer 10KΩ | 1 | Buy Now |
Resistor 1KΩ | 1 | Buy Now |
Jumper Wires | Few | Buy Now |
Breadboard | 1 | Buy Now |
What is the HC-SR04 Ultrasonic Sensor?
The HC-SR04 Ultrasonic Sensor is a popular sensor module used in electronics projects to measure distances between objects. It works by emitting high-frequency sound waves that bounce off nearby objects and return to the sensor. By measuring the time it takes for the sound waves to return, the sensor can calculate the distance to the object. The HC-SR04 Ultrasonic Sensor is small, affordable, and easy to use, making it a popular choice for hobbyists and DIY enthusiasts. It can be used in a wide range of applications, including robotics, home automation, and security systems.
Pinout of HC-SR04 Sensor
Pinout Configuration of HC-SR04 Sensor
Pin Name | Description |
---|---|
Vcc | The Vcc pin powers the sensor, typically with +5V |
Trigger | Trigger pin is an Input pin. This pin has to be kept high for 10us to initialize measurement by sending US wave. |
Echo | Echo pin is an Output pin. This pin goes high for a period of time which will be equal to the time taken for the US wave to return back to the sensor. |
Ground | This pin is connected to the Ground of the system. |
Specifications of HC-SR04 Sensor
- Operating Voltage: 5V DC
- Operating Current: <15mA
- Detection Range: 2cm to 400cm
- Resolution: 0.3 cm
- Frequency: 40 KHz
- Trigger Input Signal: 10μS TTL pulse
Features of HC-SR04 Sensor
- Easy to use and interface with microcontrollers like Arduino and Raspberry Pi
- Low cost and widely available
- Accurate and reliable distance measurements
- Non-contact measurement
- Small form factor
Circuit Diagram
The following circuit shows you the connection of the HC-SR04 Ultrasonic Sensor with Arduino Please make the connection carefully
Working Explanation
The working of the Measure Distance using HC-SR04 Ultrasonic is based on the principle of Time of Flight (ToF). When a trigger pulse is sent to the sensor, it emits an ultrasonic pulse of 40 KHz, which travels through the air until it hits an obstacle. The pulse then bounces back and is received by the sensor. The time taken by the pulse to travel back to the sensor is measured, and using the speed of sound in air, the distance to the obstacle is calculated.
To use the HC-SR04 Ultrasonic Sensor with Arduino, connect the VCC and GND pins of the sensor to the 5V and GND pins of the Arduino board, respectively. Connect the Echo and Trig pins of the sensor to any digital pins of the Arduino board. To measure the distance using the sensor, send a trigger pulse of 10 microseconds to the Trig pin, which will activate the sensor to send the ultrasonic pulse. The sensor will then receive the reflected pulse and output a pulse on the Echo pin. The time between the trigger pulse and the received pulse can be measured using the pulseIn() function of the Arduino, and the distance can be calculated using the formula mentioned earlier.
Installing Arduino IDE Software
First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow
Code
//For more Projects: www.arduinocircuit.com
#include <LiquidCrystal.h>
int triggerPort = 7;
int echoPort = 8;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// display connection (pin)
void setup() {
pinMode( triggerPort, OUTPUT );
pinMode( echoPort, INPUT );
Serial.begin( 9600 );
Serial.println( "Ultrasonic sensor: ");
lcd.begin(16, 2);
// set display type (columns, rows)
lcd.print("Distance:");
}
void loop() {
//set the trigger output low
digitalWrite( triggerPort, LOW );
//send a 10microsec pulse on trigger
digitalWrite( triggerPort, HIGH );
delayMicroseconds( 10 );
digitalWrite( triggerPort, LOW );
long duration = pulseIn( echoPort, HIGH );
long r = 0.034 * duration / 2;
Serial.print( "duration: " );
Serial.print( duration );
Serial.print( " , " );
Serial.print( "distance: " );
if( duration > 38000 ) Serial.println( "out of range");
else { Serial.print( r ); Serial.println( "cm" );}
// place the cursor at column 12 and row 0
if(r > 99){
lcd.setCursor(13, 0);
lcd.print(r);}
if(r <= 99 && r > 9){
lcd.setCursor(13, 0);
lcd.print(r);
lcd.setCursor(15, 0);
lcd.print(" ");}
if(r <= 9){
lcd.setCursor(13, 0);
lcd.print(r);
lcd.setCursor(14, 0);
lcd.print(" ");}
delay(300);
}
Applications
- Robotics for obstacle avoidance and navigation
- Security systems for intruder detection and proximity sensing
- Industrial automation for distance measurement and monitoring
- Automotive parking sensors
- Smart home devices for occupancy detection and motion sensing
Conclusion
The HC-SR04 Ultrasonic Sensor is a versatile and reliable sensor that can be used in a wide range of applications requiring distance measurement. Its ease of use and low cost make it a popular choice for hobbyists, students, and professionals alike. With its non-contact measurement and accurate results, it is a valuable tool for robotics, automation, and smart home devices.