Community project

ESP32 Circuit Simulation

Photo of ESP32 Circuit Simulation
Generated with AI

Gilbert Pelagio

Published November 23, 2026

This project builds a distance monitoring system using an HC-SR04 ultrasonic sensor to detect nearby objects and provide real-time feedback through LEDs, a piezo buzzer, and an LCD display. The system continuously measures distance and triggers visual and audio alerts based on proximity thresholds, making it useful for obstacle detection, parking assistance, or proximity-based automation experiments.

The guide includes a complete wiring diagram showing connections for three indicator LEDs, the ultrasonic distance sensor, piezo buzzer, and I2C LCD display module on an Arduino Uno. Builders will receive a detailed parts list, step-by-step assembly instructions with safety guidelines, and fully commented Arduino firmware that handles sensor reading, display updates, and alert triggering.

Wiring diagram

Wiring diagram for ESP32 Circuit Simulation
Loading diagram…

Gather all the parts

QtyComponent
1

LED

Red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

LED

Yellow

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

LED

Blue

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

220 Ω

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

220 Ω

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

220 Ω

Through-hole resistor (current-limiting in series with an LED)

1

HC-SR04

HC-SR04 ultrasonic distance sensor module with separate 40 kHz transmitter and receiver transducers, measuring 2 to 400 cm. A 10 us pulse on TRIG starts a measurement and ECHO stays high for the echo's round-trip time. This HC-SR04+ board runs from 3.3 V or 5 V.

1

Piezo Buzzer

Small passive piezo buzzer (TDK PS1240) for beeps, tones and alerts. It has no built-in oscillator, so drive one lead with a 3-30 V square wave from a microcontroller pin using tone() or PWM and connect the other lead to ground. It is loudest around 4 kHz and works from about 2 to 10 kHz; driving both leads in antiphase roughly doubles the volume.

1

I2C 20x4 Arduino LCD Display Module

20x4 character LCD with I2C interface, white text on blue backlight, adjustable contrast. Operates at 5V or 3.3V; reduces pin usage to 2 lines (SDA/SCL).

Assemble it in 6 steps

1. Disconnect power first

Unplug the Arduino USB cable before inserting or moving wires. Use USB power for this circuit.

2. Connect the three LEDs

Connect D2 through rRed to the red LED's long positive leg. Connect D3 through rYellow to the yellow LED's long leg. Connect D4 through rBlue to the blue LED's long leg. Connect all short negative legs to Arduino GND. Each resistor is 220 ohms.

  • Never connect an LED without its series resistor; excessive current can damage the LED or Arduino.

3. Connect the distance sensor

Connect HC-SR04 VCC to 5V, GND to GND, TRIG to D6 (trigger pulse), and ECHO to D7 (return pulse). Read the printed pin labels rather than guessing their order.

4. Connect the piezo buzzer

Connect one passive piezo buzzer lead to D9 and the other lead to GND. If marked, use the plus lead for D9.

  • Use a small passive piezo
  • not a high-current magnetic buzzer or loudspeaker.

5. Connect the LCD

Connect the 20-column 4-row I2C LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). The firmware automatically detects supported I2C backpack addresses. Adjust the small contrast screw gently if text is not visible.

  • All GND connections share Arduino GND. Keep power polarity correct.

6. Test object distances

After powering the finished circuit, point the sensor at a broad flat object. Try 25 cm for red, 12 cm for yellow, and 8 cm for blue with sound. Between 15 and 20 cm all outputs are off. No valid echo also turns all outputs off; readings below 2 cm are unreliable.

Review all connections

1. Connections between "rRed" and "Arduino"

FunctionrRedArduino
digitalP1GPIO 2
digitalP2 → LED ANODEEXT

2. Connections between "red" and "Arduino"

FunctionredArduino
groundGNDGND

3. Connections between "rYellow" and "Arduino"

FunctionrYellowArduino
digitalP1GPIO 3
digitalP2 → LED ANODEEXT

4. Connections between "yellow" and "Arduino"

FunctionyellowArduino
groundGNDGND

5. Connections between "rBlue" and "Arduino"

FunctionrBlueArduino
digitalP1GPIO 4
digitalP2 → LED ANODEEXT

6. Connections between "blue" and "Arduino"

FunctionblueArduino
groundGNDGND

7. Connections between "buzzer" and "Arduino"

FunctionbuzzerArduino
pwmSIGGPIO 9
groundGNDGND

8. Connections between "sonar" and "Arduino"

FunctionsonarArduino
powerVCC5V
groundGNDGND
digitalTRIGGPIO 6
digitalECHOGPIO 7

9. Connections between "lcd" and "Arduino"

FunctionlcdArduino
powerVCC5V
groundGNDGND
i2cSDAGPIO 18
i2cSCLGPIO 19

Deploy the firmware

#include <Arduino.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

// Automatically finds the LCD backpack address and wiring.
hd44780_I2Cexp lcd;
const byte RED = 2, YELLOW = 3, BLUE = 4;
const byte TRIG = 6, ECHO = 7, BUZZER = 9;
bool lcdReady = false;
char previous[4][21] = {};

// Update only rows whose contents changed. Pad to erase old text.
void showRow(byte row, const char *text) {
  char padded[21];
  memset(padded, ' ', 20);
  padded[20] = '\0';
  memcpy(padded, text, min(strlen(text), (size_t)20));
  if (lcdReady && strcmp(previous[row], padded) != 0) {
    lcd.setCursor(0, row);
    lcd.print(padded);
    strcpy(previous[row], padded);
  }
}

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  digitalWrite(TRIG, LOW);
  Serial.begin(9600);
  lcdReady = (lcd.begin(20, 4) == 0);
  if (lcdReady) lcd.backlight();
  else Serial.println("LCD not found: check wiring and contrast.");
  showRow(0, "Distance Monitor");
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long duration = pulseIn(ECHO, HIGH, 30000UL);
  float cm = duration / 58.0;
  bool valid = duration != 0 && cm >= 2.0 && cm <= 400.0;

  digitalWrite(RED, LOW);
  digitalWrite(YELLOW, LOW);
  digitalWrite(BLUE, LOW);
  noTone(BUZZER);
  const char *line2 = "", *line3 = "";
  if (!valid) {
    line2 = "No valid echo";
    line3 = "Check object/sensor";
  } else if (cm >= 20.0) {
    digitalWrite(RED, HIGH);
    line2 = "The Distance is far";
  } else if (cm <= 10.0) {
    digitalWrite(BLUE, HIGH);
    tone(BUZZER, 4000);
    // This message needs two rows on a 20-column screen.
    line2 = "The OBJECT is";
    line3 = "TOO CLOSE";
  } else if (cm <= 15.0) {
    digitalWrite(YELLOW, HIGH);
    line2 = "Object is Closer";
  } else {
    line2 = "Between 15 and 20 cm";
  }
  char distanceText[21];
  if (valid) {
    char number[10];
    dtostrf(cm, 5, 1, number);
    snprintf(distanceText, sizeof(distanceText), "Distance: %s cm", number);
  } else strcpy(distanceText, "Distance: -- cm");
  showRow(1, distanceText);
  showRow(2, line2);
  showRow(3, line3);
  Serial.println(distanceText);
  delay(100); // HC-SR04 needs at least 60 ms between triggers.
}

Remix this project

Make it yours in one click

Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.

Open in Schematik