Call Us Now
0242159130 | 0546096840
Up to 2% reward

For new all Customers

Delevery schedule

We deliver  the same day through VIP bus parcel Service in Ghana

Discount on order gift

Save ¢10.00 when you spend ¢600+! 

Ultrasonic Distance Sensor

Ultrasonic Distance Sensor

In this tutorial,we will learn how to measure the distance by the ultrasonic distance sensor. 

Hardware required
1 * Arduino UNO
1 * USB Cable
1 * LCD1602

1*Ultrasonic Distance Sensor 

1 * LCD1602

1 * 10kΩ Potentiometer 

Several jumper wires 

Principle of operation

This recipe uses the popular Parallax PING ultrasonic distance sensor to measure the distance to an object ranging from 2cm to around 3m.

Ultrasonic sensors provide a measurement of the time it takes for sound to bounce off an object and return to the sensor. The “ping” sound pulse is generated when the pingPin level goes HIGH for two micro-seconds. The sensorwillthengenerateapulsethatterminateswhenthesoundreturns.The width of the pulse is proportional to the distance the sound traveled and the sketch then uses the p uls e I n ( ) function to measure that duration.The speed of sound is 340meters per second,which is 29 microseconds per

centimeter.The formula for the distance of the round trip is:

RoundTrip = microseconds / 29.

So, the formula for the one-way distance in centimeters is:

microseconds / 29 / 2

Experiment code

//File name:   Experiment 19


Description: When you move the ultrasonic distance sensor,

             you will find LCD1602 display distance.




//Author: Okudzeto osei prince

//Date: 2019/08/19

#include <LiquidCrystal.h>

const int pingPin = 5;  // pin connected to Echo Pin in the ultrasonic distance sensor

const int trigPin  = 7; // pin connected to trig Pin in the ultrasonic distance sensor

LiquidCrystal lcd(4, 6, 10, 11, 12, 13);   

void setup() 


   pinMode(pingPin, INPUT); //Set the connection pin output mode Echo pin

   pinMode(trigPin, OUTPUT);//Set the connection pin output mode trog pin

   lcd.begin(16, 2);        //set up the LCD's number of columns and rows: 

   lcd.clear();             //Clears the LCD screen and positions the cursor in the upper-left corner 

   delay(1000);             //delay 1000ms

void loop() 

   int cm = ping(pingPin) ; 

   lcd.setCursor(0, 0);           // set the cursor to column 0, line 0

    lcd.print("     oku     ");// Print a message of "oku: "to the LCD.

    lcd.setCursor(0, 1);          // set the cursor to column 0, line 0

    lcd.print("distance: ");      // Print a message of "distance: "to the LCD.

    lcd.print(cm);                // Print a centigrade distance to the LCD. 

    lcd.print(" cm    ");         // Print the unit of the centigrade distance to the LCD.




int ping(int pingPin) 

   // establish variables for duration of the pin, 

   // and the distance result in inches and centimeters: 

   long duration, cm; 

   // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. 

   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: 

   pinMode(trigPin, OUTPUT); 

   digitalWrite(trigPin, LOW); 


   digitalWrite(trigPin, HIGH); 


   digitalWrite(trigPin, LOW); 

   pinMode(pingPin, INPUT); 

   duration = pulseIn(pingPin, HIGH); 

   // convert the time into a distance 

   cm = microsecondsToCentimeters(duration); 

   return cm ; 

long microsecondsToCentimeters(long microseconds) 

   // The speed of sound is 340 m/s or 29 microseconds per centimeter. 

   // The ping travels out and back, so to find the distance of the 

   // object we take half of the distance travelled. 

   return microseconds / 29 / 2; 

Compile the program and upload to Arduino UNO board Now, change the distance between the ultrasonic module and the obstacle, and you will find the distance value displayed on the LCD1602 changed

Add Comment