/*
Wokwi | questions
Friday, January 23, 2026 11:34 PM
Help with physical build (beginner)
BarniMao
I been trying to make an earthquake detector for a school project
but so far I been very un sucessful with the the physical build...
I don't know if my connections to the breadboard are wrong or if
its the code??
If anyone can give me any tips, it will be greatly appreciated
*/
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <SSD1306Ascii.h>
#include <SSD1306AsciiWire.h>
#define I2C_ADDRESS 0x3C
#define RST_PIN -1
const float THRESHOLD = 1.0;
const int BUZZER_PIN = 5;
const int RED_LED_PIN = 4;
const int GRN_LED_PIN = 3;
const int BUTTON_PIN = 2;
bool isTriggered = false;
bool isDanger = false;
bool oldBtnState = true;
bool oldDangerState = true;
float max_wave = 0.0;
float oldMaxWave = 0.0;
Adafruit_MPU6050 mpu;
SSD1306AsciiWire oled;
void checkButton() {
int btnState = digitalRead(BUTTON_PIN); // read the pin
if (btnState != oldBtnState) { // if it changed
oldBtnState = btnState; // remember the state
if (btnState == LOW) { // was just pressed
//Serial.println("Button Pressed");
isTriggered = false;
} else { // was just released
//Serial.println("Button Released");
}
delay(20); // short delay to debounce button
}
}
void displayDanger() {
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GRN_LED_PIN, LOW);
//tone(BUZZER_PIN, 1000);
oled.set2X();
oled.setCursor(30, 4);
oled.println("ALERT!");
oled.set1X();
oled.setCursor(0, 7);
oled.println("Earthquake Detected!");
Serial.println("⚠️ EARTHQUAKE DETECTED!");
}
void displaySafe() {
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GRN_LED_PIN, HIGH);
noTone(BUZZER_PIN);
oled.set2X();
oled.setCursor(40, 5);
oled.println("SAFE");
oled.set1X();
Serial.println("SAFE");
}
void setup() {
Serial.begin(115200);
// --- Initialize oled ---
oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
oled.setFont(System5x7);
oled.displayRemap(true);
oled.clear();
// --- Initialize MPU6050 ---
if (!mpu.begin()) {
Serial.println("MPU6050 not found!");
while (1);
}
// set pins
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GRN_LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// show splash
oled.setCursor(30, 2);
oled.print("Seismograph");
oled.setCursor(50, 4);
oled.print("V1.00");
delay(2000);
oled.clear();
}
void loop() {
char buffer[32]; // print buffer
char floatStr[10]; // float buffer
char *label;
checkButton();
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// compute acceleration magnitudes
float p_wave = abs(a.acceleration.x) + abs(a.acceleration.y);
float s_wave = abs(a.acceleration.z - 9.81);
// get max values
if (p_wave >= s_wave) {
max_wave = p_wave;
label = "Accel P:";
} else {
max_wave = s_wave;
label = "Accel S:";
}
if (max_wave >= oldMaxWave) {
oldMaxWave = max_wave;
}
if (isTriggered) {
dtostrf(oldMaxWave, 5, 2, floatStr); // 5 char min total width, 2 after decimal
} else {
dtostrf(max_wave, 5, 2, floatStr); // 5 char min total width, 2 after decimal
oldMaxWave = 0.0;
}
snprintf(buffer, 32, "%s %s ", label, floatStr);
// Display header
oled.setCursor(15, 0);
oled.print("Real-time Seismo");
oled.setCursor(20, 2);
oled.print(buffer);
// ----- EARTHQUAKE ALERT -----
if (!isTriggered) {
if (max_wave >= THRESHOLD) {
isDanger = true;
isTriggered = true;
} else {
isDanger = false;
}
}
if (isDanger != oldDangerState) {
oldDangerState = isDanger;
oled.clear();
if (isTriggered ) {
displayDanger();
} else {
displaySafe();
}
}
delay(100);
}
Loading
ssd1306
ssd1306
Reset