Projects / Ultrasonic distance alert

Ultrasonic distance alert

Beginner45 minGuided buildReviewed
Arduino Uno5VD9D10D8D7GNDHC-SR04BuzzerAlarm LED
Distance: 60 cm · buzzer OFF

Tap a part to see what it does.

Tap the parts to see what each one does. A teaching simulation, not a circuit simulator.

Build it step by step

0 of 5 done
  1. UNO106 cm

    Place the sensor

    HC-SR04 at the breadboard edge, eyes facing out, all four pins in separate rows.

  2. UNO

    Power it

    VCC to 5V, GND to GND.

  3. UNO

    Connect TRIG and ECHO

    TRIG to D9, ECHO to D10.

  4. UNO16.5 mA

    Add buzzer and LED

    Buzzer + to D8 and − to GND. D7 to 220 Ω, to LED long leg, short leg to GND.

  5. UNO

    Upload and test

    Upload, open the serial monitor at 9600 baud, move your hand towards the sensor.

In short

How do you build an ultrasonic distance alert?

An HC-SR04 measures distance; under 20 cm a buzzer beeps and an LED lights. You need Arduino Uno R3, HC-SR04 Ultrasonic Sensor, Active buzzer 5 V, LED 5 mm red, Resistor 220 Ω and Breadboard 830 Tie Points. It takes 5 steps and about 45 minutes.

Level
Beginner
Time
About 45 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
HC-SR04 VCCUno 5VSensor power
HC-SR04 GNDUno GNDGround
HC-SR04 TRIGUno D9Start a measurement
HC-SR04 ECHOUno D10Echo pulse
Buzzer +Uno D8, buzzer − to GNDAlarm
Uno D7220 Ω, then LED anodeAlarm light

Code

Arduino IDE, board "Arduino Uno". No extra libraries. Use an active buzzer (it beeps on its own); a passive one needs tone().

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int TRIG = 9, ECHO = 10, BUZZER = 8, LED = 7;
const int ALERT_CM = 20;

void setup() {
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
  pinMode(BUZZER, OUTPUT); pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(TRIG, LOW); delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long us = pulseIn(ECHO, HIGH, 30000UL);  // 30 ms timeout, about 5 m
  if (us == 0) { Serial.println("No echo"); delay(100); return; }
  float cm = us / 58.0;
  bool alert = cm < ALERT_CM;
  digitalWrite(BUZZER, alert);
  digitalWrite(LED, alert);
  Serial.print("Distance: "); Serial.print(cm, 1); Serial.println(" cm");
  delay(100);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Uno with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Distance: 80.5 cm
Distance: 80.2 cm
Distance: 80.4 cm
Distance: 80.6 cm
Distance: 80.3 cm
Distance: 80.4 cm

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Wall 50 cm awayDistance about 50 cm (±2 cm)
Hand at 10 cmBuzzer beeps, LED on
Nothing in frontLarge value or "No echo"

If it does not work

Always 0 or "No echo"

Check TRIG and ECHO are not swapped and VCC is 5V.

Readings jump around

Soft or angled surfaces scatter sound: test against a flat wall.

Buzzer silent

It may be a passive buzzer: use tone(BUZZER, 2000) instead.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ultrasonic distance alert?

Arduino Uno R3, HC-SR04 Ultrasonic Sensor, Active buzzer 5 V, LED 5 mm red, Resistor 220 Ω and Breadboard 830 Tie Points.

How long does the ultrasonic distance alert take to build?

About 45 minutes for a beginner maker, in 5 steps.

How do I know the ultrasonic distance alert works?

Wall 50 cm away: Distance about 50 cm (±2 cm). Hand at 10 cm: Buzzer beeps, LED on. Nothing in front: Large value or "No echo".

Always 0 or "No echo: what should I check?

Check TRIG and ECHO are not swapped and VCC is 5V.

Readings jump around: what should I check?

Soft or angled surfaces scatter sound: test against a flat wall.

Buzzer silent: what should I check?

It may be a passive buzzer: use tone(BUZZER, 2000) instead.