Earthquake detection system with ESP32 and MPU-6050 accelerometer

Author: Platon Gabriel Mihai

Master: SRIC2


The purpose of this project is to make the precedent of an easy to use earthquake detector which can easily and safely be installed in any household for little cost while giving accurate details as well as the potential for scalability and upgrades. It also shows the potential of IOT devices such as the ESP32, which is used in this project.

2.Accelerometer and earthquakes

The accelerometer is a tool which measures the rate of change of velocity ( which is what accelerometer means) in its own rest frame. The gyroscope, however, measures the rational velocity which is the change of the angular position over time along the XYZ axis (roll, pitch and yaw). This means that an accelerometer on rest will measure an acceleration due to Earth’s gravity (~10m/s^2) , but if it’s falling or if it’s not hitting anything, it will measure 0m/s^2. An earthquake manifests by displacing and disrupting the ground, which causes seismic waves on the planet surface. Because of this, earthquakes will make objects have spontaneous and fast movements . Since the epicenter of an earthquake does not directly affect Earth’s gravity, it means the movement of the accelerometer will be registered and changes on it can be easily observed.

3.Project and Hardware Description

The project is made of two main parts and the peripheral parts:

The ESP32 board houses the source code, the web server as well as the link to the peripheral parts of the project.

The web server is linked to the router through wi-fi and its purpose is to gather the data directly shown by the accelerometer.

The MPU-6050 is a 6 axis motion tracking IOT device designed for low power low cost and high performance. This consists of three axis accelerometer and three axis gyroscope. Besides these, it also includes an embedded temperature sensor that can measure temperature over the range of -40 - 85 degrees Celsius in the silicon die which it incorporates.

The last peripheral is an OLED display (0.96), which also shows the data of the accelerometer so it is much easier to crosscheck.

The Esp32 board used is Node MCU ESP32 and has the following pinout:

The pins used are

* The 3.3V pin to provide power to the other components * The GND pin to complement it * The GPIO 22 and GPIO 21 pins used for SDA and SCL I2C communication with the accelerometer and with the OLED display

The accelerometer used is MPU6050 and has the following pinout:

The pins used are of course the first 4 , as explained for the esp32 board pins.

The OLED 0.96 has the following pinout and respects the same connection rules:

4.Software Description

The web server displays the accelerometer and the gyroscope values on the 3 axis on very fast intervals ( 200ms and 10ms respectively). The MPU sensor can also measure temperature, which while not being part of the general idea of earthquake detection, it’s still displayed on the web server.

The server takes the readings using Server-Sent Events. The files used for the web server implementation are organised using the SPIFFS filesystem library.

Web server folder structure

* Code * Data (folder)

  • Index.html
  • Style.css
  • Script.js

The Esp32 board creates a web server which displays in real time the data shown by the MPU6050 accelerometer.

We use the following libraries in the code: Adafruit Mpu6050, Adafruit Bus IO, Adafruit Unified Sensor These are the libraries which the MPU6050 sensor uses in the Arduino IDE environment SSD1306 This is the library that the OLED display uses in the Arduino IDE environment.

The ESP32 sends sensor readings periodically as events to the browser. When the client receives those events, we want to place the readings on the corresponding HTML elements.The gyroscope readings are a String in JSON . Once an event is instantiated , we can start listening for messages from the server. When new gyroscope readings are available, the ESP32 sends an event to the client and we need to listen to it.

For the cube 3D movement, we use a renderer based on the gyro rotation, with our accelerometer being the cube itself.

For the accelerometer and temperature reading, HTML is used to display the data on the HTML page using the said EventListener aboves.

We also need to declare the reset button, which we also need to create a function for.

After everything is done, we insert our network credentials from the WI-FI router in the code. After the code is run, we will connect and receive an IP from the subnet of the router, on which the web server will be hosted. Note that in order to access the server from a browser, we need to connect with a device which is connected to the same WI-FI and check if the IP address received by said device is also from the same subnet.

For the OLED display, we just need to check the initialization, set the cursor and get the sensor events to display properly.

5.Issues and Solutions

One of the issues that might arise is the errors of reading that can happen from different sources:

The gravitational acceleration needs to be accurate in the code in order for the web server to display the values for the 3 axis very accurately. Because of this, the elevation on which we actually run the project can influence the readings.

While the processor can handle such intense readings(milliseconds based), it might still have errors every once in a while, and even more when sending data to the web server.

Due to the reading errors, in order to detect an earthquake, the solution proposed would be to detect the difference in deviation of the gyroscope and the accelerometer based on each reading that happens in the loop. In short: we introduce a monitoring variable which records the difference between current readings and previous readings. Should said variable pass over a certain threshold, we can assume an earthquake happens. This variable needs to, once again, be documented based on the location recorded.

Most of the problems can be fixed by making the necessary adjustments and upgrading the technologies with other more powerful hardware. Nonetheless, the fact that the base, current hardware used is functioning properly shows how there is room for upgrade.


The MPU6050 and the ESP32 board are a great combination of IOT devices which make earthquake detection a simple matter through the use of an accelerometer with an included gyroscope and a temperature sensor, even if only at a minimal level. This however proves that with upgrades, the hardware can become reliable.

7.Demo and code
iothings/proiecte/2021/earthquakedetection.txt · Last modified: 2022/01/28 17:52 by gabriel.platon
CC Attribution-Share Alike 3.0 Unported Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0