This project builds a distance monitoring system that alerts users when objects approach too closely. Using an HC-SR04 ultrasonic sensor, the system continuously measures distance and provides real-time feedback through three color-coded LEDs, an audible buzzer, and a 20x4 LCD display showing the current distance in centimeters.
The guide includes a complete wiring diagram, parts list with recommended component values, and ready-to-upload Arduino firmware. Assembly steps cover safe power management, sensor calibration, and testing across three distance ranges to ensure the warning system responds correctly at close (under 10cm), medium (10-15cm), and far (over 20cm) distances.
Wiring diagram

Gather all the parts
Assemble it in 6 steps
1. Disconnect power first
Unplug USB while making connections. Use jumper wires to join every GND connection to the Uno GND. Power this circuit from USB.
2. Connect the three LEDs safely
Connect D2 through a 220 ohm resistor to the red LED long leg, D3 through a separate 220 ohm resistor to the yellow LED long leg, and D4 through a third 220 ohm resistor to the blue LED long leg. Connect all short legs (flat-sided cathodes) to GND. Green may replace yellow; white may replace blue.
- Never connect an LED directly to a digital pin without its series resistor.
3. Connect the ultrasonic sensor
Connect HC-SR04 VCC to 5V, GND to GND, TRIG to D6 (trigger output), and ECHO to D7 (distance pulse input). Follow the printed pin labels.
4. Connect the piezo buzzer
Connect the passive piezo signal lead to D9 and its other lead to GND. Use a small passive piezo, not a high-current active buzzer.
5. Connect the four-line screen
Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). Check the printed labels rather than assuming the connector order. The code automatically detects the I2C backpack address. Adjust the contrast screw gently if the backlight is lit but text is invisible.
6. Test each distance range
After powering the circuit, move a broad flat object in front of the sensor. At 20 cm or more red lights; at 10 cm or less blue lights and the piezo sounds; above 10 through 15 cm yellow lights. Above 15 but below 20 cm all indicators are off because the assignment gives no rule for that interval. With no valid echo all indicators turn off and the screen reports a measurement problem. Readings below 2 cm are outside the sensor range.
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>
const byte RED = 2, YELLOW = 3, BLUE = 4;
const byte TRIG = 6, ECHO = 7, BUZZER = 9;
hd44780_I2Cexp lcd; // Automatically finds the backpack address.
bool lcdOK;
// Write only lines whose text has changed, avoiding flicker.
void line(byte row, const char *text) {
static char old[4][21] = {{0}};
if (!lcdOK || strcmp(old[row], text) == 0) return;
lcd.setCursor(0, row);
lcd.print(text);
for (byte i = strlen(text); i < 20; i++) lcd.print(' ');
strncpy(old[row], text, 20);
old[row][20] = '\0';
}
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"));
line(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 distance = duration / 58.0;
bool valid = duration != 0 && distance >= 2 && distance <= 400;
bool far = valid && distance >= 20;
bool close = valid && distance <= 10;
bool closer = valid && distance > 10 && distance <= 15;
digitalWrite(RED, far);
digitalWrite(BLUE, close);
digitalWrite(YELLOW, closer);
static bool sounding = false;
if (close != sounding) {
if (close) tone(BUZZER, 4000);
else noTone(BUZZER);
sounding = close;
}
char value[21];
if (valid) {
char number[10];
dtostrf(distance, 5, 1, number);
snprintf(value, sizeof(value), "Distance: %s cm", number);
} else strcpy(value, "Distance: no echo");
line(1, value);
if (!close) line(3, "");
if (!valid) line(2, "Check object/sensor");
else if (far) line(2, "The Distance is far");
else if (close) {
line(2, "The OBJECT is");
line(3, "TOO CLOSE");
} else if (closer) line(2, "Object is Closer");
else line(2, "Between 15 and 20 cm");
delay(150);
}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.




