0% found this document useful (0 votes)
172 views

Arduino Earthquake Code

This code is initializing an MPU6050 accelerometer sensor and checking its settings. It is reading the raw and normalized acceleration values from the sensor in the loop. If the normalized acceleration exceeds thresholds, it triggers an earthquake detection by lighting LEDs and displaying a message on an LCD.

Uploaded by

kimchen edenelle
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views

Arduino Earthquake Code

This code is initializing an MPU6050 accelerometer sensor and checking its settings. It is reading the raw and normalized acceleration values from the sensor in the loop. If the normalized acceleration exceeds thresholds, it triggers an earthquake detection by lighting LEDs and displaying a message on an LCD.

Uploaded by

kimchen edenelle
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <Wire.

h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780
chip

#include <MPU6050.h>

#define minval -5

#define maxval 3

MPU6050 mpu;

void setup()
{

lcd.begin(16, 2);

Serial.begin(115200);
pinMode(7,OUTPUT);

lcd.print(" EarthQuake ");

lcd.setCursor(0, 1);

lcd.print(" Detector");

delay (2000);

lcd.clear();

// Initialize MPU6050

Serial.println("Initialize MPU6050");

while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}

// If you want, you can set accelerometer offsets


// mpu.setAccelOffsetX();
// mpu.setAccelOffsetY();
// mpu.setAccelOffsetZ();

checkSettings();
}

void checkSettings()

{ Serial.println();
Serial.print(" * Sleep Mode: ");
Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled");

Serial.print(" * Clock Source: ");


switch(mpu.getClockSource())
{
case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps
the timing generator in reset"); break;
case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz
reference"); break;
case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz
reference"); break;
case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope
reference"); break;
case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope
reference"); break;
case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope
reference"); break;
case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator");
break;
}

Serial.print(" * Accelerometer: ");


switch(mpu.getRange())
{
case MPU6050_RANGE_16G: Serial.println("+/- 16 g"); break;
case MPU6050_RANGE_8G: Serial.println("+/- 8 g"); break;
case MPU6050_RANGE_4G: Serial.println("+/- 4 g"); break;
case MPU6050_RANGE_2G: Serial.println("+/- 2 g"); break;
}

Serial.print(" * Accelerometer offsets: ");


Serial.print(mpu.getAccelOffsetX());
Serial.print(" / ");
Serial.print(mpu.getAccelOffsetY());
Serial.print(" / ");
Serial.println(mpu.getAccelOffsetZ());

Serial.println();
}

void loop()
{
Vector rawAccel = mpu.readRawAccel();

Vector normAccel = mpu.readNormalizeAccel();

Serial.print(" Xraw = ");

Serial.print(rawAccel.XAxis);

Serial.print(" Yraw = ");

Serial.print(rawAccel.YAxis);

Serial.print(" Zraw = ");


Serial.println(rawAccel.ZAxis);

if(normAccel.XAxis > maxval || normAccel.XAxis < minval && normAccel.YAxis > maxval
|| normAccel.YAxis < minval && normAccel.ZAxis > maxval || normAccel.ZAxis <
minval)

{ digitalWrite(7,HIGH);

digitalWrite(8,HIGH);

delay(300);

digitalWrite(7,HIGH);

digitalWrite(8,HIGH);

delay(100);

lcd.clear();

lcd.print("***EarthQuake***");

lcd.print("***DETECTED***");

delay (1000);

lcd.clear();}

else{digitalWrite(7,LOW);

digitalWrite(8,LOW);}

Serial.print(" Xnorm = ");

Serial.print(normAccel.XAxis);

Serial.print(" Ynorm = ");

Serial.print(normAccel.YAxis);

Serial.print(" Znorm = ");

Serial.println(normAccel.ZAxis);

delay(10);
}

You might also like