ESP32 Obstacle Alarm With Auto, Debug and Web-Controlled Manual Modes
by Ku_aman in Circuits > Microcontrollers
23 Views, 0 Favorites, 0 Comments
ESP32 Obstacle Alarm With Auto, Debug and Web-Controlled Manual Modes
In this project I built an obstacle alarm using an ESP32. An IR sensor and an HC-SR04 ultrasonic sensor measure how close an object is, and a buzzer and LED warn you with faster beeps as the object gets closer.
The code is fully non-blocking (no delay()), and the system has three modes: Auto, Debug and Manual. In Manual mode the ESP32 hosts a small web page, so you can control the alarm from your phone.
Video: https://www.youtube.com/shorts/TN3ATCxotLs
Demo Online Link: https://app.cirkitdesigner.com/project/fec9b56d-e9db-426b-89ee-a721e4b73853
Supplies
- ESP32 Dev Module
- HC-SR04 ultrasonic sensor
- IR obstacle sensor module
- Passive buzzer
- LED (any colour)
- 220 ohm resistor (for LED)
- 1k ohm and 2k ohm resistors (voltage divider for Echo pin)
- Breadboard
- Jumper wires
- USB data cable
Note : Use a passive buzzer, because the code uses tone() to make different sounds.
How the Alarm Works
The IR sensor tells if there is an object in front, and the ultrasonic sensor measures the distance. The ESP32 combines both readings and picks one of four states.
State Condition Buzzer
SAFE
- No object, distance above 80 cm
- Off
WARNING
- Object, 20 to 80 cm
- Slow beep
DANGER
- Object, 20 cm or less
- Fast beep
SENSOR_ERROR
- No echo, or the sensors disagree
- Low warning tone
The system has three modes:
- Auto: sensors control everything.
- Debug: same as Auto, but prints all sensor data.
- Manual: you set the state yourself from Serial Monitor or a web page.
Wiring the Circuit
Connect the components as shown in the diagram and table.
Component ESP32 pin
Buzzer (+)
GPIO 23
LED (+) through 220 ohm resistor
GPIO 22
IR sensor OUT
GPIO 21
HC-SR04 Trig
GPIO 19
HC-SR04 Echo
GPIO 18 (through voltage divider)
VCC / GND of sensors
Power as per module, common GND
Important: the HC-SR04 Echo pin outputs 5V, but ESP32 pins are 3.3V. Use a voltage divider (1k and 2k resistors) on the Echo pin to protect the ESP32.
Setting Up Arduino IDE for ESP32
- Install Arduino IDE.
- Open Tools → Board → Boards Manager, search "esp32" (by Espressif) and install.
- Select Tools → Board → ESP32 Dev Module.
- Connect the ESP32 and select the correct Port.
- Open Serial Monitor and set baud rate to 115200.
Note: "If the port does not show up, try another USB cable or install the CP210x / CH340 driver."
Code Part 1: Reading Sensors and Finding the State
The readSensors() function triggers the ultrasonic sensor and measures the echo time with pulseIn(). A 30 ms timeout is used, so the code never waits forever if the sensor is disconnected. Distance is calculated as duration * 0.034 / 2.
The updateState() function combines the IR reading and the distance to pick one of the four states.
Code Part 2: Non-Blocking Beeping (No Delay)
Using delay() freezes the whole program, so the sensors and serial input stop working while the buzzer beeps. Instead, this code uses millis(). Every loop it checks: has enough time passed? If yes, it toggles the buzzer and LED.
WARNING beeps every 300 ms, DANGER every 100 ms with a higher tone, and SENSOR_ERROR uses a lower tone.
Code Part 3: Auto and Debug Modes
Press a in Serial Monitor for Auto mode. It reads the sensors, runs the alarm and prints only when the state changes.
Press d for Debug mode. It prints the full IR value, echo duration and distance every 200 ms, which is useful for testing and calibration.
Code Part 4: Manual Mode With a Web Page
Press m for Manual mode. The ESP32 connects to WiFi and starts a web server. The IP address is printed in Serial Monitor. Open it in a browser on the same WiFi network (2.4 GHz).
The page has two buttons to switch to Auto or Debug mode, and four buttons to set the state: SAFE, WARNING, DANGER, SENSOR_ERROR. You can also use keys 1 to 4 in Serial Monitor.
WiFi and the web server run only in Manual mode. When you leave Manual mode, both are turned off.
For other functions if left, see the attached .ino code file.
Uploading and Testing
Upload the code. If you see "Connecting......" press the BOOT button on the ESP32 until upload starts. Open Serial Monitor at 115200 baud and test each mode.
Test: move your hand slowly towards the sensors and watch the state change from SAFE to WARNING to DANGER.
Downloads
Troubleshooting
- COM port not showing: try another cable (data cable), install CP210x/CH340 driver.
- Nothing in Serial Monitor: set baud rate to 115200 and press the EN button.
- WiFi does not connect: ESP32 supports only 2.4 GHz WiFi; check name and password.
- tone not declared: update the ESP32 board package.
- Buzzer just clicks: you may be using an active buzzer, use a passive one.
- Always SENSOR_ERROR: check Echo wiring and voltage divider, IR sensor pin.