Arduino Projects

Measure distance using HC-SR04 ultrasonic sensor with Arduino

Learn how to build an Arduino distance sensor by interfacing HC-SR04 sensor with Arduino and display on LCD display module.

In this Arduino project, let us learn how to interface HC-SR04 ultrasonic distance sensor with Arduino to build an Arduino Distance sensor and display the distance measurements on a 16X2 LCD display in centimeters and inches.

Article Flow:

  • Learn what is HC-SR04 sensor and its working
  • Know how to connect HC-SR04 with Arduino
  • Write a Program code to measure distance in Cms and inches to display on LCD display.

What is HC-SR04? and how it works

HC-SR04 is an ultrasonic distance sensor that uses ultrasound to measure the distance of an object with a range of 2cms to 400cms(4 meters)  with decent accuracy at a cheaper cost. It stands out for its small size, good precision,  and low power consumption. Light has not effect on the measurements.

HC-SR04 ultrasonic sensor pinout:

ultrasonic distance sensor

As you can see from the above image, the sensor has 4 pins. 2 pins, Vcc and Gnd, are for powering up the sensor and the other 2 pins, Trig and Echo, are to send and receive the ultrasonic pulses and measure the time taken to reach and get back to the sensor to calculate the distance of an object which we discuss below.

Interesting Article: DIY Measuring Wheel using Arduino and Rotary Encoder

Principle and Working:

This sensor has 2 transducers: one is an emitter, and the other one is a receiver which are Piezoelectric. To start measuring, the emitter emits 8 ultrasound pulses (40KHz) after receiving the HIGH command on the TRIG pin. The emitted sound waves travel in the air and bounce back when they hit an object, and the receiver detects the bounced-back sound waves. The ECHO pin stays on High (5 Volts) from the time when the sound wave is emitted to the time the bounced-back sound wave is detected. The microcontroller measures the time of the ECHO pulse. Thus, the distance can be calculated to the object.

ultra sonic sensor working

In Simple terms: The operation is very simple. The sensor sends an ultrasonic sound wave through the trigger or trigger, bounces off the object and the receiver or echo detects the wave. By measuring the time taken for the wave from emission till it gets back we can know the distance.

The distance is calculated with the below formula which we learnt at school.

Velocity(v) = Distance(s)/time(t). ⇒ Distance(s)= Velocity(v) X time(t).

As we know the speed of the sound in air is 343 meters per second(1234.8kmph) at 20°C, and we can measure the time between the emitter and receiver. By multiplying speed with time we can calculate the distance.

Distance = {(ECHO pulse time) * (Sound speed=0.0347cm/s)}/2

As the ECHO signal time is the time taken by the sound wave to travel from the sensor to the object and the object to the sensor, we need to divide it by 2 to get the distance between the sensor and the object.

Technical Specifications

  • Operating Voltage: 5V DC
  • Current when inactive: < 2mA
  • Current while measuring: 15mA
  • Range: 2cm to 400cm
  • Ultrasound frequency: 40KHz
  • Accuracy: +- 3mm
  • Minimum TRIG trigger pulse duration: 10 μS
  • Minimum waiting time between measurements: 20ms (recommended 50ms).

For more details refer the HC-SR04 datasheet: here

Interfacing HC-SR04 with Arduino and LCD Display.

Required Components:

Product NameQuantityamazon logoamazon logo india
HC-SR04 ultrasonic distance sensor1https://amzn.to/3CdXXAihttps://amzn.to/3sVUiUF
Arduino Uno Microcontroller1https://amzn.to/3H4cKxZhttps://amzn.to/3638aTS
LCD 16X2 module with or without I2C adapter1https://amzn.to/3BNSRuphttps://amzn.to/363ki7F
Breadboardhttps://amzn.to/3CcBR16https://amzn.to/3tH3k6X
5V power supply (Micro USB or External).1https://amzn.to/3s1a8g3https://amzn.to/364yInH
Few Connecting Wireshttps://amzn.to/3H2BV4ehttps://amzn.to/3J0WVu2
Here are the required components along with Amazon buying links at best prices.

Circuit Diagram:

Now Connect all the above given required components shown in the below circuit diagram to build an Arduino Distance meter.

interfacing hcsr04 ultrasonic sensor with arduino and lcd display

As you can see from the above circuit diagram, HC-SR04 ultrasonic distance sensor power pins, Vcc and GND are connected to Arduino’s 5V and GND pins. The other 2 pins, Trig and Echo of the sensor are connected to digital pins 8 and 7 of Arduino, respectively.

Next LCD display is connected to Arduino using the I2C communication protocol. VCC and GND pins of LCD are connected to 5V and GND pins of Arduino and SDA and SCL pins of LCD are connected to A4 and A5 pins of Arduino, respectively.

If you don’t like to use the I2c adapter for LCD display refer this article: Interfacing LCD Display with Arduino in detail

It is recommended to place the sensor on the breadboard to make it stable and measure the distances precisely.

Also read: Measure Wind Speed using Anemometer and Arduino

Program code:

Now its time to upload the code to Arduino using Arduino IDE to make it measure distance of an object. Copy the below program code and paste it in the Arduino Workspace. Select the correct board and port from the Tools menu and Hit upload button.

Required Libraries:

  •  LiquidCrystal_I2C: Download here.
// Compiled and tested with no errors
// Measure distance using HC-SR04 ultrasonic sensor with Arduino
// source - www.circuitschools.com
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

int trigPin = 8;      //declare pin for trigger pin of UltraSonic sensor; 
int echoPin = 7;      //declare pin for echo pin of UltraSonic sensor;
float speed = 0.0347; //declare speed of sound in air @ room temp;
int dist;           //declare variable for containing distance sensed;
float pingTime;       //declare variable for containing echo time;
float distinch;

void setup() {
  Serial.begin(9600);

  pinMode(trigPin,OUTPUT);    //set trigger pin as outpin;
  pinMode(echoPin,INPUT);     //set echo pin as input;
  // Initialize LCD
  lcd.init();
  lcd.backlight();
  lcd.print ("CIRCUITSCHOOLS..");
  lcd. setCursor (0, 1);
  lcd.print ("Distance sensor");
  
delay(2000);
lcd.clear();
}

void loop() {
  digitalWrite(trigPin,LOW);
  delayMicroseconds(20);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);          //creating a pulse for sensing distance;
  pingTime = pulseIn(echoPin,HIGH);   //read the echoTime, &hence the distance;
  dist = (speed*pingTime*0.5);  
  distinch = dist/2.54;  
  Serial.print("Distance: ");
  Serial.print(dist); 
  Serial.print("cms --- ");
  Serial.print(distinch); 
  Serial.println("inches"); 
  lcd.setCursor(0, 0);
  lcd.print(dist);
  lcd.print("cms");
  lcd.setCursor(0, 1);
  lcd.print(distinch);
  lcd.print("inch");

delay(250);
  }

After uploading the code open serial monitor set the baud rate to 9600 and see the measurements in cms and inches as shown in the below output serial monitor screenshot.

distance sensor hc-sr04 arduino output on serial monitor

 

The same measurements are also displayed on the 16X2 LCD display as below image.

distance sensor hc-sr04 arduino output on lcd display

Applications:

  • Can be used in Robots and robot cars to avoid obstacles
  • Can used as Car parking sensor : Check our project Car Parking sensor with Arduino
  • Burglar alarms
  • Depth measurement (example: water level in tank).
  • Build a radar system to check nearby obstacles by rotating.

CircuitSchools Staff

We at CircuitSchools publish on basics of electronics and electric components and everything related to evolution of electrical technology with complete analysis of development boards and modules along with latest projects with innovative ideas.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button