This project builds a distance monitoring system that uses an ultrasonic sensor to detect how far an object is and provides real-time feedback through LEDs, a buzzer, and an LCD display. The system triggers alerts when objects get too close, making it useful for parking assistance, collision avoidance, or proximity monitoring applications.
The guide includes a complete wiring diagram showing how to connect the HC-SR04 ultrasonic sensor, I2C LCD display, three indicator LEDs, and piezo buzzer to an Arduino Uno. Builders will receive the full Arduino firmware, a detailed parts list, step-by-step assembly instructions, and testing procedures to verify the circuit works correctly.
Wiring diagram

Gather all the parts
Assemble it in 6 steps
1. Disconnect power first
Unplug USB before connecting wires. Add three 220 ohm resistors, one per LED, and use jumper wires for the connections.
- Do not connect an LED directly to an Arduino output; excess current can damage it or the board.
2. Connect the ultrasonic sensor
Connect VCC to 5V and GND to GND. Connect TRIG to D9 to send the measuring pulse, and ECHO to D10 to read its return time. Use the printed pin names on the module.
3. Connect the LCD
Connect VCC to 5V, GND to GND, SDA to A4 for data, and SCL to A5 for the clock. This is a 20-column, four-row character LCD with an I2C backpack.
- If the screen lights but text is invisible
- gently adjust the small contrast screw on its backpack.
4. Connect the three indicator LEDs
Connect D2 through the first 220 ohm resistor to the red LED long leg. Connect D3 through the second resistor to the yellow or green LED long leg. Connect D4 through the third resistor to the blue or white LED long leg. Connect every short leg to GND. The short leg or flat side marks the negative side.
5. Connect the passive piezo buzzer
Connect one buzzer lead to D11 for the sound signal and the other to GND. If marked, use the positive lead for D11. All parts share the Arduino ground.
- This connection is for a small passive piezo
- not a high-current siren or magnetic buzzer.
6. Inspect and test the circuit
Check for swapped power wires and confirm every LED has its own resistor. Point the sensor toward a flat object and test at about 8 cm, 12 cm, 17 cm, and 25 cm. These should give blue with sound, yellow without sound, all off, and red without sound respectively.
- The HC-SR04 is unreliable below 2 cm; no valid echo turns all outputs off and is not proof that the area is clear. This is a laboratory demonstration
- not a safety alarm.
Review all connections
1. Connections between "sensor" and "Arduino"
2. Connections between "lcd" and "Arduino"
3. Connections between "r_red" and "Arduino"
4. Connections between "red" and "Arduino"
5. Connections between "r_yellow" and "Arduino"
6. Connections between "yellow" and "Arduino"
7. Connections between "r_blue" and "Arduino"
8. Connections between "blue" and "Arduino"
9. Connections between "buzzer" 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 = 11, TRIG = 9, ECHO = 10;
hd44780_I2Cexp lcd; // Automatically find the LCD address and backpack wiring.
int previousState = -1;
long previousTenths = -999;
bool lcdReady = false;
// Write a complete line, erasing any leftover characters.
void line(byte row, const char *text) {
lcd.setCursor(0, row);
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);
lcdReady = (lcd.begin(20, 4) == 0);
if (!lcdReady) 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.0f;
bool valid = duration != 0 && distance >= 2.0f && distance <= 400.0f;
// 0=no reading, 1=too close, 2=closer, 3=neutral, 4=far.
int state = 0;
if (valid) {
if (distance <= 10.0f) state = 1;
else if (distance <= 15.0f) state = 2;
else if (distance >= 20.0f) state = 4;
else state = 3; // The assignment does not specify 15 < distance < 20.
}
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 (lcdReady) {
if (state == 1) {
line(1, "The OBJECT is");
line(2, "TOO CLOSE");
} else {
if (state == 2) line(1, "Object is Closer");
else if (state == 4) line(1, "The Distance is far");
else if (state == 3) line(1, "Neutral zone");
else line(1, "No valid echo");
line(2, "");
}
line(3, state == 1 ? "Buzzer: ON" : "Buzzer: OFF");
}
previousState = state;
}
long tenths = valid ? (long)(distance * 10.0f + 0.5f) : -1;
if (tenths != previousTenths) {
if (lcdReady) {
char text[21];
if (valid) snprintf(text, sizeof(text), "Distance: %ld.%ld cm", tenths / 10, tenths % 10);
else snprintf(text, sizeof(text), "Distance: --- cm");
line(0, text);
}
if (valid) Serial.println(distance, 1);
else Serial.println(F("No valid echo"));
previousTenths = tenths;
}
delay(100); // HC-SR04 measurements must be at least 60 ms apart.
}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.




