#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_MPU6050.h>
// OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_MPU6050 mpu;
const int buzzerPin = 1;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize I2C communication
Wire.begin(19, 18); // SDA (GPIO 6), SCL (GPIO 7)
// Initialize MPU6050
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.display();
delay(1000); // Pause for 1 second
display.clearDisplay();
// Configure the MPU6050 sensor
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
// Initialize buzzer pin
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
}
void loop() {
// Get new sensor events with the readings
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Clear the buffer
display.clearDisplay();
// Display acceleration values
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Accel X: "); display.println(a.acceleration.x);
display.print("Accel Y: "); display.println(a.acceleration.y);
display.print("Accel Z: "); display.println(a.acceleration.z);
// Display gyroscope values
display.setCursor(0, 30);
display.print("Gyro X: "); display.println(g.gyro.x);
display.print("Gyro Y: "); display.println(g.gyro.y);
display.print("Gyro Z: "); display.println(g.gyro.z);
// Read temperature from MPU6050
float temperature = temp.temperature;
// Display temperature value
display.setCursor(0, 55);
display.print("Temp: "); display.print(temperature); display.println(" *C");
// Check if the temperature exceeds 50 degrees Celsius
if (temperature > 50.0) {
tone(buzzerPin, 1000); // Turn on the buzzer with 1kHz tone
} else {
noTone(buzzerPin); // Turn off the buzzer
}
display.display();
// Delay to make it readable
delay(500);
}