This $5 Sensor Knows When You’re Too Close (ESP32 Distance Alarm System)
The “Back Off!” Moment
Imagine this.
You’re working on your laptop. Someone walks too close to your desk.
BEEP. RED LIGHT. WARNING. ESP32 Distance Alarm.
They step back.
Silence. Blue light. Peace.
This isn’t a scene from a spy movie. It’s a $5 sensor, an ESP32, and two LEDs.
I built an ESP32 Distance Alarm that watches a zone and reacts when someone gets too close. No camera. No internet. Just ultrasonic sound waves and a few lines of code.
And you can build it in 20 minutes.
What Are We Building?
ESP32 Distance Alarm System?
| Component | What It Does |
|---|---|
| HC-SR04 Ultrasonic Sensor | Shoots sound waves, measures how long they take to bounce back |
| ESP32 | The brain – calculates distance, decides what to do |
| Red LED | “DANGER ZONE” – you’re too close! |
| Blue LED | “Safe Zone” – you’re good |
| Buzzer (Optional) | Makes noise when someone crosses the line |
How it works:
Sensor shoots an ultrasonic pulse (you can’t hear it)
Pulse hits an object and bounces back
ESP32 measures the time
Distance = (Time × Speed of Sound) / 2
If distance < 30cm → Red LED + Buzzer
If distance > 30cm → Blue LED + Peace

Why ESP32 Distance Alarm System is Perfect for Beginners
| Reason | Why It Matters |
|---|---|
| Only 4 components | ESP32, sensor, 2 LEDs, buzzer (optional) |
| No soldering required | Breadboard and jumper wires only |
| Teaches real physics | Speed of sound, time-of-flight measurement |
| Instant feedback | LEDs light up – you see it working immediately |
| Useful | Desk alarm, parking sensor, social distancing monitor |
For a BUET first-year student? This ESP32 Distance Alarm System is the perfect weekend project.
What You’ll Need for ESP32 Distance Alarm System
Components
| Component | Specs | Approx Price (USD) |
|---|---|---|
| ESP32 Development Board | Any version | $5.00 – $6.70 |
| HC-SR04 Ultrasonic Sensor | 5V, 2cm-400cm range | $1.25 – $2.10 |
| Red LED | 5mm, any brightness | $0.05 – $0.10 |
| Blue LED | 5mm, any brightness | $0.05 – $0.10 |
| 220Ω Resistors (2x) | For LEDs | $0.02 – $0.05 each |
| Passive Buzzer (Optional) | 5V | $0.25 – $0.45 |
| Breadboard | 400 points | $0.85 – $1.25 |
| Jumper Wires | Male-to-female | $0.40 – $0.85 |
Total (without buzzer): ~$8.00 – $10.50 USD – less than a pizza!

Quick Buy Links
| Component | Where to Find |
|---|---|
| ESP32 Development Board | AliExpress |
| HC-SR04 Ultrasonic Sensor | AliExpress |
| LED Kit (Various Colors) | AliExpress |
| 220Ω Resistors (Pack) | AliExpress |
| Passive Buzzer | AliExpress |
| Breadboard + Jumper Kit | AliExpress |
Wiring ESP32 Distance Alarm System Up (10 Minutes)
HC-SR04 Ultrasonic Sensor
| Sensor Pin | ESP32 Pin | Notes |
|---|---|---|
| VCC | 5V | Needs 5V power |
| GND | GND | Common ground |
| TRIG | GPIO 5 | Send trigger pulse |
| ECHO | GPIO 18 | Receive echo pulse |
How the sensor works:
TRIG pin sends a 10µs HIGH pulse
Sensor shoots 8 ultrasonic bursts
Sound bounces back
ECHO pin goes HIGH for the time it took
ESP32 measures that time
LEDs
| LED | ESP32 Pin | Through Resistor |
|---|---|---|
| Red LED Anode (+) | GPIO 19 | 220Ω resistor |
| Red LED Cathode (-) | GND | Direct |
| Blue LED Anode (+) | GPIO 21 | 220Ω resistor |
| Blue LED Cathode (-) | GND | Direct |
Buzzer for ESP32 Distance Alarm System (Optional)
| Buzzer Pin | ESP32 Pin |
|---|---|
| Positive (+) | GPIO 22 |
| Negative (-) | GND |

The Science Stuff for ESP32 Distance Alarm System (But Make It Fun)
How Ultrasonic Sensors Work
Think of it like a bat or a dolphin. They scream, listen for the echo, and know how far away things are.
The HC-SR04 does the same thing:
TRIGGER: ESP32 tells sensor “SCREAM NOW!”
SCREAM: Sensor shoots 8 ultrasonic pulses (40kHz – too high for human ears)
ECHO: Sound hits object and bounces back
LISTEN: Sensor measures how long the echo took
CALCULATE: Distance = (Time × 343 m/s) / 2
Why divide by 2? The sound goes there AND back. We only want the “there” distance.
Speed of Sound (The Only Math You Need)
| Medium | Speed |
|---|---|
| Air (20°C) | 343 m/s |
| Air (0°C) | 331 m/s |
| Water | 1480 m/s |
| Steel | 5000 m/s |
For room temperature, we use 0.034 cm/µs (centimeters per microsecond).
So: Distance (cm) = (Time in µs × 0.034) / 2
The code does this for you. No math degree required.
The Code (Copy-Paste Ready) ESP32 Distance Alarm System
Full code available on GitHub: [ESP32 Distance Alarm System]
How to Test It
Step 1: Upload the code to ESP32
Step 2: Open Serial Monitor (115200 baud)
Step 3: Wave your hand in front of the sensor
Step 4: Watch the LEDs and Serial Monitor
| Distance | Red LED | Blue LED | Buzzer |
|---|---|---|---|
| < 30 cm | ON | OFF | Beeping |
| 30-400 cm | OFF | ON | Silent |
| > 400 cm | OFF | OFF | Silent |
Troubleshooting for ESP32 Distance Alarm System
| Problem | Likely Cause | Fix |
|---|---|---|
| Distance always 0 | ECHO pin not connected | Check GPIO 18 connection |
| Distance always 400cm | No echo received | Sensor facing wrong way? Object too far? |
| LEDs not working | Wrong polarity or missing resistor | Check anode (+) to ESP32, cathode (-) to GND |
| Buzzer not working | Passive vs active confusion | Use tone() for passive buzzer |
| Serial shows random numbers | Power fluctuation | Add 100µF capacitor across 5V and GND |
Make It Your Own (Upgrades)
| Upgrade | What You’ll Need | Difficulty |
|---|---|---|
| Multiple zones | Add yellow LED for “warning” zone (30-50cm) | ⭐ Easy |
| LCD display | 16×2 I2C LCD – show exact distance | ⭐⭐ Medium |
| Servo reaction | SG90 servo – point at the intruder | ⭐⭐ Medium |
| Data logging | SD card module – log distances over time | ⭐⭐⭐ Hard |
| Remote alert | LoRa module – send alert to another room | ⭐⭐⭐ Hard |
Real-World Applications for ESP32 Distance Alarm System
| Use Case | How It Works |
|---|---|
| Parking sensor | Mount on your car bumper – beeps when too close to wall |
| Social distancing monitor | Alerts when someone gets within 6 feet |
| Desk intrusion alarm | Protects your snacks from roommates |
| Liquid level sensor | Point at a water tank – measures fill level |
| Robot obstacle avoidance | Your robot can “see” walls and turn away |
What You Learned from ESP32 Distance Alarm System
| Concept | Why It Matters |
|---|---|
| Ultrasonic sensors | Measure distance without touching anything |
pulseIn() function | Measures time of electrical pulses |
| Speed of sound calculation | Real physics in your code |
| LED indicators | Visual feedback for users |
| Threshold logic | “If this, then that” decision making |
The ESP32 Advantage for ESP32 Distance Alarm System
| Why ESP32 Instead of Arduino Uno? | Benefit |
|---|---|
| Faster processor | More accurate timing |
| Built-in Bluetooth/WiFi | Add remote notifications later |
| More pins | Room to expand |
| Still beginner-friendly | Same Arduino code works |
If you only have an Arduino Uno, the code works exactly the same – just change the pin numbers.
Project Demo- ESP32 Distance Alarm System
Read other blogs:
Your Turn
This ESP32 Distance Alarm System project taught me that you don’t need expensive sensors to do cool things. A $5 ultrasonic sensor, two LEDs, and an ESP32 – that’s it.
Now go build it. Then break it. Then fix it and make it better.
That’s how we learn.
FAQs
Affiliate Disclosure
Some links in this ESP32 Distance Alarm System post are affiliate links. If you purchase through them, I may earn a small commission at no extra cost to you. This helps support Roborear. Thanks!






