This project builds a proximity alert system that monitors distance using an ultrasonic sensor and provides real-time feedback through an LCD display, colored LEDs, and a buzzer. As objects approach, the system transitions through distance zones, triggering different visual and audio alerts to warn when something gets too close.
The guide includes a complete wiring diagram, parts list, and Arduino firmware that reads the HC-SR04 sensor, calculates distance in centimeters, and drives the alert indicators based on configurable distance thresholds. Assembly takes about 30 minutes and requires only basic soldering skills.
Wiring diagram

Gather all the parts
Assemble it in 6 steps
1. Disconnect power first
Unplug USB before connecting wires. Use USB power for this project. All GND connections go to the Uno GND.
2. Connect the distance sensor
HC-SR04 VCC to 5V and GND to GND. TRIG to D9 sends the measurement pulse; ECHO to D10 returns the travel time.
3. Connect the screen
LCD VCC to 5V, GND to GND, SDA to A4 (data), SCL to A5 (clock). This project uses a generic 20x4 PCF8574 backpack at address 0x3F, matching your sketch, not the DFR0154 module.
- If the backlight is on but text is invisible, gently turn the small contrast screw on the backpack. Some backpacks use 0x27 instead; change the address in the code if needed.
4. Wire the three LEDs with resistors
D11 to rred, then red LED long leg. D12 to rgreen, then green LED long leg. D13 to rblue, then blue LED long leg. Each resistor is 220 ohms. Connect each LED short leg (flat edge) to GND. Resistors can face either way.
- Never connect an LED directly to a pin without its series resistor; this can damage the LED or Uno.
5. Connect the passive piezo
Connect the piezo signal lead to D8 and its other lead to GND. The code generates a 4 kHz tone, rather than a steady voltage. Use a small passive piezo, not a magnetic buzzer or speaker.
6. Check the distance zones
After powering the finished circuit, move a flat object in front of the sensor: 20 cm or more lights red; 10 cm or less lights blue and sounds the piezo; 11 to 15 cm lights green. At 16 to 19 cm all LEDs and the piezo stay off. A missing or out-of-range echo shows an error and turns all indicators off. The sensor's useful range starts around 2 cm, so do not rely on it for safety-critical detection.
Review all connections
1. Connections between "sensor" and "Arduino"
2. Connections between "lcd" and "Arduino"
3. Connections between "rred" and "Arduino"
4. Connections between "red" and "Arduino"
5. Connections between "rgreen" and "Arduino"
6. Connections between "green" and "Arduino"
7. Connections between "rblue" and "Arduino"
8. Connections between "blue" and "Arduino"
9. Connections between "alarm" and "Arduino"
Deploy the firmware
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int trigPin = 9;
const int echoPin = 10;
const int redLed = 11;
const int greenLed = 12;
const int blueLed = 13;
const int buzzer = 8;
// Common backpack addresses: 0x3F and 0x27. Match your module.
LiquidCrystal_I2C lcd(0x3F, 20, 4);
long duration;
int distance;
int lastDistance = -999;
int lastState = -1;
// Pad each row so old letters never remain on screen.
void printRow(uint8_t row, const char *text) {
lcd.setCursor(0, row);
uint8_t i = 0;
while (text[i] && i < 20) lcd.print(text[i++]);
while (i++ < 20) lcd.print(' ');
}
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(trigPin, LOW);
Serial.begin(9600);
Serial.println("System Ready");
lcd.init();
lcd.backlight();
printRow(0, "Distance Alert");
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH, 30000UL);
distance = duration / 58;
// A missing echo is not a zero-centimetre object.
bool valid = duration > 0 && distance >= 2 && distance <= 400;
int state;
if (!valid) state = 0;
else if (distance >= 20) state = 1;
else if (distance <= 10) state = 2;
else if (distance <= 15) state = 3;
else state = 4; // 16 through 19 cm: all indicators off.
if (state != lastState) {
digitalWrite(redLed, state == 1 ? HIGH : LOW);
digitalWrite(blueLed, state == 2 ? HIGH : LOW);
digitalWrite(greenLed, state == 3 ? HIGH : LOW);
if (state == 2) tone(buzzer, 4000);
else noTone(buzzer);
printRow(3, "");
switch (state) {
case 0: printRow(1, "No valid echo"); printRow(2, "Check object/sensor"); break;
case 1: printRow(1, "The Distance is far"); printRow(2, ""); break;
case 2: printRow(1, "The OBJECT is"); printRow(2, "TOO CLOSE"); break;
case 3: printRow(1, "Object is Closer"); printRow(2, ""); break;
default: printRow(1, "Safe Zone"); printRow(2, ""); break;
}
}
int shownDistance = valid ? distance : -1;
if (shownDistance != lastDistance) {
char text[21];
if (valid) snprintf(text, sizeof(text), "Distance: %d cm", distance);
else snprintf(text, sizeof(text), "Distance: -- cm");
printRow(0, text);
lastDistance = shownDistance;
}
Serial.print("Distance: ");
if (valid) { Serial.print(distance); Serial.println(" cm"); }
else Serial.println("No valid echo");
switch (state) {
case 1: Serial.println("The Distance is far"); break;
case 2: Serial.println("The OBJECT is TOO CLOSE"); break;
case 3: Serial.println("Object is Closer"); break;
case 4: Serial.println("Safe Zone"); break;
default: Serial.println("Check object/sensor"); break;
}
lastState = state;
delay(500);
}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.




