This ultrasonic distance warning system uses an HC-SR04 sensor to detect how far an object is and provides real-time feedback through LEDs, a buzzer, and an LCD display. The system categorizes distances into zones: far (20cm+), medium (15-20cm), close (10-15cm), and critical (under 10cm), with the buzzer sounding an alarm when an object gets too close.
The guide includes a complete wiring diagram showing how to connect the ultrasonic sensor, three indicator LEDs, a piezo buzzer, and a 20x4 I2C LCD display to an Arduino Uno. Builders will receive the full parts list, step-by-step assembly instructions, and ready-to-upload firmware that handles sensor readings, distance calculations, and multi-output alerts.
Wiring diagram

Gather all the parts
Assemble it in 3 steps
1. Connect the three LEDs with USB unplugged
Connect D2, D3 and D4 through separate 220 ohm resistors to the long legs of the red, yellow and blue LEDs respectively. Connect each short leg to Uno GND.
2. Wire the ultrasonic sensor and buzzer
Connect sensor VCC to 5V, GND to GND, TRIG to D6 and ECHO to D7. Connect the passive piezo positive lead to D5 and its other lead to GND.
3. Wire the four-line LCD
Connect VCC to 5V, GND to GND, SDA to A4 and SCL to A5. If the backlight is on but text is hard to see, gently adjust the contrast screw on the LCD backpack.
Review all connections
1. Connections between "sonar" and "Arduino"
2. Connections between "lcd" and "Arduino"
3. Connections between "piezo" 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>
hd44780_I2Cexp lcd;
const int RED = 2, YELLOW = 3, BLUE = 4, BUZZER = 5;
const int TRIG = 6, ECHO = 7;
int previous = -1;
bool lcdOK;
void setup() {
for (int pin = RED; pin <= TRIG; pin++) pinMode(pin, OUTPUT);
pinMode(ECHO, INPUT);
lcdOK = (lcd.begin(20, 4) == 0);
}
void loop() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
unsigned long echo = pulseIn(ECHO, HIGH, 25000);
float cm = echo / 58.0;
int state = 0; // No echo
if (echo != 0) {
if (cm >= 20) state = 1;
else if (cm <= 10) state = 2;
else if (cm <= 15) state = 3;
else state = 4; // Above 15 but below 20 cm
}
if (state != previous) {
previous = state;
digitalWrite(RED, state == 1);
digitalWrite(BLUE, state == 2);
digitalWrite(YELLOW, state == 3);
if (state == 2) tone(BUZZER, 4000);
else noTone(BUZZER);
if (lcdOK) {
lcd.clear();
if (state == 0) lcd.print(F("No echo"));
if (state == 1) lcd.print(F("The Distance is far"));
if (state == 2) {
lcd.print(F("The OBJECT is"));
lcd.setCursor(0, 1);
lcd.print(F("TOO CLOSE"));
}
if (state == 3) lcd.print(F("Object is Closer"));
if (state == 4) lcd.print(F("Between 15 and 20 cm"));
}
}
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.




