This project builds an ultrasonic distance warning system that monitors how close objects are to the HC-SR04 sensor and provides real-time feedback through multiple output channels. As an object approaches, the system lights up different colored LEDs, sounds an alarm, and displays status messages on an LCD screen to alert the user to proximity changes.
The guide includes a complete wiring diagram showing how to connect the ultrasonic sensor, three indicator LEDs with current-limiting resistors, a piezo buzzer, and an I2C LCD display to an Arduino Uno. Assembly steps walk through breadboard setup, component placement, and circuit verification, while the provided firmware handles distance measurement, state tracking, and coordinated activation of all warning indicators.
Wiring diagram

Gather all the parts
Assemble it in 8 steps
1. Prepare the breadboard power rails
Keep USB unplugged while wiring. Place the Uno R3 beside an 830-point breadboard. Use red jumpers from Uno 5V to the breadboard + rail and black jumpers from Uno GND to the - rail. Use only these two rails for all power returns. Bridge each rail's midpoint if your breadboard has split rails.
- Rows a-e are connected together; f-j are separate across the central gap. Different numbered rows are not connected.
- Use red for 5V and the D2 LED signal, black for GND, yellow for the D3 LED signal, and blue for the D4 LED signal. Use male-to-male jumpers for the breadboard and female-to-male jumpers for module headers.
- Do not connect 5V to GND: a short circuit can damage the board or USB supply.
2. Install the red LED and its resistor
Place red LED long positive leg in d14 and short negative leg in d15. Put a 220-ohm resistor between e10 and e14. Connect Uno D2 to a10 with red wire. Connect a15 to the ground rail with black wire. This makes D2 -> resistor -> LED positive; LED negative -> GND.
3. Install the yellow LED and its resistor
Place yellow LED positive in d24 and negative in d25. Put its 220-ohm resistor between e20 and e24. Connect Uno D3 to a20 with yellow wire and a25 to the ground rail with black wire.
4. Install the blue LED and its resistor
Place blue LED positive in d34 and negative in d35. Put its 220-ohm resistor between e30 and e34. Connect Uno D4 to a30 with blue wire and a35 to the ground rail with black wire.
- Each LED needs its own resistor to prevent excessive current damaging the LED or Uno pin.
5. Connect the passive piezo buzzer
Insert the passive piezo leads into e40 and e43. Connect a40 to Uno D5 with purple wire and a43 to the ground rail with black wire. If marked, put the + lead in e40. Unmarked passive piezos work either way round.
- Use a small passive piezo, not a high-current active or magnetic buzzer.
6. Connect the HC-SR04
Keep the sensor beside the breadboard, facing the object. Read its header labels: red wire VCC -> + rail (5V); black GND -> - rail; green TRIG -> Uno D9; orange ECHO -> Uno D10. Use female-to-male jumpers.
- A 5V echo is compatible with the Uno R3, so no voltage divider is needed.
- Usable distance is approximately 2-400 cm; missing echoes turn all indicators off and display No valid echo.
7. Connect the four-line LCD
Connect the LCD backpack VCC to the + rail with red wire, GND to the - rail with black wire, SDA to Uno A4 with green wire, and SCL to Uno A5 with blue wire. Read the labels rather than assuming header order. The display sits beside the breadboard.
- The code detects the I2C address automatically. Adjust the backpack contrast screw gently if letters are not visible.
- Messages occupy the first line; remaining lines stay blank. Fractional distances above 15 and below 20 cm use Object approaching.
8. Check the complete circuit
Confirm D2 red LED, D3 yellow LED, D4 blue LED, D5 piezo, D9 trigger, D10 echo, A4 LCD data, and A5 LCD clock. Verify every ground returns to the black rail and both modules receive 5V. Ensure LED legs occupy different numbered rows.
Review all connections
1. Connections between "red_resistor" and "Arduino"
2. Connections between "red_led" and "Arduino"
3. Connections between "yellow_resistor" and "Arduino"
4. Connections between "yellow_led" and "Arduino"
5. Connections between "blue_resistor" and "Arduino"
6. Connections between "blue_led" and "Arduino"
7. Connections between "piezo" 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 uint8_t RED_PIN = 2;
const uint8_t YELLOW_PIN = 3;
const uint8_t BLUE_PIN = 4;
const uint8_t BUZZER_PIN = 5;
const uint8_t TRIG_PIN = 9;
const uint8_t ECHO_PIN = 10;
hd44780_I2Cexp lcd; // Detect backpack address and pin mapping automatically.
enum State { UNKNOWN, FAR, CLOSE, CLOSER, APPROACHING, NO_ECHO };
State previous=UNKNOWN;
bool lcdReady=false;
void showState(State state) {
if (state==previous) return; // Redraw only when the message changes.
previous=state;
digitalWrite(RED_PIN,state==FAR);
digitalWrite(YELLOW_PIN,state==CLOSER);
digitalWrite(BLUE_PIN,state==CLOSE);
if(state==CLOSE) tone(BUZZER_PIN,4000);
else { noTone(BUZZER_PIN); digitalWrite(BUZZER_PIN,LOW); }
const char* message="No valid echo";
switch(state) {
case FAR: message="The distance is far"; break;
case CLOSE: message="OBJECT IS TOO CLOSE"; break;
case CLOSER: message="Object is closer"; break;
case APPROACHING: message="Object approaching"; break;
default: break;
}
Serial.println(message);
if(lcdReady) {
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(message);
lcd.backlight();
}
}
void setup() {
Serial.begin(9600);
pinMode(RED_PIN,OUTPUT); pinMode(YELLOW_PIN,OUTPUT);
pinMode(BLUE_PIN,OUTPUT); pinMode(BUZZER_PIN,OUTPUT);
pinMode(TRIG_PIN,OUTPUT); pinMode(ECHO_PIN,INPUT);
digitalWrite(TRIG_PIN,LOW);
lcdReady=(lcd.begin(20,4)==0);
if(!lcdReady) Serial.println("LCD not detected: check wiring and contrast.");
showState(NO_ECHO);
}
void loop() {
digitalWrite(TRIG_PIN,LOW); delayMicroseconds(2);
digitalWrite(TRIG_PIN,HIGH); delayMicroseconds(10);
digitalWrite(TRIG_PIN,LOW);
unsigned long duration=pulseIn(ECHO_PIN,HIGH,30000UL);
float cm=duration/58.0f;
if(duration==0 || cm<2 || cm>400) showState(NO_ECHO);
else if(cm>=20) showState(FAR);
else if(cm<=10) showState(CLOSE);
else if(cm<=15) showState(CLOSER);
else showState(APPROACHING); // 15 < cm < 20: avoids a fractional-distance gap.
delay(100); // More than the sensor's required 60 ms between triggers.
}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.




