Community project

Ultrasonic Distance Alert System

Photo of Ultrasonic Distance Alert System
Generated with AI

Angelica Magno

Published November 23, 2026

This project builds an ultrasonic distance alert system that monitors how close an object is and provides real-time feedback through multiple indicators. The HC-SR04 sensor measures distance, while an Arduino Uno processes the data and controls three LEDs (red, yellow, blue), a piezo buzzer, and a 20x4 LCD display to show the current distance state.

Builders will receive a complete wiring diagram showing how to connect the distance sensor, LCD display via I2C, indicator LEDs, and buzzer to the Arduino. The guide includes a full parts list, step-by-step assembly instructions, and ready-to-upload firmware that triggers different alerts based on distance thresholds—perfect for proximity detection, parking assistance, or object avoidance applications.

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

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

LED

Red

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

LED

Yellow

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

LED

Blue

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)

Assemble it in 3 steps

1. Connect the distance sensor and screen

With USB unplugged, connect both modules to 5V and GND. Connect sensor TRIG to D8 and ECHO to D9. Connect LCD SDA to A4 and SCL to A5.

  • Check the printed pin labels rather than assuming the pins are in the same order.

2. Connect the three lights

Run D2 to the red LED, D3 to the yellow or green LED, and D4 to the blue or white LED. Put one 220 ohm resistor between each Arduino pin and its LED long leg. Connect each short leg to GND.

3. Connect the passive piezo

Connect the positive lead to D5 and the negative lead to GND.

Review all connections

1. Connections between "sonar" and "Arduino"

FunctionsonarArduino
powerVCC5V
groundGNDGND
digitalTRIGGPIO 8
digitalECHOGPIO 9

2. Connections between "lcd" and "Arduino"

FunctionlcdArduino
powerVCC5V
groundGNDGND
i2cSDAGPIO 18
i2cSCLGPIO 19

3. Connections between "piezo" and "Arduino"

FunctionpiezoArduino
pwmSIGGPIO 5
groundGNDGND

4. Connections between "r2" and "Arduino"

Functionr2Arduino
digitalP1GPIO 2

5. Connections between "red" and "Arduino"

FunctionredArduino
digitalANODE → Resistor P2EXT
groundGNDGND

6. Connections between "r3" and "Arduino"

Functionr3Arduino
digitalP1GPIO 3

7. Connections between "yellow" and "Arduino"

FunctionyellowArduino
digitalANODE → Resistor P2EXT
groundGNDGND

8. Connections between "r4" and "Arduino"

Functionr4Arduino
digitalP1GPIO 4

9. Connections between "blue" and "Arduino"

FunctionblueArduino
digitalANODE → Resistor P2EXT
groundGNDGND

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, BUZZER = 5;
const byte TRIG = 8, ECHO = 9;
enum State { NO_ECHO, TOO_CLOSE, CLOSER, IN_BETWEEN, FAR };
int previous = -1;
bool lcdReady;

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  lcdReady = (lcd.begin(20, 4) == 0);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long echoTime = pulseIn(ECHO, HIGH, 30000UL);
  float cm = echoTime / 58.0f;

  State state;
  if (echoTime == 0) state = NO_ECHO;
  else if (cm >= 20) state = FAR;
  else if (cm <= 10) state = TOO_CLOSE;
  else if (cm <= 15) state = CLOSER;
  else state = IN_BETWEEN;

  if (state != previous) {
    previous = state;
    digitalWrite(RED, state == FAR);
    digitalWrite(YELLOW, state == CLOSER);
    digitalWrite(BLUE, state == TOO_CLOSE);
    if (state == TOO_CLOSE) tone(BUZZER, 2500);
    else noTone(BUZZER);

    if (lcdReady) {
      lcd.clear();
      lcd.setCursor(0, 0);
      switch (state) {
        case FAR: lcd.print("The Distance is far"); break;
        case CLOSER: lcd.print("Object is Closer"); break;
        case TOO_CLOSE:
          lcd.print("The OBJECT is");
          lcd.setCursor(0, 1);
          lcd.print("TOO CLOSE");
          break;
        case IN_BETWEEN: lcd.print("Between 15 and 20 cm"); break;
        case NO_ECHO: lcd.print("No echo received"); break;
      }
    }
  }
  delay(80);
}

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