Community project

Ultrasonic Distance Alert System

Photo of Ultrasonic Distance Alert System
Generated with AI

kitz aberde

Published November 23, 2026

This ultrasonic distance alert system uses an HC-SR04 sensor to continuously monitor the distance to nearby objects and provides real-time visual and audio feedback. The Arduino Uno processes distance readings and controls three color-coded LEDs (red for far, yellow for approaching, blue for too close) along with a piezo buzzer that sounds when an object gets dangerously near.

Builders will receive a complete wiring diagram showing how to connect the distance sensor, LEDs with current-limiting resistors, passive buzzer, and I2C LCD display to the Arduino Uno. The guide includes the full firmware with distance thresholds, assembly steps starting from a disconnected state, and a parts list. The 20x4 LCD displays real-time distance measurements and system status, making this project ideal for learning sensor integration and multi-output control.

Wiring diagram

Wiring diagram for Ultrasonic Distance Alert System
Loading diagram…

Gather all the parts

QtyComponent
1

LED

Red

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

1

LED

Yellow or green

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

1

LED

Blue or white

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

1

Resistor

220 Ω, 1/4 W

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

1

Resistor

220 Ω, 1/4 W

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

1

Resistor

220 Ω, 1/4 W

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

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

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 5 steps

1. Disconnect USB before wiring

Use jumper wires to make the connections below. All GND connections share the Arduino GND. Power this circuit from USB.

2. Connect the three LEDs through resistors

Connect D2 through r_red (220 ohms) to the red LED long leg; D3 through r_yellow to the yellow or green LED long leg; D4 through r_blue to the blue or white LED long leg. Connect every short leg (flat side) to GND.

  • Never connect an LED directly to an Arduino output without its series resistor.

3. Connect the distance sensor

Connect HC-SR04 VCC to 5V, GND to GND, TRIG to D7 (trigger output), and ECHO to D8 (distance pulse input). Read the pin labels rather than assuming their order.

4. Connect the passive piezo

Connect the piezo signal lead to D9 and its other lead to GND. This design uses a small passive piezo, not a high-current or magnetic buzzer.

5. Connect the 20-column four-row LCD

Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). The code automatically detects the I2C backpack address. Adjust the small contrast screw if text is not visible.

  • Swapping VCC and GND can damage the display.

Review all connections

1. Connections between "r_red" and "Arduino"

Functionr_redArduino
digitalP1GPIO 2
digitalP2 → LED ANODEEXT

2. Connections between "red" and "Arduino"

FunctionredArduino
groundGNDGND

3. Connections between "r_yellow" and "Arduino"

Functionr_yellowArduino
digitalP1GPIO 3
digitalP2 → LED ANODEEXT

4. Connections between "yellow" and "Arduino"

FunctionyellowArduino
groundGNDGND

5. Connections between "r_blue" and "Arduino"

Functionr_blueArduino
digitalP1GPIO 4
digitalP2 → LED ANODEEXT

6. Connections between "blue" and "Arduino"

FunctionblueArduino
groundGNDGND

7. Connections between "sonar" and "Arduino"

FunctionsonarArduino
powerVCC5V
groundGNDGND
digitalTRIGGPIO 7
digitalECHOGPIO 8

8. Connections between "buzzer" and "Arduino"

FunctionbuzzerArduino
pwmSIGGPIO 9
groundGNDGND

9. Connections between "lcd" and "Arduino"

FunctionlcdArduino
powerVCC5V
groundGNDGND
i2cSDAGPIO 18
i2cSCLGPIO 19

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;
const byte TRIG = 7, ECHO = 8, BUZZER = 9;
bool lcdOK;
int previousDistance = -2, previousState = -2;

void line(byte row, const char *text) {
  lcd.setCursor(0, row);
  lcd.print(text);
  for (byte n = strlen(text); n < 20; n++) lcd.print(' ');
}

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);
  lcdOK = (lcd.begin(20, 4) == 0);
  if (!lcdOK) Serial.println(F("LCD not found: check wiring"));
}

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.0;
  bool valid = duration != 0 && distance >= 2 && distance <= 400;
  // State: 0=no reading, 1=far, 2=too close, 3=closer, 4=gap.
  int state = 0;
  if (valid) {
    if (distance >= 20) state = 1;
    else if (distance <= 10) state = 2;
    else if (distance <= 15) state = 3;
    else state = 4;
  }
  digitalWrite(RED, state == 1);
  digitalWrite(BLUE, state == 2);
  digitalWrite(YELLOW, state == 3);
  if (state != previousState) {
    if (state == 2) tone(BUZZER, 4000);
    else noTone(BUZZER);
  }
  int shownDistance = valid ? (int)(distance + 0.5) : -1;
  if (lcdOK && (shownDistance != previousDistance || state != previousState)) {
    char text[21];
    if (valid) snprintf(text, sizeof(text), "Distance: %d cm", shownDistance);
    else strcpy(text, "Distance: no echo");
    line(0, text);
    line(1, state == 1 ? "The Distance is far" :
            state == 2 ? "The OBJECT is" :
            state == 3 ? "Object is Closer" :
            state == 4 ? "Between 15 and 20 cm" : "No valid reading");
    line(2, state == 2 ? "TOO CLOSE" : "");
    line(3, state == 2 ? "Buzzer: ON" : "Buzzer: OFF");
  }
  previousDistance = shownDistance;
  previousState = state;
  delay(100);
}

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