Community project

Ultrasonic Distance Alert

Photo of Ultrasonic Distance Alert
Generated with AI

Kaizer Lagamon

Published December 2, 2026

This project builds a proximity alert system that monitors distance using an ultrasonic sensor and provides real-time feedback through an LCD display, colored LEDs, and an audible buzzer. As objects approach, the system transitions through different alert states, helping makers understand how to integrate multiple output devices for a complete feedback experience.

The guide includes a complete wiring diagram, parts list, and Arduino firmware that calculates distance in centimeters and triggers appropriate alerts based on configurable zones. Builders will learn how to interface the HC-SR04 distance sensor, control an I2C LCD display, drive multiple LEDs with current-limiting resistors, and generate tones with a passive piezo buzzer—all coordinated by a single Arduino Uno.

Wiring diagram

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

20x4 LCD with PCF8574 I2C backpack

Generic 5 V 20x4 character LCD with I2C backpack, address 0x27 as specified in the supplied sketch.

1

LED

Red

Standard 3mm/5mm through-hole LED. A current-limiting series resistor is added automatically.

1

LED

Green

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 ohm

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

1

Resistor

220 ohm

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

1

Resistor

220 ohm

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

1

Passive piezo buzzer (tone driven)

Small passive PS1240 piezo. Arduino tone() generates a square wave on D8 using a timer; hardware PWM pin support is not required.

Assemble it in 6 steps

1. Disconnect power first

Unplug USB before connecting wires. Use USB power for this project. All GND connections go to the Uno GND.

2. Connect the distance sensor

HC-SR04 VCC to 5V and GND to GND. TRIG to D9 sends the measurement pulse; ECHO to D10 returns the travel time.

3. Connect the screen

LCD VCC to 5V, GND to GND, SDA to A4 (data), SCL to A5 (clock). This project uses a generic 20x4 PCF8574 backpack at address 0x27, matching your sketch, not the DFR0154 module.

  • If the backlight is on but text is invisible, gently turn the small contrast screw on the backpack. Some backpacks use 0x3F instead; change the address in the code if needed.

4. Wire the three LEDs with resistors

D11 to rred, then red LED long leg. D12 to rgreen, then green LED long leg. D13 to rblue, then blue LED long leg. Each resistor is 220 ohms. Connect each LED short leg (flat edge) to GND. Resistors can face either way.

  • Never connect an LED directly to a pin without its series resistor; this can damage the LED or Uno.

5. Connect the passive piezo

Connect the piezo signal lead to D8 and its other lead to GND. The code generates a 4 kHz tone, rather than a steady voltage. Use a small passive piezo, not a magnetic buzzer or speaker.

6. Check the distance zones

After powering the finished circuit, move a flat object in front of the sensor: 20 cm or more lights red; 10 cm or less lights blue and sounds the piezo; 11 to 15 cm lights green. At 16 to 19 cm all LEDs and the piezo stay off. A missing or out-of-range echo shows an error and turns all indicators off. The sensor's useful range starts around 2 cm, so do not rely on it for safety-critical detection.

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

FunctionrredArduino
digitalP1GPIO 11
digitalP2 → LED ANODEEXT

4. Connections between "red" and "Arduino"

FunctionredArduino
groundGNDGND

5. Connections between "rgreen" and "Arduino"

FunctionrgreenArduino
digitalP1GPIO 12
digitalP2 → LED ANODEEXT

6. Connections between "green" and "Arduino"

FunctiongreenArduino
groundGNDGND

7. Connections between "rblue" and "Arduino"

FunctionrblueArduino
digitalP1GPIO 13
digitalP2 → LED ANODEEXT

8. Connections between "blue" and "Arduino"

FunctionblueArduino
groundGNDGND

9. Connections between "alarm" and "Arduino"

FunctionalarmArduino
pwmSIGGPIO 8
groundGNDGND

Deploy the firmware

#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const int trigPin = 9;
const int echoPin = 10;
const int redLed = 11;
const int greenLed = 12;
const int blueLed = 13;
const int buzzer = 8;
LiquidCrystal_I2C lcd(0x27, 20, 4);
long duration;
int distance;
int lastDistance = -999;
int lastState = -1;

// Pad each row so old letters never remain on screen.
void printRow(uint8_t row, const char *text) {
  lcd.setCursor(0, row);
  uint8_t i = 0;
  while (text[i] && i < 20) lcd.print(text[i++]);
  while (i++ < 20) lcd.print(' ');
}

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(buzzer, OUTPUT);
  digitalWrite(trigPin, LOW);
  lcd.init();
  lcd.backlight();
  printRow(0, "Distance Alert");
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH, 30000UL);
  distance = duration / 58;
  // A missing echo is not a zero-centimetre object.
  bool valid = duration > 0 && distance >= 2 && distance <= 400;
  int state;
  if (!valid) state = 0;
  else if (distance >= 20) state = 1;
  else if (distance <= 10) state = 2;
  else if (distance <= 15) state = 3;
  else state = 4; // 16 through 19 cm: all indicators off.

  if (state != lastState) {
    digitalWrite(redLed, state == 1 ? HIGH : LOW);
    digitalWrite(blueLed, state == 2 ? HIGH : LOW);
    digitalWrite(greenLed, state == 3 ? HIGH : LOW);
    if (state == 2) tone(buzzer, 4000);
    else noTone(buzzer);
    printRow(3, "");
    switch (state) {
      case 0: printRow(1, "No valid echo"); printRow(2, "Check object/sensor"); break;
      case 1: printRow(1, "The Distance is far"); printRow(2, ""); break;
      case 2: printRow(1, "The OBJECT is"); printRow(2, "TOO CLOSE"); break;
      case 3: printRow(1, "Object is Closer"); printRow(2, ""); break;
      default: printRow(1, "Safe Zone"); printRow(2, ""); break;
    }
  }
  int shownDistance = valid ? distance : -1;
  if (shownDistance != lastDistance) {
    char text[21];
    if (valid) snprintf(text, sizeof(text), "Distance: %d cm", distance);
    else snprintf(text, sizeof(text), "Distance: -- cm");
    printRow(0, text);
    lastDistance = shownDistance;
  }
  lastState = state;
  delay(500);
}

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