This project builds an ultrasonic distance alert system that monitors how close an object is and provides real-time feedback through multiple indicators. The HC-SR04 sensor measures distance, while an Arduino Uno processes the data and controls three LEDs (red, yellow, blue), a piezo buzzer, and a 20x4 LCD display to show the current distance state.
Builders will receive a complete wiring diagram showing how to connect the distance sensor, LCD display via I2C, indicator LEDs, and buzzer to the Arduino. The guide includes a full parts list, step-by-step assembly instructions, and ready-to-upload firmware that triggers different alerts based on distance thresholds—perfect for proximity detection, parking assistance, or object avoidance applications.
Wiring diagram

Gather all the parts
Assemble it in 3 steps
1. Connect the distance sensor and screen
With USB unplugged, connect both modules to 5V and GND. Connect sensor TRIG to D8 and ECHO to D9. Connect LCD SDA to A4 and SCL to A5.
- Check the printed pin labels rather than assuming the pins are in the same order.
2. Connect the three lights
Run D2 to the red LED, D3 to the yellow or green LED, and D4 to the blue or white LED. Put one 220 ohm resistor between each Arduino pin and its LED long leg. Connect each short leg to GND.
3. Connect the passive piezo
Connect the positive lead to D5 and the negative lead to GND.
Review all connections
1. Connections between "sonar" and "Arduino"
2. Connections between "lcd" and "Arduino"
3. Connections between "piezo" and "Arduino"
4. Connections between "r2" and "Arduino"
5. Connections between "red" and "Arduino"
6. Connections between "r3" and "Arduino"
7. Connections between "yellow" and "Arduino"
8. Connections between "r4" 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>
hd44780_I2Cexp lcd;
const byte RED = 2, YELLOW = 3, BLUE = 4, BUZZER = 5;
const byte TRIG = 8, ECHO = 9;
enum State { NO_ECHO, TOO_CLOSE, CLOSER, IN_BETWEEN, FAR };
int previous = -1;
bool lcdReady;
void setup() {
pinMode(RED, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
lcdReady = (lcd.begin(20, 4) == 0);
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
unsigned long echoTime = pulseIn(ECHO, HIGH, 30000UL);
float cm = echoTime / 58.0f;
State state;
if (echoTime == 0) state = NO_ECHO;
else if (cm >= 20) state = FAR;
else if (cm <= 10) state = TOO_CLOSE;
else if (cm <= 15) state = CLOSER;
else state = IN_BETWEEN;
if (state != previous) {
previous = state;
digitalWrite(RED, state == FAR);
digitalWrite(YELLOW, state == CLOSER);
digitalWrite(BLUE, state == TOO_CLOSE);
if (state == TOO_CLOSE) tone(BUZZER, 2500);
else noTone(BUZZER);
if (lcdReady) {
lcd.clear();
lcd.setCursor(0, 0);
switch (state) {
case FAR: lcd.print("The Distance is far"); break;
case CLOSER: lcd.print("Object is Closer"); break;
case TOO_CLOSE:
lcd.print("The OBJECT is");
lcd.setCursor(0, 1);
lcd.print("TOO CLOSE");
break;
case IN_BETWEEN: lcd.print("Between 15 and 20 cm"); break;
case NO_ECHO: lcd.print("No echo received"); break;
}
}
}
delay(80);
}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.




