This ultrasonic distance alert system uses an HC-SR04 sensor to monitor how close objects are and provides real-time feedback through LEDs, a buzzer, and an LCD display. The Arduino Uno continuously measures distance and triggers different alert levels: red LED and loud buzzer for objects too close, yellow for approaching objects, and blue for safe distances.
Builders will receive a complete wiring diagram showing how to connect the distance sensor, three indicator LEDs with resistors, passive piezo buzzer, and I2C LCD display to the Arduino Uno. The guide includes step-by-step assembly instructions, the full firmware with distance thresholds and alert logic, and testing procedures to verify the system responds correctly at different ranges.
Wiring diagram

Gather all the parts
Assemble it in 6 steps
1. Prepare the circuit with power disconnected
Unplug USB while wiring. Use jumper wires and a breadboard to join the listed connections. Connect Uno 5V and GND to separate breadboard rails; bridge split rails if needed. All GND connections share the same ground.
- Never connect 5V directly to GND; a short can damage the board or USB port.
2. Connect the three indicator LEDs
D2 goes through rRed (220 ohms) to the red LED long leg. D3 goes through rYellow (220 ohms) to the yellow or green LED long leg. D4 goes through rBlue (220 ohms) to the blue or white LED long leg. Each short leg goes to GND.
- Each LED needs its own series resistor to prevent LED and board damage.
3. Connect the distance sensor
HC-SR04 VCC goes to 5V, GND to GND, TRIG to D7 (measurement trigger), and ECHO to D8 (return pulse). Follow the printed pin labels. Face the two round transducers toward the object.
4. Connect the passive piezo buzzer
Connect the piezo signal lead to D9 and its other lead to GND. Use a small passive piezo element, not a high-current magnetic buzzer.
5. Connect the four-row LCD
Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). This project uses a 20-column, 4-row LCD with an I2C backpack. Adjust its contrast screw gently if the text is not visible.
6. Inspect the wiring and test distances
Check LED polarity, resistor placement, and power connections. Test with a flat object at 8 cm, 13 cm, 17 cm, and 25 cm. These select blue with sound, yellow or green without sound, all off, and red without sound. Below the sensor's 2 cm minimum or without an echo, all outputs are off; this is a classroom demonstration, not a safety alarm.
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 "sonar" and "Arduino"
8. Connections between "buzzer" 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>
const byte RED = 2, YELLOW = 3, BLUE = 4;
const byte TRIG = 7, ECHO = 8, BUZZER = 9;
hd44780_I2Cexp lcd; // Automatically finds the LCD address.
bool lcdOK;
int lastState = -1;
long lastDistance = -1;
// Write a full row to erase leftover letters without flickering.
void row(byte number, const char *text) {
lcd.setCursor(0, number);
byte i = 0;
while (text[i] && i < 20) lcd.write(text[i++]);
while (i++ < 20) lcd.write(' ');
}
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);
lcdOK = (lcd.begin(20, 4) == 0);
if (lcdOK) row(0, "Distance Monitor");
else Serial.println("LCD error: check wiring and contrast");
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
unsigned long duration = pulseIn(ECHO, HIGH, 30000UL);
float distance = duration / 58.0;
bool valid = duration != 0 && distance >= 2 && distance <= 400;
// 0 = no reading, 1 = too close, 2 = closer, 3 = gap, 4 = far
int state;
if (!valid) state = 0;
else if (distance <= 10) state = 1;
else if (distance <= 15) state = 2;
else if (distance >= 20) state = 4;
else state = 3;
if (state != lastState) {
digitalWrite(RED, state == 4);
digitalWrite(YELLOW, state == 2);
digitalWrite(BLUE, state == 1);
if (state == 1) tone(BUZZER, 4000);
else noTone(BUZZER);
if (lcdOK) {
if (state == 1) {
row(2, "The OBJECT is");
row(3, "TOO CLOSE");
} else {
if (state == 2) row(2, "Object is Closer");
else if (state == 4) row(2, "The Distance is far");
else if (state == 3) row(2, "Between 15 and 20 cm");
else row(2, "No valid echo");
row(3, "");
}
}
}
long displayed = valid ? (long)(distance * 10 + 0.5) : -1;
if (lcdOK && displayed != lastDistance) {
char text[21];
if (valid) snprintf(text, sizeof(text), "Distance: %ld.%ld cm",
displayed / 10, displayed % 10);
else snprintf(text, sizeof(text), "Distance: ---");
row(1, text);
}
lastState = state;
lastDistance = displayed;
delay(100); // More than 60 ms between ultrasonic measurements.
}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.




