地震儀
[材料]
Arduino主板 x 1
麵包板 x 1
GY-88感測器 x 1
OLed x 1
[OLED每排文字說明]
單位: m/s^2
X軸、Y軸、Z軸的加速度
三軸合併的加速度(已減掉重力加速度的影響);每五秒內的最大加速度,數字已乘上100,單位為cm/s^2
顯示五秒內地震的最大震度
[GY-88腳位]
VIN -> arduino pin3
3V3 --> arduino 3v3
GND --> arduino GND
SCL --> arduino A5
SDA --> arduino A4
[OLed 腳位]
VCC --> arduino 3.3V
GND --> arduino GND
SCL --> arduino A5
SDA --> arduino A4
#include <Adafruit_MPU6050.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
unsigned long begin_time = -6000;
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("MPU6050 OLED demo");
if (!mpu.begin()) {
Serial.println("Sensor init failed");
while (1)
yield();
}
Serial.println("Found a MPU-6050 sensor");
mpu.setAccelerometerRange(MPU6050_RANGE_16_G);
Serial.print("Accelerometer range set to: ");
switch (mpu.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange()) {
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}
mpu.setFilterBandwidth(MPU6050_BAND_260_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
Serial.println("");
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.display();
delay(500); // Pause for 2 seconds
display.setTextSize(1);
display.setTextColor(WHITE);
display.setRotation(0);
}
void loop() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
float x = a.acceleration.x;
float y = a.acceleration.y;
float z = a.acceleration.z;
display.clearDisplay();
display.setCursor(0, 0);
Serial.print("Accelerometer ");
Serial.print("X: ");
Serial.print(x, 1);
Serial.print(" m/s^2, ");
Serial.print("Y: ");
Serial.print(y, 1);
Serial.print(" m/s^2, ");
Serial.print("Z: ");
Serial.print(x, 1);
Serial.println(" m/s^2");
display.println("Accelerometer - m/s^2");
display.print(x, 2);
display.print(", ");
display.print(y, 2);
display.print(", ");
display.print(z, 2);
display.println("");
float acc_tot1 = (sqrt (x*x + y*y + z*z) - 9.525) *100;
float acc_tot2;
display.print(acc_tot1,5);
Serial.println(acc_tot1 ,5);
if (acc_tot1 >= acc_tot2) {
acc_tot2 = acc_tot1;
begin_time = millis();
}
if (millis() - begin_time > 5000) {
acc_tot2 = acc_tot1;
}
/*
Serial.print(begin_time);
Serial.print(" ");
Serial.print(acc_tot1);
Serial.print(" ");
Serial.println(millis());*/
display.setCursor(68, 16);
display.println(acc_tot2,5);
display.print("1 2 3 4 5- 5+ 6- 6+ 7");
int delay_time = millis() - begin_time;
if (acc_tot2 >= 0.8) {
display.setCursor(0, 24);
display.setTextColor(BLACK, WHITE);
display.print("1");
}
if (acc_tot2 >= 2.5) {
display.setCursor(6, 24);
display.setTextColor(BLACK, WHITE);
display.print(" 2");
}
if (acc_tot2 >= 8) {
display.setCursor(18, 24);
display.setTextColor(BLACK, WHITE);
display.print(" 3");
}
if (acc_tot2 >= 25) {
display.setCursor(30, 24);
display.setTextColor(BLACK, WHITE);
display.print(" 4");
}
if (acc_tot2 >= 80) {
display.setCursor(42, 24);
display.setTextColor(BLACK, WHITE);
display.print(" 5-");
}
if (acc_tot2 >= 140) {
display.setCursor(60, 24);
display.setTextColor(BLACK, WHITE);
display.print(" 5+");
}
if (acc_tot2 >= 250) {
display.setCursor(78, 24);
display.setTextColor(BLACK, WHITE);
display.print(" 6-");
}
if (acc_tot2 >= 440) {
display.setCursor(96, 24);
display.setTextColor(BLACK, WHITE);
display.print(" 6+");
}
if (acc_tot2 >= 800) {
display.setCursor(114, 24);
display.setTextColor(BLACK, WHITE);
display.print(" 7");
}
display.setTextColor(WHITE);
display.display();
//delay(100);
}