Ultrasonic Sensor with LCD – Arduino Tutorial

6,187 views

Introduction

The HC-SR04 ultrasonic sensor distance measurement system utilizing an Arduino UNO microcontroller is a versatile and efficient solution for determining the distance of an object in front of it. The ultrasonic sensor sends out a sound wave and measures the time it takes for the sound wave to bounce back, allowing the system to calculate the distance of the object. The Arduino UNO microcontroller is then used to process this distance information and display it on a convenient 16×2 LCD module. This system can be used in a wide variety of applications such as obstacle detection, automated door control, level detection, parking assistance, etc.

A 16×2 Arduino LCD module is a type of liquid crystal display (LCD) that is commonly used to display text and characters. The name “16×2” refers to the number of rows and columns on the display, meaning it has 16 columns (characters per line) and 2 rows. It can be controlled by an Arduino microcontroller, which sends commands and data to the LCD to control what is displayed on the screen. The module typically includes a controller that interprets the commands from the Arduino and controls the LCD’s display.

Hardware Components

You will require the following hardware for Ultrasonic Sensor & LCD with Arduino.

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.LEDI2C1
4.Ultrasonic Sensor1
5.Power Adapter for Arduino9V1
6.Jumper Wires1

Ultrasonic Sensor LCD with Arduino

  1. Connect the HC-SR04 ultrasonic sensor to the Arduino Uno as follows:
  • VCC pin to 5V
  • Trig pin to digital pin 2
  • Echo pin to digital pin 3
  • GND pin to GND
  1. Connect the 16×2 LCD module to the Arduino Uno as follows:
  • RS pin to digital pin 7
  • EN pin to digital pin 6
  • D4 pin to digital pin 5
  • D5 pin to digital pin 4
  • D6 pin to digital pin 3
  • D7 pin to digital pin 2
  • VSS pin to GND
  • VDD pin to 5V
  • V0 (contrast) pin to a potentiometer
  1. In the Arduino IDE, include the following libraries at the beginning of the code:
#include <Ultrasonic.h>
#include <LiquidCrystal.h>
  1. Define the pin numbers for the ultrasonic sensor and the LCD module in the setup function:
Ultrasonic ultrasonic(2, 3);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
  1. In the setup function, use the begin() function to initialize the LCD module with the number of columns and rows, and the print() function to display a message on the LCD.
void setup() {
  lcd.begin(16, 2);
  lcd.print("Distance: cm");
}
  1. In the loop function, use the distanceRead() function to get the distance in centimeters from the ultrasonic sensor, and store it in a variable. Then, use the setCursor() function to set the cursor position on the LCD and the print() function to display the distance on the second row of the LCD.
void loop() {
  int distance = ultrasonic.distanceRead();
  lcd.setCursor(0, 1);
  lcd.print(distance);
}
  1. Finally, upload the code to your Arduino Uno.

Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

ArduinoUltrasonic SensorLCD
IOREFVCC
GNDGNDGND
D9TRIG
D8ECHO
5VVCC
A4SDA
A5SCL

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Progam Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2); // I2C address 0x3F, 16 column and 2 rows

int trigPin = 9;    // TRIG pin
int echoPin = 8;    // ECHO pin

float duration_us, distance_cm;

void setup() {
  lcd.init();               // initialize the lcd
  lcd.backlight();          // open the backlight 
  pinMode(trigPin, OUTPUT); // config trigger pin to output mode
  pinMode(echoPin, INPUT);  // config echo pin to input mode
}

void loop() {
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(echoPin, HIGH);

  // calculate the distance
  distance_cm = 0.017 * duration_us;

  lcd.clear();
  lcd.setCursor(0, 0); // start to print at the first row
  lcd.print("Distance: ");
  lcd.print(distance_cm);

  delay(500);
}

Working Explanation

In the setup function, the Arduino’s serial communication is initialized with the LiquidCrystal() function and the ultrasonic sensor and LCD module pins are defined. The ultrasonic sensor is initialized by creating an object of the Ultrasonic class and passing the trigger and echo pin numbers. The LCD module is initialized by creating an object of the LiquidCrystal class and passing the pin numbers for RS, EN, D4, D5, D6, and D7.

In the loop function, the distance from the ultrasonic sensor is read using the distanceRead() function from the Ultrasonic library and stored in a variable. Then, the begin() function is used to initialize the LCD with the number of columns and rows, and the print() function is used to display the message “Distance: ” on the first row of the LCD. The setCursor() function is used to set the cursor position on the LCD and the print() function is used to display the distance on the second row of the LCD.

Applications

  • Robotics: the system can be used in robotics for obstacle detection and avoidance.
  • Security: the system can be used in security systems to detect the distance of an object and trigger an alarm if the distance is too close.
  • Industrial: the system can be used in industrial applications to measure the level of a liquid in a tank or the distance of an object on a production line.
  • Research: the system can be used in research to measure the distance of objects in experiments.

Conclusion.

We hope you have found this Arduino Ultrasonic Sensor LCD Circuit very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.