How to display distance measurements on LCD display with ultrasonic sensor on Arduino!
Being a kid, I always used to do things like twiddling with motors, make simple circuits with switches, etc. Once I grew up I came to understand about Arduino microcontrollers which enable us to make complex circuits with various elements like LED, buzzer, etc.
With Arduino, several prototypes are often made on both an outsized scale and a little scale. For starters, I might recommend buying an Arduino UNO starter kit which mostly contains all the wants for this project. Anything in beginning is going to be hard and difficult. Likewise even using Arduino, the connections and code might not be easy within the beginning. So, I feel that the project below may be a great start-up for beginners to find out and understand the connections and therefore the coding that goes to the Arduino.
The materials required for this project are Arduino Uno, breadboard, jumper wires, an ultrasonic sensor(HC-SR04), a potentiometer, and an LCD display.
Coming to the code part, it’s simple. First, I even have imported the Liquid Crystal which is required for using the LCD display. Then all the variables are initialized. Then I have defined the 2 main methods void setup and void loop.
Here, we use an ultrasonic sensor(HC-SR04) to measure the distance. We can connect the jumper wires to the Arduino board using the circuit diagram below.
Coming to the connections part, one connection is made from the 5V on the board to the positive rail, and one connection from the ground on the board to the negative rail. We use a potentiometer in this circuit in order to adjust the contrast of the LCD display.
After doing all the connections properly as per the circuit diagram, you should connect the Arduino board to the PC, and using the Arduino IDE software you can upload the code given below. If this is often your first time using Arduino IDE software, do choose the proper model of your Arduino and choose the proper port under appropriate options within the IDE. Finally, you would be able to read the space measurements on the LCD display. For beginners again, you shall not need to be connected to the laptop after uploading the code to the board, you’ll just use an influence source sort of a power bank.
Overview – In this project, we will be using an ultrasonic sensor(HC-SR04) to measure distance and output it on the LCD display.
Circuit
Circuit Diagram
Note
You should have LiquidCrystal library added to your libraries in order to upload the code. Also check whether all pins are connected in proper positions.
Code
#include <LiquidCrystal.h> LiquidCrystal lcd(1, 2, 4, 5, 6, 7); const int trigPin = 9; const int echoPin = 10; long duration; int distanceCm, distanceInch; void setup() { lcd.begin(16, 2); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distanceCm = duration * 0.034 / 2; distanceInch = duration * 0.0133 / 2; lcd.setCursor(0, 0); lcd.print("Distance: "); lcd.print(distanceCm); lcd.print(" cm"); delay(10); lcd.setCursor(0, 1); lcd.print("Distance: "); lcd.print(distanceInch); lcd.print("inch"); delay(10); }