Earthquake Detector & Alarm using XIAO RP2040

950 views

Overview

Earthquakes are scary natural disasters that can cause a lot of damage and danger. Imagine if there was a way to know when an earthquake might happen a little bit before it does. That would give us time to protect ourselves and our things.

In today’s tutorial, we are going to make an “Earthquake Detector” using the XIAO RP2040 Microcontroller and Vibration Sensor.

What is an Earthquake Detector?

An earthquake detector is a device designed to sense and detect movements or vibrations in the ground that occur during an earthquake. The detector can use various sensors, such as accelerometers or gyroscopes, to pick up these movements. When it senses unusual ground activity, it triggers an alarm or sends out warnings to alert people in advance about the possibility of an impending earthquake

PCBGOGO has offered high-quality PCBs and the best PCB assembly service all over the world since 2015. Discover an incredible offer that puts PCB prototyping within reach for just $1. Yes, you read that right – for only a dollar, you can bring your innovative ideas to life with a PCB prototype. Don’t miss out on this fantastic opportunity to turn your electronics projects into reality. Explore the campaign here: PCBGOGO $1 PCB Prototype and pave the way for your next big creation!

Hardware Components

You’ll need the following hardware components to get started:

ComponentsValue / ModelQty
PCBPCB-GOGO
XIAORP20401
Vibration SensorSW4201
Buzzer1
Jumper Wires1

XIAO RP2040 Pinout

XIAO-RP2040-Pinout

For details on the datasheet of the XIAO RP2040 Microcontroller visit this link.

Pin NoPin Name
1P26 / A0 / D0
2P27 / A1 / D1
3P28 / A2 / D2
4P29 / A3 / D3
5P6 / SDA/ D4
6P7 / SCL / D5
7P0 / TX / D6
8P1 / RX / CSN / D7
9P2 / SCK / D8
10P3 / MISO / D9
11P4 / MOSI / D10
123V3
13GND
145V

801s Vibration Sensor Pinout

Pin NoPin NamePin Description
1VCCVCC is the +5 volt Positive Pin
2SigSignal Output
3GNDGND is the (Ground) Negative

Steps-by-Step Guide

(1) Setting up Arduino IDE

Download Arduino IDE Software from its official site. Here is a step-by-step guide on “How to install Arduino IDE“.

(2) XIAO RP2040 in Arduino IDE

There’s an add-on that allows you to program the XIAO RP2040 using the Arduino IDE. Here is a step-by-step guide on “How to Install XIAO RP2040 on Arduino IDE“.

(3) Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

XIAO RP2040Vibration SensorBuzzer
5VVCC
GNDGNDGND
D2AO
D0+ve

(4) Uploading Code

Now copy the following code and upload it to Arduino IDE Software.

// Circuits DIY
// For Complete Details Visit -> https://www.circuits-diy.com/earthquake-detector-alarm-using-xiao-rp2040

int BUZZER = D0;      // Digital Pin 0 define Buzzer interface
int VIBRATION = D2;   // Digital Pin 2 define vibration sensor interface
int VAL;             // Define the variable as VAL
void setup()
{
Serial.begin(9600);  
pinMode(BUZZER,OUTPUT); //The defined Output as Buzzer
pinMode(VIBRATION,INPUT); //The defined input as Vibration
}
void loop()
{
VAL=digitalRead(VIBRATION);
if(VAL==HIGH)        // If the sensor detects a vibration Buzzer will be HIGH
{
Serial.print("Vibration Detected : ");  
Serial.println(VAL);
digitalWrite(BUZZER,HIGH);
delay(2000); 
}
else
{
Serial.println(VAL);   
digitalWrite(BUZZER,LOW); //if detects no vibration Buzzer stay in LOW
}
}

How code works

  • BUZZER and VIBRATION are defined as the digital pins connected to the buzzer and vibration sensor, respectively.
  • In the setup() function:
    • Serial.begin(9600) initializes serial communication at a baud rate of 9600 for debugging and outputting sensor readings.
    • pinMode() sets BUZZER as an output pin and VIBRATION as an input pin.
  • The loop() function continuously performs the following:
    • Reads the value from the vibration sensor using digitalRead(VIBRATION) and stores it in the variable VAL.
    • If VAL is HIGH (vibration detected), it prints a message to the serial monitor, turns on the buzzer (digitalWrite(BUZZER, HIGH)), and waits for 2 seconds (delay(2000)).
    • If no vibration is detected (when VAL is not HIGH), it turns off the buzzer (digitalWrite(BUZZER, LOW)).

This code essentially creates a basic earthquake detector: when the vibration sensor detects movement (like an earthquake), it triggers the buzzer to sound an alarm.

Applications

  • Early Warning System: Provides early alerts to individuals and communities about possible earthquakes.
  • Safety Measures: Allows time for people to take cover, secure belongings, or initiate emergency protocols.
  • Infrastructure Protection: Helps in safeguarding buildings, bridges, and critical infrastructure.
  • Research and Data Collection: Collect valuable data on seismic activities for scientific analysis.

Conclusion

The earthquake detector using XIAO RP2040 and a vibration sensor serves as a valuable tool for preemptively sensing seismic activities. Its ability to detect vibrations and trigger alarms offers crucial seconds or minutes for people to prepare and take safety precautions.