Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...

In this tutorial we are going to interface ultrasonic distance sensor HC-SR04 (proximity sensor) with Arduino and LCD. Ultrasonic sensor is used to measure the distance. It is also known as the proximity sensor or the sonar sensor. It sends an ultrasonic wave (sound wave) which comes back after hitting the object and tells us the time traveled by it. By using this time we calculate the distance traveled. The working of distance sensor is explained briefly in this tutorial.

Specifications

The specifications of the ultrasonic distance sensor HC-SR04 are below

Minimum measuring range 2 cm
Maximum measuring range 400 cm or 4 meter
Operating Voltage 5V
Operating Current 15mA
Working Frequency 40 KHz
Trigger Input signal 10us pulse
Measuring angle 15 degree

Circuit Diagram and Explanation

Ultrasonic distance sensor has four pins

VCC: Connect that to 5V pin of Arduino.

Trig: Connect that to pin 2 of Arduino.

Echo: Connect that to pin 3 of Arduino.

GND: Connect that to ground of Arduino.

ultrasonic distance sensor hc-sro4 arduino interfacing

HC-SR04 Interfacing with Arduino

For displaying the data on the LCD. We will connect a 16X2 LCD with the Arduino. Connect the wires as shown in the diagram below. If you want to read about interfacing LCD with Arduino in detail, then read this tutorial | Arduino LCD Interfacing Tutorial

ultrasonic distance sensor hc-sro4 arduino LCD interfacing

HC-SR04 Interfacing with Arduino and LCD

Working of Ultrasonic distance sensor HC-SR04

The ultrasonic distance sensor HC-SR04 emits an ultrasonic wave from the trigger and it is received by the echo after deflecting an object. In order to generate a wave, we will have to set the trigger at high for 10 us which will send a 8 cycle sonic burst at 40 KHz which will hit the object and after hitting the object, the wave will be received by the echo. The echo will then tell us the time that the wave have traveled in us (micro seconds).

We will have to convert this time into cm to calculate the distance traveled. We will use the following equation to calculate the distance.

S = v * t

Ultrasonic wave is basically a sound wave which travels at a speed of 340 m/s. To calculate the distance we will have to convert speed into cm/us which is 0.034 cm/us. The ultrasonic sensor is measuring the time it takes to hit the object and then come back but we need only time that it takes to hit the object. So, we will divide it by 2. The equation will become

S = (t * 0.034)/2

To calculate the distance in inches, we will have to convert the cm in inches. One cm is equal to 2.54 inches. So, we will divide the 0.034 cm/us by 2.54 to convert it into inches/us. To calculate the distance in inches, the equation will become

S = (t * 0.0133)/2

Code

In the following code, we are going to interface the distance sensor with Arduino and display the distance on the serial monitor.

const int triggerpin = 2;     //Connect the trigger pin at pin 2
const int echopin = 3;        //Connect the echo pin at pin 3
long time;                    //Variable for storing the time traveled
int S;                        //Variable for storing the distance covered
int inch;                     //Variable for storing the distance in inches
void setup() {
pinMode(triggerpin, OUTPUT);  //Setting the triggerpin as output pin
pinMode(echopin, INPUT);      //Setting the echo pin as input pin
Serial.begin(9600);       
}
void loop() {

digitalWrite(triggerpin, LOW);  
delayMicroseconds(2);

digitalWrite(triggerpin, HIGH); //Setting the triggerpin high for 10us to generate a wave
delayMicroseconds(10);
digitalWrite(triggerpin, LOW);

time = pulseIn(echopin, HIGH); //Setting the echopin high to receive the wave

S= time*0.034/2;                //Calculating the distance traveled in cm
inch = time*0.0133/2;           //Calculating the disatnce traveled in inches

Serial.print("Distance: ");
Serial.print(S);
Serial.print(" cm,  ");
Serial.print(inch);
Serial.println(" inch ");
delay(500);
}

Code for displaying distance on LCD display

In the following code, we are going to read from the ultrasonic distance sensor and display the distance on the LCD display.

#include <LiquidCrystal.h>          //Including the LiquidCrystal Library
LiquidCrystal lcd(12,11,10,9,8,7); 

const int triggerpin = 2;     //Connect the trigger pin at pin 2
const int echopin = 3;        //Connect the echo pin at pin 3
long time;                    //Variable for storing the time traveled
int S;                        //Variable for storing the distance covered
int inch;                     //Variable for storing the distance in inches
void setup() {
lcd.begin(16,2);              //Initializing the interface of the LCD
pinMode(triggerpin, OUTPUT);  //Setting the triggerpin as output pin
pinMode(echopin, INPUT);      //Setting the echo pin as input pin
Serial.begin(9600);       
}
void loop() {
lcd.clear();
digitalWrite(triggerpin, LOW);  
delayMicroseconds(2);

digitalWrite(triggerpin, HIGH); //Setting the triggerpin high for 10us to generate a wave
delayMicroseconds(10);
digitalWrite(triggerpin, LOW);

time = pulseIn(echopin, HIGH); //Setting the echopin high to receive the wave

S= time*0.034/2;                //Calculating the distance traveled in cm
inch = time*0.0133/2;           //Calculating the disatnce traveled in inches

lcd.setCursor(0,0);             // Sets the location at start
lcd.print("Distance: "); 
lcd.print(S); 
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(inch);
lcd.print(" inch");
delay(1000);
}

Video

If you have any questions then feel free to ask us in the comment section.

JOIN OUR NEWSLETTER
We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
We hate spam. Your email address will not be sold or shared with anyone else.

Leave a Reply

Your email address will not be published.

Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...
Skip to toolbar