Community project

Ultrasonic Distance Alert System

Photo of Ultrasonic Distance Alert System
Generated with AI

Jhon Paul Datoon

Published November 23, 2026

This project builds a proximity alert system that uses an ultrasonic distance sensor to monitor how close objects are and provides visual and audio feedback. The HC-SR04 sensor measures distance in real time, triggering different colored LEDs and a piezo buzzer based on proximity zones—red for far, yellow for closer, and blue for dangerously close with an active alarm.

The guide includes a complete wiring diagram, parts list, and Arduino firmware that displays live distance readings on a 20x4 LCD screen while managing the alert states. Assembly takes about 30 minutes and requires only basic soldering skills. This system works well for parking assistance, collision avoidance prototypes, or any application needing distance-based triggering.

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 Ω

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

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 power first

Unplug USB before connecting wires. Use jumper wires and a solderless breadboard if available. This project uses USB power only.

2. Connect the three lights

Connect D2 through a 220 ohm resistor to the red LED long leg. Connect D3 through another 220 ohm resistor to the yellow or green LED long leg. Connect D4 through the third resistor to the blue or white LED long leg. Connect each short leg (flat side) to GND.

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

3. Connect the distance sensor

Connect HC-SR04 VCC to 5V, GND to GND, TRIG to D7 (trigger signal), and ECHO to D8 (distance timing signal). Follow the printed pin labels.

  • Aim the sensor at a flat object between 2 and 400 cm away. Readings below 2 cm are not reliable.

4. Connect the passive piezo buzzer

Connect one piezo lead to D9 (tone signal) and the other to GND. If marked plus, connect that lead to D9. Use a small passive piezo, not a high-current speaker or magnetic buzzer.

5. Connect the four-line display

Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). All ground connections must join the Uno GND.

  • If the backlight comes on but letters are invisible
  • gently turn the contrast screw on the LCD backpack.
  • Check VCC and GND before powering up; reversed power can damage the modules.

Review all connections

1. Connections between "red_r" and "Arduino"

Functionred_rArduino
digitalP1GPIO 2
digitalP2 → LED ANODEEXT

2. Connections between "red" and "Arduino"

FunctionredArduino
groundGNDGND

3. Connections between "yellow_r" and "Arduino"

Functionyellow_rArduino
digitalP1GPIO 3
digitalP2 → LED ANODEEXT

4. Connections between "yellow" and "Arduino"

FunctionyellowArduino
groundGNDGND

5. Connections between "blue_r" and "Arduino"

Functionblue_rArduino
digitalP1GPIO 4
digitalP2 → LED ANODEEXT

6. Connections between "blue" and "Arduino"

FunctionblueArduino
groundGNDGND

7. Connections between "sonar" and "Arduino"

FunctionsonarArduino
digitalTRIGGPIO 7
digitalECHOGPIO 8
powerVCC5V
groundGNDGND

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>

const byte RED = 2, YELLOW = 3, BLUE = 4;
const byte TRIG = 7, ECHO = 8, BUZZER = 9;
hd44780_I2Cexp lcd; // Automatically finds the LCD address.
bool lcdOK;
int previousState = -1, previousDistance = -2;

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

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 initialization failed: check wiring and contrast."));
}

void loop() {
  digitalWrite(TRIG, LOW); delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long duration = pulseIn(ECHO, HIGH, 30000UL);
  float cm = duration / 58.0f;
  bool valid = duration != 0 && cm >= 2 && cm <= 400;
  // 0: no reading, 1: too close, 2: closer, 3: neutral, 4: far.
  int state = !valid ? 0 : cm <= 10 ? 1 : cm <= 15 ? 2 : cm >= 20 ? 4 : 3;
  int displayedDistance = valid ? (int)(cm + 0.5f) : -1;
  if (state != previousState) {
    digitalWrite(RED, state == 4);
    digitalWrite(YELLOW, state == 2);
    digitalWrite(BLUE, state == 1);
    if (state == 1) tone(BUZZER, 4000);
    else noTone(BUZZER);
    if (lcdOK) {
      line(1, state == 1 ? "The OBJECT is" : state == 2 ? "Object is Closer" :
              state == 4 ? "The Distance is far" : state == 3 ? "Object is nearby" : "No valid echo");
      line(2, state == 1 ? "TOO CLOSE" : "");
      line(3, state == 0 ? "Check sensor/target" : "");
    }
  }
  if (displayedDistance != previousDistance || state != previousState) {
    if (lcdOK) {
      char text[21];
      if (valid) snprintf(text, sizeof(text), "Distance: %d cm", displayedDistance);
      else snprintf(text, sizeof(text), "Distance: -- cm");
      line(0, text);
    }
    Serial.print(F("Distance (cm): "));
    if (valid) Serial.println(cm, 1); else Serial.println(F("No valid echo"));
  }
  previousState = state; previousDistance = displayedDistance;
  delay(100); // More than 60 ms between sensor 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