Introduction: Measuring Distance With Ultrasonic Sensor Using Arduino and Printing on a 1602 LCD Display

Hi guys in this instructables we will make a project which can tell the distance of nearby object and print it on the lcd display attached to arduino uno. This project can be very useful when creating a autodriving robot car or the Projects which requires measuring the distance of a obstacle or object. So basically ultrasonic sensor we will be using to read the distance of object from the sensor and this data will be read by arduino from ultrasonic sensor and then arduino will print this data on 1602 LCD display.

Step 1: Things You Need

For this instructables we will be needing following things :


Arduino Uno or Pro Mini or nano


Ultrasonic sensor Module
16x2 LCD

Bread board

Connecting wires

Step 2: Circuit Diagram

The circuit diagram for arduino and ultrasonic sensor is shown above to measure the distance. In circuit connections Ultrasonic sensor module’s “trigger” and “echo” pins are directly connected to pin 18(A4) and 19(A5) of arduino. A 16x2 LCD is connected with arduino in 4-bit mode. Control pin RS, RW and En are directly connected to arduino pin 2, GND and 3. And data pin D4-D7 is connected to 4, 5, 6 and 7 of arduino.

Step 3: Working

we need to trigger the ultrasonic sensor module to transmit signal by using arduino and then wait for receive ECHO. Arduino reads the time between triggering and Received ECHO. We know that speed of sound is around 340m/s. so we can calculate distance by using given formula:
Distance= (travel time/2) * speed of sound
Where speed of sound around 340m per second.
A 16x2 LCD is used for displaying distance.

Step 4: Code

Please copy the following code & Upload it to your arduino Board ;

#include "LiquidCrystal.h"

#define trigger 18
#define echo 19

LiquidCrystal lcd(2,3,4,5,6,7);

float time=0,distance=0;

void setup()
{
lcd.begin(16,2);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
lcd.print(" Ultra sonic");
lcd.setCursor(0,1);
lcd.print("Distance Meter");
delay(2000);
lcd.clear();
delay(2000);
}

void loop()
{
lcd.clear();
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
lcd.clear();
lcd.print("Distance:");
lcd.print(distance);
lcd.print("cm");
lcd.setCursor(0,1);
lcd.print("Distance:");
lcd.print(distance/100);
lcd.print("m");
delay(1000);
}

Step 5: Measuring Distance in Real Time

So after connecting everything together & uploading code to arduino board and if everything is fine then if we put a object in front of ultrasonic sensor we can see the distance of object in lcd display as you can see different different distance values in the images. So have fun using ultrasonic sensor with arduino and measure distance with arduino.