ESP32 Earthquake Detection System
by drahul1103 in Circuits > Arduino
2201 Views, 5 Favorites, 0 Comments
ESP32 Earthquake Detection System
This is an ESP32-based earthquake early warning system that detects ground shaking using an accelerometer and alerts you with LED and buzzer warnings at two different sensitivity levels.
The project uses an MPU6050 accelerometer to monitor vibration on all three axes. A digital filter removes gravity noise so we can detect actual shaking. When acceleration crosses a low threshold, a blinking LED activates as an early warning. If shaking gets stronger and crosses a high threshold, a passive buzzer kicks in with a sweeping alarm tone. A backup KY-002 vibration switch provides secondary confirmation of shaking.
This teaches I2C sensor communication, digital filtering, and two-stage alert logic — the same principles used in real earthquake early warning systems.
Supplies
- ESP32 development board (with USB cable)
- MPU6050 accelerometer module (I2C sensor)
- Passive buzzer module (PWM-driven, not active buzzer)
- LED (any color)
- 220Ω current-limiting resistor
- Breadboard and jumper wires
- Arduino IDE with ESP32 board support
Wire All Sensors and Outputs to ESP32
MPU6050 accelerometer (I2C):
- VCC → ESP32 3V3
- GND → ESP32 GND
- SDA → ESP32 GPIO21
- SCL → ESP32 GPIO22
LED (visual warning):
- Positive leg → 220Ω resistor → ESP32 GPIO2
- Negative leg → ESP32 GND
Passive Buzzer (audio alarm):
- Signal → ESP32 GPIO5
- VCC → ESP32 3V3
- GND → ESP32 GND
Critical notes: SDA/SCL on the MPU6050 must not be swapped. GPIO5 must be used for the buzzer (it supports PWM). The LED's longer leg is positive and goes toward GPIO2 through the resistor. Double-check all connections before uploading code.
Install Library and Upload Code
First, install the MPU6050 library: In Arduino IDE, go Sketch → Include Library → Manage Libraries, search for MPU6050, and install the version by elektronn.
Then paste this code into a new Arduino sketch and upload it to your ESP32:
Connect your ESP32 via USB, select Board: ESP32 Dev Module and the correct COM port, then click upload. Wait for "Done uploading" message.
Test the System
Once uploaded, open Arduino IDE's Serial Monitor (Tools → Serial Monitor, set to 115200 baud). You'll see real-time acceleration readings.
Gently tap or shake the breadboard near the ESP32:
- Light shaking (around 15 m/s²) → LED blinks
- Harder shaking (around 25 m/s²) → LED blinks + Buzzer alarm sounds with rising/falling tone
- Stop shaking → Both turn off within a second
Watch the Serial Monitor to see exact acceleration values. If the system doesn't respond, check that all wires are firmly seated in the breadboard and that GPIO pins match the code exactly.
Calibrate Thresholds
The default thresholds (15 m/s² for LED, 25 m/s² for buzzer) are starting points. Your setup may need different values depending on table mass and sensor placement.
While watching Serial Monitor output:
- Note the acceleration value for gentle shaking you want to trigger the LED
- Note the acceleration value for strong shaking you want to trigger the buzzer
- Set LOW_THRESHOLD to roughly 80% of your gentle value
- Set HIGH_THRESHOLD to roughly 80% of your strong value
- Re-upload and test again
You can also adjust the ALPHA in the filter value (currently 0.2): lower values (0.1) give smoother response but slower detection, higher values (0.3) give faster but noisier response.
How It Works and Next Steps
The System Logic: The MPU6050 reads acceleration hundreds of times per second. A high-pass filter (ALPHA coefficient) removes constant gravity, leaving only actual movement. When acceleration magnitude exceeds LOW_THRESHOLD, the LED blinks as a warning. If it exceeds HIGH_THRESHOLD or the vibration switch triggers, the buzzer sounds with a sweeping alarm tone. Both stay active until shaking stops.
Next Steps: You can expand this project by adding WiFi to send alerts to your phone, logging acceleration data to an SD card for analysis, or adding an OLED display to show real-time values. This same logic is used in actual seismic monitoring stations around the world.