Community project

Ultrasonic Distance Alert System

Photo of Ultrasonic Distance Alert System
Generated with AI

Myrtel Abigael Lubas

Published November 23, 2026

This project builds a distance monitoring system that uses an ultrasonic sensor to detect how far an object is and provides real-time feedback through LEDs, a buzzer, and an LCD display. The system triggers alerts when objects get too close, making it useful for parking assistance, collision avoidance, or proximity monitoring applications.

The guide includes a complete wiring diagram showing how to connect the HC-SR04 ultrasonic sensor, I2C LCD display, three indicator LEDs, and piezo buzzer to an Arduino Uno. Builders will receive the full Arduino firmware, a detailed parts list, step-by-step assembly instructions, and testing procedures to verify the circuit works correctly.

Wiring diagram

Wiring diagram for Ultrasonic Distance Alert 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

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).

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 ohm, 1/4 W

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

1

Resistor

220 ohm, 1/4 W

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

1

Resistor

220 ohm, 1/4 W

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.

Assemble it in 6 steps

1. Disconnect power first

Unplug USB before connecting wires. Add three 220 ohm resistors, one per LED, and use jumper wires for the connections.

  • Do not connect an LED directly to an Arduino output; excess current can damage it or the board.

2. Connect the ultrasonic sensor

Connect VCC to 5V and GND to GND. Connect TRIG to D9 to send the measuring pulse, and ECHO to D10 to read its return time. Use the printed pin names on the module.

3. Connect the LCD

Connect VCC to 5V, GND to GND, SDA to A4 for data, and SCL to A5 for the clock. This is a 20-column, four-row character LCD with an I2C backpack.

  • If the screen lights but text is invisible
  • gently adjust the small contrast screw on its backpack.

4. Connect the three indicator LEDs

Connect D2 through the first 220 ohm resistor to the red LED long leg. Connect D3 through the second resistor to the yellow or green LED long leg. Connect D4 through the third resistor to the blue or white LED long leg. Connect every short leg to GND. The short leg or flat side marks the negative side.

5. Connect the passive piezo buzzer

Connect one buzzer lead to D11 for the sound signal and the other to GND. If marked, use the positive lead for D11. All parts share the Arduino ground.

  • This connection is for a small passive piezo
  • not a high-current siren or magnetic buzzer.

6. Inspect and test the circuit

Check for swapped power wires and confirm every LED has its own resistor. Point the sensor toward a flat object and test at about 8 cm, 12 cm, 17 cm, and 25 cm. These should give blue with sound, yellow without sound, all off, and red without sound respectively.

  • The HC-SR04 is unreliable below 2 cm; no valid echo turns all outputs off and is not proof that the area is clear. This is a laboratory demonstration
  • not a safety alarm.

Review all connections

1. Connections between "sensor" and "Arduino"

FunctionsensorArduino
powerVCC5V
groundGNDGND
digitalTRIGGPIO 9
digitalECHOGPIO 10

2. Connections between "lcd" and "Arduino"

FunctionlcdArduino
powerVCC5V
groundGNDGND
i2cSDAGPIO 18
i2cSCLGPIO 19

3. Connections between "r_red" and "Arduino"

Functionr_redArduino
digitalP1GPIO 2
digitalP2 → LED ANODEEXT

4. Connections between "red" and "Arduino"

FunctionredArduino
groundGNDGND

5. Connections between "r_yellow" and "Arduino"

Functionr_yellowArduino
digitalP1GPIO 3
digitalP2 → LED ANODEEXT

6. Connections between "yellow" and "Arduino"

FunctionyellowArduino
groundGNDGND

7. Connections between "r_blue" and "Arduino"

Functionr_blueArduino
digitalP1GPIO 4
digitalP2 → LED ANODEEXT

8. Connections between "blue" and "Arduino"

FunctionblueArduino
groundGNDGND

9. Connections between "buzzer" and "Arduino"

FunctionbuzzerArduino
pwmSIGGPIO 11
groundGNDGND

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 BUZZER = 11, TRIG = 9, ECHO = 10;
hd44780_I2Cexp lcd; // Automatically find the LCD address and backpack wiring.
int previousState = -1;
long previousTenths = -999;
bool lcdReady = false;

// Write a complete line, erasing any leftover characters.
void line(byte row, const char *text) {
  lcd.setCursor(0, row);
  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);
  lcdReady = (lcd.begin(20, 4) == 0);
  if (!lcdReady) 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.0f;
  bool valid = duration != 0 && distance >= 2.0f && distance <= 400.0f;

  // 0=no reading, 1=too close, 2=closer, 3=neutral, 4=far.
  int state = 0;
  if (valid) {
    if (distance <= 10.0f) state = 1;
    else if (distance <= 15.0f) state = 2;
    else if (distance >= 20.0f) state = 4;
    else state = 3; // The assignment does not specify 15 < distance < 20.
  }

  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 (lcdReady) {
      if (state == 1) {
        line(1, "The OBJECT is");
        line(2, "TOO CLOSE");
      } else {
        if (state == 2) line(1, "Object is Closer");
        else if (state == 4) line(1, "The Distance is far");
        else if (state == 3) line(1, "Neutral zone");
        else line(1, "No valid echo");
        line(2, "");
      }
      line(3, state == 1 ? "Buzzer: ON" : "Buzzer: OFF");
    }
    previousState = state;
  }

  long tenths = valid ? (long)(distance * 10.0f + 0.5f) : -1;
  if (tenths != previousTenths) {
    if (lcdReady) {
      char text[21];
      if (valid) snprintf(text, sizeof(text), "Distance: %ld.%ld cm", tenths / 10, tenths % 10);
      else snprintf(text, sizeof(text), "Distance: --- cm");
      line(0, text);
    }
    if (valid) Serial.println(distance, 1);
    else Serial.println(F("No valid echo"));
    previousTenths = tenths;
  }
  delay(100); // HC-SR04 measurements must be at least 60 ms apart.
}

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