Community project

Ultrasonic Distance Alert System

Photo of Ultrasonic Distance Alert System
Generated with AI

kathleenlorainefabillo-sudo

Published December 2, 2026

This project builds a distance monitoring system using an ultrasonic sensor to detect how far an object is and provide real-time visual and audio feedback. The HC-SR04 sensor measures distance, triggering different colored LEDs and a piezo buzzer to alert when objects enter danger zones, with all readings displayed on a 20x4 LCD screen.

The guide includes a complete wiring diagram showing how to connect the ultrasonic sensor, LEDs with current-limiting resistors, buzzer, and I2C LCD display to an Arduino Uno. Assembly steps walk through the setup process, and the provided firmware handles distance calculations, state management, and multi-zone alerts so the system is ready to deploy after uploading the code.

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

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

330 ohm

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

1

Resistor

330 ohm

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

1

Resistor

330 ohm

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. Disconnect power

Unplug USB before connecting wires. Use jumper wires and a solderless breadboard for the laboratory build.

2. Connect the LEDs with resistors

Connect D2 through a 330 ohm resistor to the red LED's long leg; D3 through its own 330 ohm resistor to the yellow (or green) LED's long leg; D4 through its own 330 ohm resistor to the blue (or white) LED's long leg. Connect each short leg (flat side) to GND.

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

3. Connect the distance sensor

Connect HC-SR04 VCC to 5V, GND to GND, TRIG to D6 (trigger output), and ECHO to D7 (distance pulse input). Follow printed pin labels.

4. Connect the piezo buzzer

Connect the passive piezo signal lead to D5 and the other lead to GND. If marked, use plus for D5 and minus for GND.

5. Connect the LCD

Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). All grounds must be joined. Adjust the LCD contrast screw gently if text is invisible.

6. Check the laboratory setup

Check for reversed power and short circuits before plugging in USB. Aim the sensor at a flat object and test at 8, 12, 17, and 25 cm. The sensor measures about 2 to 400 cm; missing echoes are not proof that an object is far away.

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 "buzzer" and "Arduino"

FunctionbuzzerArduino
pwmSIGGPIO 5
groundGNDGND

8. Connections between "sonar" and "Arduino"

FunctionsonarArduino
digitalTRIGGPIO 6
digitalECHOGPIO 7
powerVCC5V
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 BUZZER = 5, TRIG = 6, ECHO = 7;
hd44780_I2Cexp lcd;  // Automatically finds the LCD address.
bool lcdOK;
int previousDistance = -2, previousState = -1;

// Pad each row to remove old text, without clearing the screen.
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) 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 cm = duration / 58.0f;
  bool valid = duration != 0 && cm >= 2 && cm <= 400;
  int state;
  if (!valid) state = 0;
  else if (cm >= 20) state = 1;
  else if (cm <= 10) state = 2;
  else if (cm <= 15) state = 3;
  else state = 4;  // Unspecified range: more than 15, less than 20.

  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 distance = valid ? (int)(cm + 0.5f) : -1;
  if (lcdOK && (distance != previousDistance || state != previousState)) {
    char line[21];
    if (valid) snprintf(line, sizeof(line), "Distance: %d cm", distance);
    else snprintf(line, sizeof(line), "Distance: --");
    row(0, line);
    if (state == 1) { row(1, "The Distance is far"); row(2, ""); }
    else if (state == 2) { row(1, "The OBJECT is"); row(2, "TOO CLOSE"); }
    else if (state == 3) { row(1, "Object is Closer"); row(2, ""); }
    else if (state == 4) { row(1, "Object approaching"); row(2, "15 < distance < 20"); }
    else { row(1, "No valid echo"); row(2, "Check object/sensor"); }
    row(3, state == 2 ? "Buzzer: ON" : "Buzzer: OFF");
  }
  previousDistance = distance;
  previousState = state;
  delay(100); // HC-SR04 needs at least 60 ms between 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