This project builds a distance monitoring system using an ultrasonic sensor to detect how far an object is and provide real-time visual and audio feedback. The HC-SR04 sensor measures distance, triggering different colored LEDs and a piezo buzzer to alert when objects enter danger zones, with all readings displayed on a 20x4 LCD screen.
The guide includes a complete wiring diagram showing how to connect the ultrasonic sensor, LEDs with current-limiting resistors, buzzer, and I2C LCD display to an Arduino Uno. Assembly steps walk through the setup process, and the provided firmware handles distance calculations, state management, and multi-zone alerts so the system is ready to deploy after uploading the code.
Wiring diagram

Gather all the parts
Assemble it in 6 steps
1. Disconnect power
Unplug USB before connecting wires. Use jumper wires and a solderless breadboard for the laboratory build.
2. Connect the LEDs with resistors
Connect D2 through a 330 ohm resistor to the red LED's long leg; D3 through its own 330 ohm resistor to the yellow (or green) LED's long leg; D4 through its own 330 ohm resistor to the blue (or white) LED's long leg. Connect each short leg (flat side) to GND.
- Never connect an LED directly to an Arduino pin without its series resistor.
3. Connect the distance sensor
Connect HC-SR04 VCC to 5V, GND to GND, TRIG to D6 (trigger output), and ECHO to D7 (distance pulse input). Follow printed pin labels.
4. Connect the piezo buzzer
Connect the passive piezo signal lead to D5 and the other lead to GND. If marked, use plus for D5 and minus for GND.
5. Connect the LCD
Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). All grounds must be joined. Adjust the LCD contrast screw gently if text is invisible.
6. Check the laboratory setup
Check for reversed power and short circuits before plugging in USB. Aim the sensor at a flat object and test at 8, 12, 17, and 25 cm. The sensor measures about 2 to 400 cm; missing echoes are not proof that an object is far away.
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 BUZZER = 5, TRIG = 6, ECHO = 7;
hd44780_I2Cexp lcd; // Automatically finds the LCD address.
bool lcdOK;
int previousDistance = -2, previousState = -1;
// Pad each row to remove old text, without clearing the screen.
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) 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 cm = duration / 58.0f;
bool valid = duration != 0 && cm >= 2 && cm <= 400;
int state;
if (!valid) state = 0;
else if (cm >= 20) state = 1;
else if (cm <= 10) state = 2;
else if (cm <= 15) state = 3;
else state = 4; // Unspecified range: more than 15, less than 20.
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 distance = valid ? (int)(cm + 0.5f) : -1;
if (lcdOK && (distance != previousDistance || state != previousState)) {
char line[21];
if (valid) snprintf(line, sizeof(line), "Distance: %d cm", distance);
else snprintf(line, sizeof(line), "Distance: --");
row(0, line);
if (state == 1) { row(1, "The Distance is far"); row(2, ""); }
else if (state == 2) { row(1, "The OBJECT is"); row(2, "TOO CLOSE"); }
else if (state == 3) { row(1, "Object is Closer"); row(2, ""); }
else if (state == 4) { row(1, "Object approaching"); row(2, "15 < distance < 20"); }
else { row(1, "No valid echo"); row(2, "Check object/sensor"); }
row(3, state == 2 ? "Buzzer: ON" : "Buzzer: OFF");
}
previousDistance = distance;
previousState = state;
delay(100); // HC-SR04 needs at least 60 ms between 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.




