This project builds a proximity alert system that uses an ultrasonic distance sensor to monitor how close objects are and provides visual and audio feedback. The HC-SR04 sensor measures distance in real time, triggering different colored LEDs and a piezo buzzer based on proximity zones—red for far, yellow for closer, and blue for dangerously close with an active alarm.
The guide includes a complete wiring diagram, parts list, and Arduino firmware that displays live distance readings on a 20x4 LCD screen while managing the alert states. Assembly takes about 30 minutes and requires only basic soldering skills. This system works well for parking assistance, collision avoidance prototypes, or any application needing distance-based triggering.
Wiring diagram

Gather all the parts
Assemble it in 5 steps
1. Disconnect power first
Unplug USB before connecting wires. Use jumper wires and a solderless breadboard if available. This project uses USB power only.
2. Connect the three lights
Connect D2 through a 220 ohm resistor to the red LED long leg. Connect D3 through another 220 ohm resistor to the yellow or green LED long leg. Connect D4 through the third resistor to the blue or white LED long leg. Connect each short leg (flat side) to GND.
- Never connect an LED directly to an Arduino output without its resistor.
3. Connect the distance sensor
Connect HC-SR04 VCC to 5V, GND to GND, TRIG to D7 (trigger signal), and ECHO to D8 (distance timing signal). Follow the printed pin labels.
- Aim the sensor at a flat object between 2 and 400 cm away. Readings below 2 cm are not reliable.
4. Connect the passive piezo buzzer
Connect one piezo lead to D9 (tone signal) and the other to GND. If marked plus, connect that lead to D9. Use a small passive piezo, not a high-current speaker or magnetic buzzer.
5. Connect the four-line display
Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). All ground connections must join the Uno GND.
- If the backlight comes on but letters are invisible
- gently turn the contrast screw on the LCD backpack.
- Check VCC and GND before powering up; reversed power can damage the modules.
Review all connections
1. Connections between "red_r" and "Arduino"
2. Connections between "red" and "Arduino"
3. Connections between "yellow_r" and "Arduino"
4. Connections between "yellow" and "Arduino"
5. Connections between "blue_r" 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 previousState = -1, previousDistance = -2;
void line(byte row, const char *text) {
lcd.setCursor(0, row);
byte n = 0;
while (*text && n < 20) { lcd.write(*text++); ++n; }
while (n++ < 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) Serial.println(F("LCD initialization failed: 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 cm = duration / 58.0f;
bool valid = duration != 0 && cm >= 2 && cm <= 400;
// 0: no reading, 1: too close, 2: closer, 3: neutral, 4: far.
int state = !valid ? 0 : cm <= 10 ? 1 : cm <= 15 ? 2 : cm >= 20 ? 4 : 3;
int displayedDistance = valid ? (int)(cm + 0.5f) : -1;
if (state != previousState) {
digitalWrite(RED, state == 4);
digitalWrite(YELLOW, state == 2);
digitalWrite(BLUE, state == 1);
if (state == 1) tone(BUZZER, 4000);
else noTone(BUZZER);
if (lcdOK) {
line(1, state == 1 ? "The OBJECT is" : state == 2 ? "Object is Closer" :
state == 4 ? "The Distance is far" : state == 3 ? "Object is nearby" : "No valid echo");
line(2, state == 1 ? "TOO CLOSE" : "");
line(3, state == 0 ? "Check sensor/target" : "");
}
}
if (displayedDistance != previousDistance || state != previousState) {
if (lcdOK) {
char text[21];
if (valid) snprintf(text, sizeof(text), "Distance: %d cm", displayedDistance);
else snprintf(text, sizeof(text), "Distance: -- cm");
line(0, text);
}
Serial.print(F("Distance (cm): "));
if (valid) Serial.println(cm, 1); else Serial.println(F("No valid echo"));
}
previousState = state; previousDistance = displayedDistance;
delay(100); // More than 60 ms between sensor 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.




