Community project

Ultrasonic Distance Warning System

Photo of Ultrasonic Distance Warning System
Generated with AI

Rhoda Gajeto

Published December 3, 2026

This ultrasonic distance warning system uses an HC-SR04 sensor to detect how far an object is and provides real-time feedback through LEDs, a buzzer, and an LCD display. The system categorizes distances into zones: far (20cm+), medium (15-20cm), close (10-15cm), and critical (under 10cm), with the buzzer sounding an alarm when an object gets too close.

The guide includes a complete wiring diagram showing how to connect the ultrasonic sensor, three indicator LEDs, a piezo buzzer, and a 20x4 I2C LCD display to an Arduino Uno. Builders will receive the full parts list, step-by-step assembly instructions, and ready-to-upload firmware that handles sensor readings, distance calculations, and multi-output alerts.

Wiring diagram

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

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

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)

Assemble it in 3 steps

1. Connect the three LEDs with USB unplugged

Connect D2, D3 and D4 through separate 220 ohm resistors to the long legs of the red, yellow and blue LEDs respectively. Connect each short leg to Uno GND.

2. Wire the ultrasonic sensor and buzzer

Connect sensor VCC to 5V, GND to GND, TRIG to D6 and ECHO to D7. Connect the passive piezo positive lead to D5 and its other lead to GND.

3. Wire the four-line LCD

Connect VCC to 5V, GND to GND, SDA to A4 and SCL to A5. If the backlight is on but text is hard to see, gently adjust the contrast screw on the LCD backpack.

Review all connections

1. Connections between "sonar" and "Arduino"

FunctionsonarArduino
powerVCC5V
groundGNDGND
digitalTRIGGPIO 6
digitalECHOGPIO 7

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

FunctionrRedArduino
digitalP1GPIO 2

5. Connections between "red" and "Arduino"

FunctionredArduino
digitalANODE → Resistor P2EXT
groundGNDGND

6. Connections between "rYellow" and "Arduino"

FunctionrYellowArduino
digitalP1GPIO 3

7. Connections between "yellow" and "Arduino"

FunctionyellowArduino
digitalANODE → Resistor P2EXT
groundGNDGND

8. Connections between "rBlue" and "Arduino"

FunctionrBlueArduino
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 int RED = 2, YELLOW = 3, BLUE = 4, BUZZER = 5;
const int TRIG = 6, ECHO = 7;
int previous = -1;
bool lcdOK;

void setup() {
  for (int pin = RED; pin <= TRIG; pin++) pinMode(pin, OUTPUT);
  pinMode(ECHO, INPUT);
  lcdOK = (lcd.begin(20, 4) == 0);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long echo = pulseIn(ECHO, HIGH, 25000);
  float cm = echo / 58.0;

  int state = 0; // No echo
  if (echo != 0) {
    if (cm >= 20) state = 1;
    else if (cm <= 10) state = 2;
    else if (cm <= 15) state = 3;
    else state = 4; // Above 15 but below 20 cm
  }

  if (state != previous) {
    previous = state;
    digitalWrite(RED, state == 1);
    digitalWrite(BLUE, state == 2);
    digitalWrite(YELLOW, state == 3);
    if (state == 2) tone(BUZZER, 4000);
    else noTone(BUZZER);

    if (lcdOK) {
      lcd.clear();
      if (state == 0) lcd.print(F("No echo"));
      if (state == 1) lcd.print(F("The Distance is far"));
      if (state == 2) {
        lcd.print(F("The OBJECT is"));
        lcd.setCursor(0, 1);
        lcd.print(F("TOO CLOSE"));
      }
      if (state == 3) lcd.print(F("Object is Closer"));
      if (state == 4) lcd.print(F("Between 15 and 20 cm"));
    }
  }
  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