Community project

Ultrasonic Distance Warning System

Photo of Ultrasonic Distance Warning System
Generated with AI

Kristen Palorma

Published December 3, 2026

This project builds a distance monitoring system that alerts users when objects approach too closely. Using an HC-SR04 ultrasonic sensor, the system continuously measures distance and provides real-time feedback through three color-coded LEDs, an audible buzzer, and a 20x4 LCD display showing the current distance in centimeters.

The guide includes a complete wiring diagram, parts list with recommended component values, and ready-to-upload Arduino firmware. Assembly steps cover safe power management, sensor calibration, and testing across three distance ranges to ensure the warning system responds correctly at close (under 10cm), medium (10-15cm), and far (over 20cm) distances.

Wiring diagram

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

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

Unplug USB while making connections. Use jumper wires to join every GND connection to the Uno GND. Power this circuit from USB.

2. Connect the three LEDs safely

Connect D2 through a 220 ohm resistor to the red LED long leg, D3 through a separate 220 ohm resistor to the yellow LED long leg, and D4 through a third 220 ohm resistor to the blue LED long leg. Connect all short legs (flat-sided cathodes) to GND. Green may replace yellow; white may replace blue.

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

3. Connect the ultrasonic sensor

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

4. Connect the piezo buzzer

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

5. Connect the four-line screen

Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). Check the printed labels rather than assuming the connector order. The code automatically detects the I2C backpack address. Adjust the contrast screw gently if the backlight is lit but text is invisible.

6. Test each distance range

After powering the circuit, move a broad flat object in front of the sensor. At 20 cm or more red lights; at 10 cm or less blue lights and the piezo sounds; above 10 through 15 cm yellow lights. Above 15 but below 20 cm all indicators are off because the assignment gives no rule for that interval. With no valid echo all indicators turn off and the screen reports a measurement problem. Readings below 2 cm are outside the sensor range.

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 9
groundGNDGND

8. Connections between "sonar" and "Arduino"

FunctionsonarArduino
digitalTRIGGPIO 6
digitalECHOGPIO 7
powerVCC5V
groundGNDGND

9. Connections between "lcd" and "Arduino"

FunctionlcdArduino
powerVCC → LCD power5V
groundGND → LCD groundGND
i2cSDA → LCD I2C dataGPIO 18
i2cSCL → LCD I2C clockGPIO 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 = 6, ECHO = 7, BUZZER = 9;
hd44780_I2Cexp lcd; // Automatically finds the backpack address.
bool lcdOK;

// Write only lines whose text has changed, avoiding flicker.
void line(byte row, const char *text) {
  static char old[4][21] = {{0}};
  if (!lcdOK || strcmp(old[row], text) == 0) return;
  lcd.setCursor(0, row);
  lcd.print(text);
  for (byte i = strlen(text); i < 20; i++) lcd.print(' ');
  strncpy(old[row], text, 20);
  old[row][20] = '\0';
}

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"));
  line(0, "Distance monitor");
}

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;
  bool far = valid && distance >= 20;
  bool close = valid && distance <= 10;
  bool closer = valid && distance > 10 && distance <= 15;

  digitalWrite(RED, far);
  digitalWrite(BLUE, close);
  digitalWrite(YELLOW, closer);
  static bool sounding = false;
  if (close != sounding) {
    if (close) tone(BUZZER, 4000);
    else noTone(BUZZER);
    sounding = close;
  }

  char value[21];
  if (valid) {
    char number[10];
    dtostrf(distance, 5, 1, number);
    snprintf(value, sizeof(value), "Distance: %s cm", number);
  } else strcpy(value, "Distance: no echo");
  line(1, value);
  if (!close) line(3, "");
  if (!valid) line(2, "Check object/sensor");
  else if (far) line(2, "The Distance is far");
  else if (close) {
    line(2, "The OBJECT is");
    line(3, "TOO CLOSE");
  } else if (closer) line(2, "Object is Closer");
  else line(2, "Between 15 and 20 cm");
  delay(150);
}

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