This ultrasonic distance alert system uses an HC-SR04 sensor to continuously monitor the distance to nearby objects and provides real-time visual and audio feedback. The Arduino Uno processes distance readings and controls three color-coded LEDs (red for far, yellow for approaching, blue for too close) along with a piezo buzzer that sounds when an object gets dangerously near.
Builders will receive a complete wiring diagram showing how to connect the distance sensor, LEDs with current-limiting resistors, passive buzzer, and I2C LCD display to the Arduino Uno. The guide includes the full firmware with distance thresholds, assembly steps starting from a disconnected state, and a parts list. The 20x4 LCD displays real-time distance measurements and system status, making this project ideal for learning sensor integration and multi-output control.
Wiring diagram

Gather all the parts
Assemble it in 5 steps
1. Disconnect USB before wiring
Use jumper wires to make the connections below. All GND connections share the Arduino GND. Power this circuit from USB.
2. Connect the three LEDs through resistors
Connect D2 through r_red (220 ohms) to the red LED long leg; D3 through r_yellow to the yellow or green LED long leg; D4 through r_blue to the blue or white LED long leg. Connect every short leg (flat side) to GND.
- Never connect an LED directly to an Arduino output without its series resistor.
3. Connect the distance sensor
Connect HC-SR04 VCC to 5V, GND to GND, TRIG to D7 (trigger output), and ECHO to D8 (distance pulse input). Read the pin labels rather than assuming their order.
4. Connect the passive piezo
Connect the piezo signal lead to D9 and its other lead to GND. This design uses a small passive piezo, not a high-current or magnetic buzzer.
5. Connect the 20-column four-row LCD
Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). The code automatically detects the I2C backpack address. Adjust the small contrast screw if text is not visible.
- Swapping VCC and GND can damage the display.
Review all connections
1. Connections between "r_red" and "Arduino"
2. Connections between "red" and "Arduino"
3. Connections between "r_yellow" and "Arduino"
4. Connections between "yellow" and "Arduino"
5. Connections between "r_blue" 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>
hd44780_I2Cexp lcd;
const byte RED = 2, YELLOW = 3, BLUE = 4;
const byte TRIG = 7, ECHO = 8, BUZZER = 9;
bool lcdOK;
int previousDistance = -2, previousState = -2;
void line(byte row, const char *text) {
lcd.setCursor(0, row);
lcd.print(text);
for (byte n = strlen(text); n < 20; n++) lcd.print(' ');
}
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) Serial.println(F("LCD not found: check wiring"));
}
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;
// State: 0=no reading, 1=far, 2=too close, 3=closer, 4=gap.
int state = 0;
if (valid) {
if (distance >= 20) state = 1;
else if (distance <= 10) state = 2;
else if (distance <= 15) state = 3;
else state = 4;
}
digitalWrite(RED, state == 1);
digitalWrite(BLUE, state == 2);
digitalWrite(YELLOW, state == 3);
if (state != previousState) {
if (state == 2) tone(BUZZER, 4000);
else noTone(BUZZER);
}
int shownDistance = valid ? (int)(distance + 0.5) : -1;
if (lcdOK && (shownDistance != previousDistance || state != previousState)) {
char text[21];
if (valid) snprintf(text, sizeof(text), "Distance: %d cm", shownDistance);
else strcpy(text, "Distance: no echo");
line(0, text);
line(1, state == 1 ? "The Distance is far" :
state == 2 ? "The OBJECT is" :
state == 3 ? "Object is Closer" :
state == 4 ? "Between 15 and 20 cm" : "No valid reading");
line(2, state == 2 ? "TOO CLOSE" : "");
line(3, state == 2 ? "Buzzer: ON" : "Buzzer: OFF");
}
previousDistance = shownDistance;
previousState = state;
delay(100);
}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.




