/* Library Section */
#include <Adafruit_MPU6050.h> // library to interface with MPU6050 sensor
#include <Adafruit_Sensor.h> // library for sensor-related functionalities
#include <Wire.h> // library for I2C communication
#include <Adafruit_SSD1306.h> // library to interface with the SSD1306 OLED display
#include <Adafruit_GFX.h> // library to display user interfaces
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); // Set an SSD1306 display object named 'oled'
Adafruit_MPU6050 mpu; // Set an MPU6050 sensor object named 'mpu'
/* Setup Section */
void setup(void) {
Serial.begin(9600);
// Initialize the OLED display
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Check if the display initialization failed
Serial.println(F("SSD1306 allocation failed"));
for (;;) {
delay(10);
}
}
// Initialize the MPU6050 sensor
if (!mpu.begin()) { // Check if MPU6050 initialization failed
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
oled.display(); // Initialize with the display buffer
delay(2000); // Pause for 2 seconds
oled.clearDisplay(); // Clear the display buffer
}
/* Loop Section */
void loop() {
sensors_event_t a, g, temp; // Create sensor event objects for accelerometer, gyroscope, and temperature
mpu.getEvent(&a, &g, &temp); // Read sensor data from the MPU6050 and store it in the sensor event objects
oled.clearDisplay(); // Clear the OLED display buffer
oled.setTextSize(1); // Set the text size to 1
oled.setTextColor(SSD1306_WHITE); // Set the text color to white
oled.setCursor(0, 0); // Set the cursor position to (0, 0) on the display
// Display text for accelerometer data
oled.println("RANGGA RETIADJI PRASETYAWAN");
oled.println("Accelerometer (g):");
oled.print("X: "); oled.println(a.acceleration.x / 10);
oled.print("Y: "); oled.println(a.acceleration.y / 10);
oled.print("Z: "); oled.println(a.acceleration.z / 10);
// Display text for gyroscope data
oled.println("Gyroscope (deg/s):");
oled.print("X: "); oled.println(g.gyro.x / 10);
oled.print("Y: "); oled.println(g.gyro.y / 10);
oled.print("Z: "); oled.println(g.gyro.z / 10);
oled.display(); // Update the OLED display with the new data
delay(500); // Delay before the next loop iteration
}