Community project

Ultrasonic Distance Warning System

Photo of Ultrasonic Distance Warning System
Generated with AI

gabbycasuyon-web

Published November 30, 2026

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

Wiring diagram for Ultrasonic Distance Warning System
Loading diagram…

Gather all the parts

QtyComponent
1

HC-SR04

HC-SR04 ultrasonic distance sensor module with separate 40 kHz transmitter and receiver transducers, measuring 2 to 400 cm. A 10 us pulse on TRIG starts a measurement and ECHO stays high for the echo's round-trip time. This HC-SR04+ board runs from 3.3 V or 5 V.

1

LED

Red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

LED

Yellow

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

LED

Blue

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

Resistor

220 Ω

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

220 Ω

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

220 Ω

Through-hole resistor (current-limiting in series with an LED)

1

Piezo Buzzer

Small passive piezo buzzer (TDK PS1240) for beeps, tones and alerts. It has no built-in oscillator, so drive one lead with a 3-30 V square wave from a microcontroller pin using tone() or PWM and connect the other lead to ground. It is loudest around 4 kHz and works from about 2 to 10 kHz; driving both leads in antiphase roughly doubles the volume.

1

I2C 20x4 Arduino LCD Display Module

20x4 character LCD with I2C interface, white text on blue backlight, adjustable contrast. Operates at 5V or 3.3V; reduces pin usage to 2 lines (SDA/SCL).

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"

Functionred_resistorArduino
digitalP1 → Red LED signal; red jumper to breadboard a10GPIO 2
digitalP2 → LED ANODEEXT

2. Connections between "red_led" and "Arduino"

Functionred_ledArduino
groundGND → LED negative d15; black jumper a15 to ground railGND

3. Connections between "yellow_resistor" and "Arduino"

Functionyellow_resistorArduino
digitalP1 → Yellow LED signal; yellow jumper to a20GPIO 3
digitalP2 → LED ANODEEXT

4. Connections between "yellow_led" and "Arduino"

Functionyellow_ledArduino
groundGND → LED negative d25; black jumper a25 to ground railGND

5. Connections between "blue_resistor" and "Arduino"

Functionblue_resistorArduino
digitalP1 → Blue LED signal; blue jumper to a30GPIO 4
digitalP2 → LED ANODEEXT

6. Connections between "blue_led" and "Arduino"

Functionblue_ledArduino
groundGND → LED negative d35; black jumper a35 to ground railGND

7. Connections between "piezo" and "Arduino"

FunctionpiezoArduino
pwmSIG → Purple jumper; buzzer positive at e40 via a40GPIO 5
groundGND → Black jumper; buzzer negative at e43 via a43GND

8. Connections between "sonar" and "Arduino"

FunctionsonarArduino
powerVCC → Red jumper from positive breadboard rail5V
groundGND → Black jumper from negative breadboard railGND
digitalTRIG → Green jumper; trigger outputGPIO 9
digitalECHO → Orange jumper; echo inputGPIO 10

9. Connections between "lcd" and "Arduino"

FunctionlcdArduino
powerVCC → Red jumper from positive breadboard rail5V
groundGND → Black jumper from negative breadboard railGND
i2cSDA → Green jumper; I2C dataGPIO 18
i2cSCL → Blue jumper; I2C clockGPIO 19

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.

Open in Schematik