Community project

Ultrasonic Distance Alert

Photo of Ultrasonic Distance Alert
Generated with AI

Angel Trecia Mae Datoon

Published December 3, 2026

This project builds a distance monitoring system that uses an ultrasonic sensor to detect how far an object is from the Arduino, then displays the measurement on an LCD screen and provides visual and audio alerts based on distance thresholds. The system uses three LEDs and a piezo buzzer to indicate different distance ranges, making it useful for parking assistance, object detection, or proximity warnings.

The guide includes a complete wiring diagram showing how to connect the HC-SR04 ultrasonic sensor, I2C LCD display, LEDs, and piezo buzzer to the Arduino Uno, a full parts list with component specifications, the complete Arduino firmware with distance calculation and alert logic, and step-by-step assembly instructions to get the project working.

Wiring diagram

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 5 steps

1. Prepare with power disconnected

Unplug USB before wiring. Add three 220 ohm resistors to the supplied materials; never connect an LED directly to an Arduino output. Use jumper wires and a solderless breadboard if available.

2. Connect the three LEDs

D2 goes through rred to the red LED long leg. D3 goes through ryellow to the yellow (or green) LED long leg. D4 goes through rblue to the blue (or white) LED long leg. Connect each short leg (flat-edge side) to Arduino GND. Resistors work either way around.

  • Missing series resistors can damage the LEDs or Arduino pins.

3. Connect the distance sensor

Follow the printed HC-SR04 labels: VCC to 5V, GND to GND, TRIG to D7 (start measurement), ECHO to D8 (return timing). Aim its two round transducers toward the object.

4. Connect the LCD

Connect LCD VCC to 5V, GND to GND, SDA to A4 (data), and SCL to A5 (clock). This is a 20-column, four-row LCD with an I2C backpack. Adjust the small contrast screw gently if characters are invisible.

5. Connect the passive piezo

Connect one piezo lead to D9 (sound signal) and the other to GND. Use a small passive piezo, not a high-current magnetic buzzer. Check that all ground connections join Arduino GND and power wires are not swapped.

Review all connections

1. Connections between "sonar" and "Arduino"

FunctionsonarArduino
powerVCC5V
groundGNDGND
digitalTRIGGPIO 7
digitalECHOGPIO 8

2. Connections between "lcd" and "Arduino"

FunctionlcdArduino
powerVCC5V
groundGNDGND
i2cSDAGPIO 18
i2cSCLGPIO 19

3. Connections between "buzzer" and "Arduino"

FunctionbuzzerArduino
pwmSIGGPIO 9
groundGNDGND

4. Connections between "rred" and "Arduino"

FunctionrredArduino
digitalP1GPIO 2
digitalP2 → LED ANODEEXT

5. Connections between "red" and "Arduino"

FunctionredArduino
groundGNDGND

6. Connections between "ryellow" and "Arduino"

FunctionryellowArduino
digitalP1GPIO 3
digitalP2 → LED ANODEEXT

7. Connections between "yellow" and "Arduino"

FunctionyellowArduino
groundGNDGND

8. Connections between "rblue" and "Arduino"

FunctionrblueArduino
digitalP1GPIO 4
digitalP2 → LED ANODEEXT

9. Connections between "blue" and "Arduino"

FunctionblueArduino
groundGNDGND

Deploy the firmware

#include <Arduino.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <string.h>

const byte RED = 2, YELLOW = 3, BLUE = 4;
const byte TRIG = 7, ECHO = 8, BUZZER = 9;
hd44780_I2Cexp lcd; // Automatically detect LCD address and backpack wiring.
bool lcdReady = false;
char previous[4][21] = {};

// Update only LCD rows whose text changed; pad to erase old characters.
void showRow(byte row, const char *text) {
  char padded[21];
  memset(padded, ' ', 20);
  padded[20] = '\0';
  size_t n = strlen(text);
  if (n > 20) n = 20;
  memcpy(padded, text, n);
  if (lcdReady && strcmp(previous[row], padded) != 0) {
    lcd.setCursor(0, row);
    lcd.print(padded);
    strcpy(previous[row], padded);
  }
}

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);
  lcdReady = (lcd.begin(20, 4) == 0);
  if (!lcdReady) Serial.println(F("LCD not found: check wiring and contrast."));
}

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.0f;
  bool valid = duration != 0 && distance >= 2 && distance <= 400;

  byte state = 0; // Undefined 15-20 cm range: all outputs off.
  const char *line1 = "Between 15 and 20 cm";
  const char *line2 = "";
  if (!valid) {
    line1 = "No valid echo";
    line2 = "Check target/sensor";
  } else if (distance >= 20) {
    state = 1;
    line1 = "The Distance is far";
  } else if (distance <= 10) {
    state = 3;
    line1 = "The OBJECT is";
    line2 = "TOO CLOSE";
  } else if (distance <= 15) {
    state = 2;
    line1 = "Object is Closer";
  }

  digitalWrite(RED, state == 1);
  digitalWrite(YELLOW, state == 2);
  digitalWrite(BLUE, state == 3);
  static byte lastState = 255;
  if (state != lastState) {
    if (state == 3) tone(BUZZER, 4000);
    else noTone(BUZZER);
    lastState = state;
  }

  char measurement[21];
  if (valid) {
    char number[10];
    dtostrf(distance, 5, 1, number);
    snprintf(measurement, sizeof(measurement), "Distance: %s cm", number);
    Serial.println(measurement);
  } else {
    strcpy(measurement, "Distance: -- cm");
  }
  showRow(0, measurement);
  showRow(1, line1);
  showRow(2, line2);
  showRow(3, "");
  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