Complete Guide: ESP32 Ultrasonic Distance Meter with LCD Display - Wokwi Simulator Tutorial

  

This circuit combines an ESP32 microcontroller, HC-SR04 ultrasonic sensor, and 16x2 LCD display to create an intelligent distance measurement system. Perfect for parking sensors, obstacle detection, and automation projects.


Components Required

Diagram.json:
{
  "version": 1,
  "author": "AkhileshwarReddy velijarla",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-esp32-devkit-v1", "id": "esp", "top": 0, "left": 0, "attrs": {} },
    { "type": "wokwi-hc-sr04", "id": "ultrasonic1", "top": 12.87, "left": -228.6, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd1", "top": 117.19, "left": -127.89, "attrs": {} },
    { "type": "wokwi-gnd", "id": "gnd2", "top": 128.95, "left": 197.42, "attrs": {} },
    { "type": "wokwi-vcc", "id": "vcc1", "top": 115.61, "left": -226.85, "attrs": {} },
    { "type": "wokwi-vcc", "id": "vcc2", "top": 11.36, "left": 186.26, "attrs": {} },
    {
      "type": "wokwi-lcd1602",
      "id": "lcd1",
      "top": 29.34,
      "left": 232.42,
      "attrs": { "pins": "i2c" }
    }
  ],
  "connections": [
    [ "esp:TX0", "$serialMonitor:RX", "", [] ],
    [ "esp:RX0", "$serialMonitor:TX", "", [] ],
    [ "vcc1:VCC", "ultrasonic1:VCC", "red", [ "v8.82", "h59.56" ] ],
    [ "ultrasonic1:GND", "gnd1:GND", "black", [ "v0" ] ],
    [ "lcd1:GND", "gnd2:GND", "black", [ "h-31.96", "v1.78" ] ],
    [ "vcc2:VCC", "lcd1:VCC", "red", [ "v0" ] ],
    [ "lcd1:SDA", "esp:D21", "green", [ "h-104.08", "v-18.01" ] ],
    [ "esp:D22", "lcd1:SCL", "blue", [ "h73.37", "v52.63" ] ],
    [ "ultrasonic1:TRIG", "esp:D32", "green", [ "v56.87", "h122.87", "v-93.28" ] ],
    [ "esp:D33", "ultrasonic1:ECHO", "yellow", [ "h-47.46", "v98.21", "h-91.71" ] ]
  ],
  "dependencies": {}
}
  • ESP32 Development Board - Main microcontroller
  • HC-SR04 Ultrasonic Sensor - Distance measurement (2cm - 400cm range)
  • 16x2 LCD Display (I2C) - Visual output display
  • Connecting Wires - For circuit connections
  • Power Supply - 5V via USB or external source

Circuit Connections: Step-by-Step Wiring Guide

1. HC-SR04 Ultrasonic Sensor Connections

HC-SR04 PinESP32 PinWire ColorFunction
VCC5V/3.3VRedPower supply
GNDGNDBlackGround
TRIGGPIO 5YellowTrigger signal
ECHOGPIO 18GreenEcho signal

Wiring Steps:

  • Connect VCC to ESP32's VCC (5V or 3.3V depending on sensor)
  • Connect GND to ESP32's ground
  • Connect TRIG pin to GPIO 5 (yellow wire)
  • Connect ECHO pin to GPIO 18 (green wire)

2. 16x2 LCD Display Connections (I2C Interface)

LCD PinESP32 PinWire ColorFunction
VCC5VRedPower supply
GNDGNDBlackGround
SDAGPIO 21BlueI2C Data line
SCLGPIO 22GreenI2C Clock line

Wiring Steps:

  • Connect LCD VCC to ESP32's 5V
  • Connect LCD GND to ESP32's ground
  • Connect SDA to GPIO 21 (default I2C data pin)
  • Connect SCL to GPIO 22 (default I2C clock pin)

How the Circuit Works

Operating Principle:

  1. Ultrasonic Pulse Transmission: ESP32 sends a trigger signal through GPIO 5 to HC-SR04
  2. Sound Wave Emission: Sensor emits 40kHz ultrasonic waves
  3. Echo Reception: Reflected waves are received by the sensor
  4. Distance Calculation: ESP32 measures time delay and calculates distance using:
    • Distance = (Time × Speed of Sound) / 2
    • Speed of sound ≈ 343 m/s
  5. Display Output: Calculated distance shown on LCD in real-time

Setting Up in Wokwi Simulator

Step-by-Step Wokwi Tutorial:

  1. Create New Project: Go to wokwi.com → Start new ESP32 project
  2. Add Components:
    • Search and add "ESP32"
    • Add "HC-SR04" ultrasonic sensor
    • Add "LCD 16x2 (I2C)" display
  3. Wire Connections: Click and drag to connect pins as per diagram
  4. Upload Code: Paste your Arduino code in the code editor
  5. Configure LCD I2C Address: Set to 0x27 (common default)
  6. Run Simulation: Click green play button to start
  7. Test Functionality: Use Wokwi's virtual object to test distance readings

Understanding the Boot Log

Code:

#include <LiquidCrystal_I2C.h>


int lcdColumns = 16;
int lcdRows = 2;

 int trigPin = 32;
 int echoPin = 33;
float duration;
int distance;

LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  

void setup(){
  pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

  lcd.init();
                     
  lcd.backlight();
}

void loop(){


digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= (duration*0.034)/2;
lcd.setCursor(0,0);
lcd.print("Distance :");
lcd.setCursor(1,3);
lcd.print(distance);
delay(2000);
lcd.clear();

}

The console output shows:

  • Clock drivers: System initialization
  • DIO mode: Flash memory communication mode
  • Load addresses: Program memory sections
  • Entry point: Code execution start (0x40080dc)

This confirms successful ESP32 bootloader operation.




Applications and Use Cases

  • Parking Assistance Systems
  • Robot Obstacle Avoidance
  • Water Level Monitoring
  • Automatic Door Openers
  • Industrial Automation
  • Smart Home Projects
  • Security Systems

Comments