Project 4: Distance Alert Display Using Ultrasonic Sensor & LCD

Step 1: Overview

This project demonstrates how to use an ESP32, an ultrasonic sensor (HC-SR04), a buzzer, and an I2C LCD to create a proximity alert system. The system continuously measures distance using the ultrasonic sensor, displays it on an LCD, and triggers a buzzer when an object comes too close.

Step 2: Components Required

  • ESP32 Development Board
  • HC-SR04 Ultrasonic Sensor
  • 16x2 LCD Display (I2C Module — PCF8574)
  • Buzzer
  • Breadboard & Jumper Wires

Step 3: Circuit Connections

Connecting the HC-SR04 Ultrasonic Sensor to ESP32

  • VCC → 5V of ESP32
  • GND → GND of ESP32
  • Trig Pin → GPIO 5
  • Echo Pin → GPIO 18
HC-SR04 Connection

Connecting the LCD to ESP32 (I2C Communication)

  • VCC → 5V of ESP32
  • GND → GND of ESP32
  • SDA → GPIO 21
  • SCL → GPIO 22

Connecting the Buzzer to ESP32

  • Buzzer Pin → GPIO 12
  • GND → GND of ESP32

Step 4: Breadboard Wiring Guide

  1. Place the ESP32 on the breadboard.
  2. Connect the HC-SR04 ultrasonic sensor to the ESP32 as per the connections above.
  3. Connect the LCD’s I2C pins (SDA, SCL) to ESP32’s GPIO 21 and 22.
  4. Connect the buzzer’s positive pin to GPIO 12 and its negative pin to GND.
  5. Double-check all connections before powering up the ESP32.

Step 5: Upload the ESP32 Code

Copy and paste the following code into the Arduino IDE and upload it to your ESP32.

#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>

// Ultrasonic sensor pins
const int trigPin = 5;
const int echoPin = 18;

// Buzzer pin
const int buzzerPin = 12;

// Define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701

long duration;
float distanceCm;
float distanceInch;

// Initialize the LCD (I2C address 0x27)
LiquidCrystal_PCF8574 lcd(0x27);

void setup() {
Serial.begin(115200);

// Initialize LCD
lcd.begin(16, 2);
lcd.setBacklight(255);
lcd.setCursor(0, 0);
lcd.print("Distance Sensor");
delay(2000);
lcd.clear();

// Initialize pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}

void playAlarm(){
int melody[] = {1000, 1200, 1500, 1200}; // Frequency pattern
int duration[] = {200, 200, 300, 200}; // Duration pattern

for (int i = 0; i < 4; i++) {
tone(buzzerPin, melody[i]);
delay(duration[i]);
noTone(buzzerPin);
delay(100);
}
}

void loop() {
// Trigger ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read echo time
duration = pulseIn(echoPin, HIGH);

// Convert time to distance
distanceCm = duration * SOUND_SPEED / 2;
distanceInch = distanceCm * CM_TO_INCH;

// Display distance on LCD
lcd.setCursor(0, 0);
lcd.print("Distance (cm): ");
lcd.setCursor(0, 1);
lcd.print(distanceCm);
lcd.print(" "); // Clear extra characters

// Buzzer alert if object is too close
if (distanceCm < 10.0) { // Threshold: 10 cm
// digitalWrite(buzzerPin, HIGH);
playAlarm();
lcd.setCursor(10, 1);
lcd.print("ALERT!");
} else {
// digitalWrite(buzzerPin, LOW);
lcd.setCursor(10, 1);
lcd.print(" "); // Clear alert message
}

// Print distance to Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);

delay(500);
}

Step 6: How It Works

  1. The ultrasonic sensor measures distance by sending a sound pulse and calculating the time taken for the echo to return.
  2. The ESP32 calculates the distance and displays it on the LCD screen.
  3. If an object comes closer than 10 cm, the buzzer turns on, and an “ALERT!” message appears on the LCD.
  4. If the object moves away, the buzzer turns off, and the LCD clears the alert message.
  5. The distance readings are also printed on the Serial Monitor for debugging.

Step 7: Testing and Enhancements

Testing:

  • Move your hand towards and away from the sensor and check the LCD and buzzer response.
  • Open the Serial Monitor (115200 baud rate) in the Arduino IDE to see real-time distance readings.

Possible Enhancements:

  • Modify the alert threshold (e.g., change if (distanceCm < 10.0) to another value).
  • Use a relay to trigger a higher-power alarm or LED when an object is too close.
  • Integrate an IoT feature to send alerts via WiFi (Blynk, Telegram, or MQTT).

Step 8: Conclusion

This ESP32-based Proximity Alert System provides real-time distance monitoring with an ultrasonic sensor and a visual & audio alert mechanism. It can be applied in object detection, parking sensors, security systems, and more.

Back to blog

Leave a comment