#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// Create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int counter = 0;
Adafruit_MPU6050 mpu; // Create MPU6050 object named "mpu"
void setup() {
Serial.begin(9600);
// Initialize the MPU6050 sensor
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
// Configure the MPU6050 sensor settings
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
// Initialize Serial communication and delay for 500 milliseconds
Serial.println("");
delay(500);
// Initialize the OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Failed to start SSD1306 OLED"));
while (1);
}
// Delay for 1.5 second and then clear the OLED display
delay(1500);
oled.clearDisplay();
// Set OLED display text size and color
oled.setTextSize(1);
oled.setTextColor(WHITE);
// Display "Accelerometer:" text (position (0, 2))
oled.setCursor(10, 0);
oled.println("Accelerometer:");
oled.display();
// Display "Gyroscope:" text (position (0, 45))
oled.setCursor(10, 42);
oled.println("Gyroscope:");
oled.display();
}
void loop() {
oled.fillRect(0, 20, 30, 20, 0x000000);
// Read sensor data from the MPU6050
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Display gyroscope data
oled.setCursor(10, 51);
oled.print(g.gyro.x / 10);
oled.print(", ");
oled.print(g.gyro.y / 10);
oled.print(", ");
oled.print(g.gyro.z / 10);
oled.display();
// Display accelerometer data
oled.setCursor(10, 10);
oled.print(a.acceleration.x / 10);
oled.print(", ");
oled.print(a.acceleration.y / 10);
oled.print(", ");
oled.print(a.acceleration.z / 10);
oled.display();
// Delay for 2 seconds before the next iteration
delay(2000);
}