Distance: 60 cm · buzzer OFFTap 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 donePlace the sensor
HC-SR04 at the breadboard edge, eyes facing out, all four pins in separate rows.
Power it
VCC to 5V, GND to GND.
Connect TRIG and ECHO
TRIG to D9, ECHO to D10.
Add buzzer and LED
Buzzer + to D8 and − to GND. D7 to 220 Ω, to LED long leg, short leg to GND.
Upload and test
Upload, open the serial monitor at 9600 baud, move your hand towards the sensor.
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
- Development BoardsBuyArduino Uno R3 × 1
- SensorsBuy
- Motors & ActuatorsBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| HC-SR04 VCC | Uno 5V | Sensor power |
| HC-SR04 GND | Uno GND | Ground |
| HC-SR04 TRIG | Uno D9 | Start a measurement |
| HC-SR04 ECHO | Uno D10 | Echo pulse |
| Buzzer + | Uno D8, buzzer − to GND | Alarm |
| Uno D7 | 220 Ω, then LED anode | Alarm 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.
| Check | Expected |
|---|---|
| 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" |
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 →
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.
