Community project

Ultrasonic Distance Alert System

Photo of Ultrasonic Distance Alert System
Generated with AI

Jhon Paul Datoon

Published November 23, 2026

This ultrasonic distance alert system uses an HC-SR04 sensor to monitor how close objects are and provides real-time feedback through LEDs, a buzzer, and an LCD display. The Arduino Uno continuously measures distance and triggers different alert levels: red LED and loud buzzer for objects too close, yellow for approaching objects, and blue for safe distances.

Builders will receive a complete wiring diagram showing how to connect the distance sensor, three indicator LEDs with resistors, passive piezo buzzer, and I2C LCD display to the Arduino Uno. The guide includes step-by-step assembly instructions, the full firmware with distance thresholds and alert logic, and testing procedures to verify the system responds correctly at different ranges.

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

1. Prepare the circuit with power disconnected

Unplug USB while wiring. Use jumper wires and a breadboard to join the listed connections. Connect Uno 5V and GND to separate breadboard rails; bridge split rails if needed. All GND connections share the same ground.

  • Never connect 5V directly to GND; a short can damage the board or USB port.

2. Connect the three indicator LEDs

D2 goes through rRed (220 ohms) to the red LED long leg. D3 goes through rYellow (220 ohms) to the yellow or green LED long leg. D4 goes through rBlue (220 ohms) to the blue or white LED long leg. Each short leg goes to GND.

  • Each LED needs its own series resistor to prevent LED and board damage.

3. Connect the distance sensor

HC-SR04 VCC goes to 5V, GND to GND, TRIG to D7 (measurement trigger), and ECHO to D8 (return pulse). Follow the printed pin labels. Face the two round transducers toward the object.

4. Connect the passive piezo buzzer

Connect the piezo signal lead to D9 and its other lead to GND. Use a small passive piezo element, not a high-current magnetic buzzer.

5. Connect the four-row LCD

Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). This project uses a 20-column, 4-row LCD with an I2C backpack. Adjust its contrast screw gently if the text is not visible.

6. Inspect the wiring and test distances

Check LED polarity, resistor placement, and power connections. Test with a flat object at 8 cm, 13 cm, 17 cm, and 25 cm. These select blue with sound, yellow or green without sound, all off, and red without sound. Below the sensor's 2 cm minimum or without an echo, all outputs are off; this is a classroom demonstration, not a safety alarm.

Review all connections

1. Connections between "rRed" and "Arduino"

FunctionrRedArduino
digitalP1GPIO 2
digitalP2 → LED ANODEEXT

2. Connections between "red" and "Arduino"

FunctionredArduino
groundGNDGND

3. Connections between "rYellow" and "Arduino"

FunctionrYellowArduino
digitalP1GPIO 3
digitalP2 → LED ANODEEXT

4. Connections between "yellow" and "Arduino"

FunctionyellowArduino
groundGNDGND

5. Connections between "rBlue" and "Arduino"

FunctionrBlueArduino
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>

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 lastState = -1;
long lastDistance = -1;

// Write a full row to erase leftover letters without flickering.
void row(byte number, const char *text) {
  lcd.setCursor(0, number);
  byte i = 0;
  while (text[i] && i < 20) lcd.write(text[i++]);
  while (i++ < 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) row(0, "Distance Monitor");
  else Serial.println("LCD error: 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 distance = duration / 58.0;
  bool valid = duration != 0 && distance >= 2 && distance <= 400;

  // 0 = no reading, 1 = too close, 2 = closer, 3 = gap, 4 = far
  int state;
  if (!valid) state = 0;
  else if (distance <= 10) state = 1;
  else if (distance <= 15) state = 2;
  else if (distance >= 20) state = 4;
  else state = 3;

  if (state != lastState) {
    digitalWrite(RED, state == 4);
    digitalWrite(YELLOW, state == 2);
    digitalWrite(BLUE, state == 1);
    if (state == 1) tone(BUZZER, 4000);
    else noTone(BUZZER);
    if (lcdOK) {
      if (state == 1) {
        row(2, "The OBJECT is");
        row(3, "TOO CLOSE");
      } else {
        if (state == 2) row(2, "Object is Closer");
        else if (state == 4) row(2, "The Distance is far");
        else if (state == 3) row(2, "Between 15 and 20 cm");
        else row(2, "No valid echo");
        row(3, "");
      }
    }
  }
  long displayed = valid ? (long)(distance * 10 + 0.5) : -1;
  if (lcdOK && displayed != lastDistance) {
    char text[21];
    if (valid) snprintf(text, sizeof(text), "Distance: %ld.%ld cm",
                        displayed / 10, displayed % 10);
    else snprintf(text, sizeof(text), "Distance: ---");
    row(1, text);
  }
  lastState = state;
  lastDistance = displayed;
  delay(100); // More than 60 ms between ultrasonic measurements.
}

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