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

Gather all the parts
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"
2. Connections between "red" and "Arduino"
3. Connections between "rYellow" and "Arduino"
4. Connections between "yellow" and "Arduino"
5. Connections between "rBlue" and "Arduino"
6. Connections between "blue" and "Arduino"
7. Connections between "buzzer" and "Arduino"
8. Connections between "sonar" and "Arduino"
9. Connections between "lcd" and "Arduino"
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.




