This project builds a distance monitoring system that uses an ultrasonic sensor to detect how far an object is from the Arduino, then displays the measurement on an LCD screen and provides visual and audio alerts based on distance thresholds. The system uses three LEDs and a piezo buzzer to indicate different distance ranges, making it useful for parking assistance, object detection, or proximity warnings.
The guide includes a complete wiring diagram showing how to connect the HC-SR04 ultrasonic sensor, I2C LCD display, LEDs, and piezo buzzer to the Arduino Uno, a full parts list with component specifications, the complete Arduino firmware with distance calculation and alert logic, and step-by-step assembly instructions to get the project working.
Wiring diagram
Gather all the parts
Assemble it in 5 steps
1. Prepare with power disconnected
Unplug USB before wiring. Add three 220 ohm resistors to the supplied materials; never connect an LED directly to an Arduino output. Use jumper wires and a solderless breadboard if available.
2. Connect the three LEDs
D2 goes through rred to the red LED long leg. D3 goes through ryellow to the yellow (or green) LED long leg. D4 goes through rblue to the blue (or white) LED long leg. Connect each short leg (flat-edge side) to Arduino GND. Resistors work either way around.
- Missing series resistors can damage the LEDs or Arduino pins.
3. Connect the distance sensor
Follow the printed HC-SR04 labels: VCC to 5V, GND to GND, TRIG to D7 (start measurement), ECHO to D8 (return timing). Aim its two round transducers toward the object.
4. Connect the LCD
Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). This is a 20-column, four-row LCD with an I2C backpack. Adjust the small contrast screw gently if characters are invisible.
5. Connect the passive piezo
Connect one piezo lead to D9 (sound signal) and the other to GND. Use a small passive piezo, not a high-current magnetic buzzer. Check that all ground connections join Arduino GND and power wires are not swapped.
Review all connections
1. Connections between "sonar" and "Arduino"
2. Connections between "lcd" and "Arduino"
3. Connections between "buzzer" and "Arduino"
4. Connections between "rred" and "Arduino"
5. Connections between "red" and "Arduino"
6. Connections between "ryellow" and "Arduino"
7. Connections between "yellow" and "Arduino"
8. Connections between "rblue" and "Arduino"
9. Connections between "blue" and "Arduino"
Deploy the firmware
#include <Arduino.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <string.h>
const byte RED = 2, YELLOW = 3, BLUE = 4;
const byte TRIG = 7, ECHO = 8, BUZZER = 9;
hd44780_I2Cexp lcd; // Automatically detect LCD address and backpack wiring.
bool lcdReady = false;
char previous[4][21] = {};
// Update only LCD rows whose text changed; pad to erase old characters.
void showRow(byte row, const char *text) {
char padded[21];
memset(padded, ' ', 20);
padded[20] = '\0';
size_t n = strlen(text);
if (n > 20) n = 20;
memcpy(padded, text, n);
if (lcdReady && strcmp(previous[row], padded) != 0) {
lcd.setCursor(0, row);
lcd.print(padded);
strcpy(previous[row], padded);
}
}
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 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 distance = duration / 58.0f;
bool valid = duration != 0 && distance >= 2 && distance <= 400;
byte state = 0; // Undefined 15-20 cm range: all outputs off.
const char *line1 = "Between 15 and 20 cm";
const char *line2 = "";
if (!valid) {
line1 = "No valid echo";
line2 = "Check target/sensor";
} else if (distance >= 20) {
state = 1;
line1 = "The Distance is far";
} else if (distance <= 10) {
state = 3;
line1 = "The OBJECT is";
line2 = "TOO CLOSE";
} else if (distance <= 15) {
state = 2;
line1 = "Object is Closer";
}
digitalWrite(RED, state == 1);
digitalWrite(YELLOW, state == 2);
digitalWrite(BLUE, state == 3);
static byte lastState = 255;
if (state != lastState) {
if (state == 3) tone(BUZZER, 4000);
else noTone(BUZZER);
lastState = state;
}
char measurement[21];
if (valid) {
char number[10];
dtostrf(distance, 5, 1, number);
snprintf(measurement, sizeof(measurement), "Distance: %s cm", number);
Serial.println(measurement);
} else {
strcpy(measurement, "Distance: -- cm");
}
showRow(0, measurement);
showRow(1, line1);
showRow(2, line2);
showRow(3, "");
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.




