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?

 

 
 
ComponentWhat It Does
HC-SR04 Ultrasonic SensorShoots sound waves, measures how long they take to bounce back
ESP32The 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:

  1. Sensor shoots an ultrasonic pulse (you can’t hear it)

  2. Pulse hits an object and bounces back

  3. ESP32 measures the time

  4. Distance = (Time × Speed of Sound) / 2

  5. If distance < 30cm → Red LED + Buzzer

  6. If distance > 30cm → Blue LED + Peace

ESP32 distance alarm circuit diagram using HC-SR04 ultrasonic sensor, LEDs, and a buzzer on a breadboard.

Why ESP32 Distance Alarm System is Perfect for Beginners

 
 
ReasonWhy It Matters
Only 4 componentsESP32, sensor, 2 LEDs, buzzer (optional)
No soldering requiredBreadboard and jumper wires only
Teaches real physicsSpeed of sound, time-of-flight measurement
Instant feedbackLEDs light up – you see it working immediately
UsefulDesk 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

 
 
ComponentSpecsApprox Price (USD)
ESP32 Development BoardAny version$5.00 – $6.70
HC-SR04 Ultrasonic Sensor5V, 2cm-400cm range$1.25 – $2.10
Red LED5mm, any brightness$0.05 – $0.10
Blue LED5mm, any brightness$0.05 – $0.10
220Ω Resistors (2x)For LEDs$0.02 – $0.05 each
Passive Buzzer (Optional)5V$0.25 – $0.45
Breadboard400 points$0.85 – $1.25
Jumper WiresMale-to-female$0.40 – $0.85

Total (without buzzer): ~$8.00 – $10.50 USD – less than a pizza!

Components for an ESP32 distance alarm including an ESP32 development board on a breadboard, HC-SR04 ultrasonic sensor, LEDs, and jumper wires.
 
 
ComponentWhere to Find
ESP32 Development BoardAliExpress
HC-SR04 Ultrasonic SensorAliExpress
LED Kit (Various Colors)AliExpress
220Ω Resistors (Pack)AliExpress
Passive BuzzerAliExpress
Breadboard + Jumper KitAliExpress

Wiring ESP32 Distance Alarm System Up (10 Minutes)

HC-SR04 Ultrasonic Sensor

 
 
Sensor PinESP32 PinNotes
VCC5VNeeds 5V power
GNDGNDCommon ground
TRIGGPIO 5Send trigger pulse
ECHOGPIO 18Receive 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

 
 
LEDESP32 PinThrough Resistor
Red LED Anode (+)GPIO 19220Ω resistor
Red LED Cathode (-)GNDDirect
Blue LED Anode (+)GPIO 21220Ω resistor
Blue LED Cathode (-)GNDDirect

Buzzer for ESP32 Distance Alarm System (Optional)

 
 
Buzzer PinESP32 Pin
Positive (+)GPIO 22
Negative (-)GND
Completed ESP32 distance alarm prototype on a breadboard with a glowing blue LED and HC-SR04 ultrasonic sensor.

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:

  1. TRIGGER: ESP32 tells sensor “SCREAM NOW!”

  2. SCREAM: Sensor shoots 8 ultrasonic pulses (40kHz – too high for human ears)

  3. ECHO: Sound hits object and bounces back

  4. LISTEN: Sensor measures how long the echo took

  5. 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)

 
 
MediumSpeed
Air (20°C)343 m/s
Air (0°C)331 m/s
Water1480 m/s
Steel5000 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

 
 
DistanceRed LEDBlue LEDBuzzer
< 30 cmONOFFBeeping
30-400 cmOFFONSilent
> 400 cmOFFOFFSilent

Troubleshooting for ESP32 Distance Alarm System

 
 
ProblemLikely CauseFix
Distance always 0ECHO pin not connectedCheck GPIO 18 connection
Distance always 400cmNo echo receivedSensor facing wrong way? Object too far?
LEDs not workingWrong polarity or missing resistorCheck anode (+) to ESP32, cathode (-) to GND
Buzzer not workingPassive vs active confusionUse tone() for passive buzzer
Serial shows random numbersPower fluctuationAdd 100µF capacitor across 5V and GND

Make It Your Own (Upgrades)

 
 
UpgradeWhat You’ll NeedDifficulty
Multiple zonesAdd yellow LED for “warning” zone (30-50cm)⭐ Easy
LCD display16×2 I2C LCD – show exact distance⭐⭐ Medium
Servo reactionSG90 servo – point at the intruder⭐⭐ Medium
Data loggingSD card module – log distances over time⭐⭐⭐ Hard
Remote alertLoRa module – send alert to another room⭐⭐⭐ Hard

Real-World Applications for ESP32 Distance Alarm System

 
 
Use CaseHow It Works
Parking sensorMount on your car bumper – beeps when too close to wall
Social distancing monitorAlerts when someone gets within 6 feet
Desk intrusion alarmProtects your snacks from roommates
Liquid level sensorPoint at a water tank – measures fill level
Robot obstacle avoidanceYour robot can “see” walls and turn away

What You Learned from ESP32 Distance Alarm System

 
 
ConceptWhy It Matters
Ultrasonic sensorsMeasure distance without touching anything
pulseIn() functionMeasures time of electrical pulses
Speed of sound calculationReal physics in your code
LED indicatorsVisual 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 processorMore accurate timing
Built-in Bluetooth/WiFiAdd remote notifications later
More pinsRoom to expand
Still beginner-friendlySame 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:

Read roborear 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!

Subscribe to The Newsletter

Join robotics enthusiasts getting weekly project ideas:

• 🔧 Under $50 projects

• ⚙️ Under $100 projects

• 🎥 New video tutorials

• 📝 Blog updates

We don’t spam! Read our privacy policy for more info.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *