Introduction: Measuring Distance and Showing It on LCD Screen Using Arduino

In this project we will connect the LCD and the ultrasonic sensor with the Arduino in order to measure the distance and display it on the LCD Screen

Step 1: Hardware

  • Ultrasonic sensor
  • Arduino uno Microcontroller
  • LCD display
  • Jumper caple

Step 2: How Does It Work

The ultrasonic sensor will mesure the distance by sending a sound wave (Like a Bat) then registering when the sound echo is received again , then converting the time between triggering the sound and receiving the echo into distance ( in centimeter) . the Arduino will process the data and display it on the LCD Screen.

Step 3: Connection


  • Vcc to 5V in the arduino
  • trig to port 2 in the arduino
  • echo to port 4 in the arduino
  • Gnd to Gnd in the arduino

2- LCD

  • LCD RS pin to digital pin 12
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2

Additionally, wire a 10k pot to +5V and GND, with it's wiper (output) to LCD screens VO pin (pin3). A 220 ohm resistor is used to power the backlight of the display, usually on pin 15 and 16 of the LCD connector

you can follow the diagrams for both connection , LCD is a bit tricky since it have lots of wires

Step 4: Schematic

Step 5: The Code

just copy the code to Arduino IDE , upload it and you are done , the code is basicly a mix of Ultrasonic and Liquid Crystal Example code that comes with the IDE example library

// include the library code:


// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int trigPin = 7;

const int echoPin = 8;

void setup() {

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// Print a message to the LCD.

lcd.print("The distance is");


void loop() {

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(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

cm = microsecondsToCentimeters(duration);

// set the cursor to column 0, line 1

// (note: line 1 is the second row, since counting begins with 0):

lcd.setCursor(0, 1);

// print the number of seconds since reset:


lcd.setCursor(2, 1);




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;