To connect an Arduino ultrasonic sensor (HC-SR04) to a 16x2 LCD display and show the measured distance, you'll need to use the LiquidCrystal library for the LCD and follow these steps:
Hardware Required:
- Arduino board (e.g., Uno, Nano)
- HC-SR04 ultrasonic sensor
- 16x2 LCD display (with I2C adapter or without I2C)
- Breadboard and jumper wires
Connections:
For LCD with I2C Adapter:
HC-SR04 to Arduino:
- VCC to Arduino 5V
- GND to Arduino GND
- TRIG to Arduino Digital Pin 9
- ECHO to Arduino Digital Pin 10
LCD (with I2C adapter) to Arduino:
- VCC to Arduino 5V
- GND to Arduino GND
- SDA to Arduino A4 (on an Uno) or SDA pin (on other boards)
- SCL to Arduino A5 (on an Uno) or SCL pin (on other boards)
For LCD without I2C Adapter:
- LCD to Arduino:
- VSS to Arduino GND
- VDD to Arduino 5V
- V0 to the middle pin of a potentiometer (for contrast adjustment)
- RS to Arduino Digital Pin 12
- RW to Arduino GND
- E to Arduino Digital Pin 11
- D0 to Arduino GND (usually not used)
- D1 to Arduino GND (usually not used)
- D2 to Arduino Digital Pin 7
- D3 to Arduino Digital Pin 6
- D4 to Arduino Digital Pin 5
- D5 to Arduino Digital Pin 4
- D6 to Arduino Digital Pin 3
- D7 to Arduino Digital Pin 2
- A to Arduino 5V (Backlight Anode)
- K to Arduino GND (Backlight Cathode)
Code:
Here is the code to interface the HC-SR04 with the 16x2 LCD:
For LCD with I2C Adapter:
cpp#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change address if needed, 0x27 is common for I2C LCDs
const int trigPin = 9;
const int echoPin = 10;
void setup() {
lcd.begin();
lcd.backlight(); // Turn on the backlight if available
lcd.setCursor(0, 0);
lcd.print("Distance: ");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration;
int distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear previous data
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print(" cm");
delay(500);
}
For LCD without I2C Adapter:
cpp#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); // Pin numbers for LCD without I2C
const int trigPin = 6;
const int echoPin = 5;
void setup() {
lcd.begin(16, 2);
lcd.print("Distance: ");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration;
int distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear previous data
lcd.setCursor(0, 1);
lcd.print(distance);
lcd.print(" cm");
delay(500);
}
Explanation:
Libraries and Initialization:
Wire.handLiquidCrystal_I2C.hare used for I2C communication and controlling the LCD.- For a standard LCD,
LiquidCrystal.his used. lcd.begin()initializes the LCD. For I2C, specify the address (e.g., 0x27) and dimensions (16x2).
Setup Function:
lcd.print("Distance: ");displays a static label on the LCD.pinMode(trigPin, OUTPUT);andpinMode(echoPin, INPUT);set up the pins.
Loop Function:
- Send a pulse via the TRIG pin and measure the echo duration using
pulseIn(). - Calculate distance in centimeters.
- Update the LCD display with the new distance. The previous data is cleared by printing spaces before the new distance value.
- Send a pulse via the TRIG pin and measure the echo duration using
.png)
