Measuring Distance and Showing It on LCD Screen Using Arduino

by FABLABJubail in Circuits > Arduino

2020 Views, 14 Favorites, 0 Comments

Measuring Distance and Showing It on LCD Screen Using Arduino

image004.jpg

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

Hardware

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

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.

Connection

image001.jpg
image002.jpg

1-Ultrasonic

  • 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

Schematic

image003.jpg

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:

#include

// 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);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

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.print(cm);

lcd.setCursor(2, 1);

lcd.print("cm");

delay(10);

}

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;

}