Detect Earthquakes Using Arduino: In this blog, we’ll build a simple earthquake detection system using an Arduino, MPU6050 accelerometer sensor, and HC-05 Bluetooth module. The system will detect significant movement and send the status to your phone via the BlueBot Controller app. This project is ideal for anyone looking to explore motion sensing and Bluetooth communication with Arduino.
Components Needed:
- Arduino Uno/Nano (or any compatible board)
- MPU6050 Accelerometer and Gyroscope Sensor
- HC-05 Bluetooth Module
- LED (optional for visual indication)
- Jumper Wires
- Breadboard
Circuit Connections:
- MPU6050 to Arduino:
- VCC → 5V
- GND → GND
- SCL → A5 (for Arduino Uno, adjust for other boards)
- SDA → A4 (for Arduino Uno, adjust for other boards)
- HC-05 Bluetooth Module to Arduino:
- VCC → 5V
- GND → GND
- TX → Pin 3 (via a voltage divider to lower logic level)
- RX → Pin 2
- LED (optional for status indication):
- Anode (longer leg) → Pin 13 (or built-in LED on most boards)
- Cathode (shorter leg) → GND
Full Code Explanation:
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <SoftwareSerial.h>
// Create MPU6050 instance
Adafruit_MPU6050 mpu;
// Variables for acceleration values
float ax, ay, az; // Current acceleration values
float refAx, refAy, refAz; // Reference (initial) position
float magnitude = 0.0; // Magnitude of movement
// Thresholds
const float MOVEMENT_THRESHOLD = 0.5; // Sensitivity to detect movement (adjust as needed)
// Bluetooth module connections
const int bluetoothTx = 2; // HC-05 TXD to Arduino RX (via SoftwareSerial)
const int bluetoothRx = 3; // HC-05 RXD to Arduino TX (via SoftwareSerial)
// SoftwareSerial for Bluetooth
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Initialize Bluetooth communication
bluetooth.begin(9600);
// Initialize MPU6050
if (!mpu.begin()) {
Serial.println("MPU6050 connection failed.");
while (1); // Halt execution if sensor initialization fails
}
Serial.println("MPU6050 initialized successfully.");
// Configure accelerometer settings
mpu.setAccelerometerRange(MPU6050_RANGE_2_G); // ±2g range
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); // Reduce noise with a filter
// Initialize built-in LED for status indication
pinMode(LED_BUILTIN, OUTPUT);
// Capture the initial position as the "normal" state
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
refAx = a.acceleration.x;
refAy = a.acceleration.y;
refAz = a.acceleration.z;
Serial.println("Reference position set:");
Serial.print("RefAx: "); Serial.print(refAx);
Serial.print(" RefAy: "); Serial.print(refAy);
Serial.print(" RefAz: "); Serial.println(refAz);
}
void loop() {
// Read current acceleration values
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
ax = a.acceleration.x;
ay = a.acceleration.y;
az = a.acceleration.z;
// Calculate the relative acceleration (subtract reference values)
float deltaAx = ax - refAx;
float deltaAy = ay - refAy;
float deltaAz = az - refAz;
// Calculate the magnitude of the relative acceleration vector
magnitude = sqrt(deltaAx * deltaAx + deltaAy * deltaAy + deltaAz * deltaAz);
// Check if the sensor has moved significantly
bool isMoved = magnitude > MOVEMENT_THRESHOLD;
// Determine status and LED indication
String status = "No Earthquake";
if (isMoved) {
status = "Earthquake Detected";
digitalWrite(LED_BUILTIN, HIGH); // Turn on LED
} else {
digitalWrite(LED_BUILTIN, LOW); // Turn off LED
}
// Prepare data for Bluetooth
String data = String(magnitude, 2) + ";" + status;
// Send data to Bluetooth
bluetooth.println(data);
// Print data to Serial Monitor for debugging
Serial.println(data);
// Delay for stability
delay(500);
}
Code Breakdown:
- Library Inclusions:
- We include the necessary libraries for MPU6050 and Bluetooth communication:
Wire.h
for I2C communicationAdafruit_MPU6050.h
andAdafruit_Sensor.h
for handling the sensorSoftwareSerial.h
to handle Bluetooth communication via pins 2 and 3.
- We include the necessary libraries for MPU6050 and Bluetooth communication:
- MPU6050 Initialization:
- The
mpu.begin()
function initializes the sensor. If it fails, the program halts with a message.
- The
- Reference Position:
- We store the initial position of the accelerometer as the reference (normal state), so we can detect significant changes in movement.
- Movement Detection:
- The current acceleration values are read in the
loop()
. Then, the code calculates the difference (deltaAx
,deltaAy
,deltaAz
) between the current and reference acceleration values. This gives us the relative movement. - The magnitude is computed using the formula:
sqrt(deltaAx^2 + deltaAy^2 + deltaAz^2)
- The current acceleration values are read in the
- Threshold Checking:
- If the magnitude exceeds the defined MOVEMENT_THRESHOLD, we detect an earthquake or significant motion. The LED will turn on to indicate motion.
- Bluetooth Communication:
- The system sends the magnitude and status over Bluetooth to your phone. You can adjust the sensitivity by changing the
MOVEMENT_THRESHOLD
.
- The system sends the magnitude and status over Bluetooth to your phone. You can adjust the sensitivity by changing the
Connecting to the BlueBot Controller App
Step-by-Step Setup:
- Install the BlueBot Controller App:
- Download the BlueBot Controller app from your app store (available for Android).
- Pairing the HC-05 Bluetooth Module:
- Enable Bluetooth on your phone and pair it with the HC-05 module (PIN: 1234 or 0000).
- Connect Using the App:
- Open the BlueBot Controller app, select your paired device (HC-05), and establish a connection.
- Once connected, the app will display the messages sent by the Arduino, like
"Earthquake Detected"
or"No Earthquake"
.
Adjusting Sensitivity
To make the system more or less sensitive to movement:
- Increase the
MOVEMENT_THRESHOLD
if you want to filter out small movements. - Decrease the threshold for more sensitive detection.
Conclusion
Detect Earthquakes Using Arduino: This simple Earthquake Detection System is a great introduction to motion sensing with MPU6050 and Bluetooth communication using an Arduino. You can easily adapt the code and hardware to detect other movements or vibrations for different applications. By integrating the BlueBot Controller app, you can monitor real-time data and status updates on your smartphone.
Happy coding, and feel free to explore more sensor-based projects with Arduino!