//Vorapon Kachapornkul 6458083656
//Importing all neccessary libaries
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//Naming the object of our components
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
Adafruit_MPU6050 mpu;
void setup(void) {
Serial.begin(9600);
// MPU Initalization Block
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.println("MPU Config Successful");
delay(1000);
//end of MPU Initalization block
// Initialize OLED display with I2C address 0x3C block
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(1000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("MCU Gyro Interface"); // set text
oled.display(); // display on OLED
Serial.println(("SSD1306 OLED Start Successful"));
delay(2000);
//end of OLED Initalization block with example text
}
void loop() {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
//Clearing the display every loop to avoid overwrites
oled.clearDisplay();
//Code block for displaying on LED
oled.setCursor(0, 2); //cursor for title
oled.println("Accelerometer:");
oled.setCursor(0, 18); //cursor for content
oled.print("X");
oled.print(a.acceleration.x/10);
oled.print(" Y");
oled.print(a.acceleration.y/10);
oled.print(" Z");
oled.println(a.acceleration.z/10);
oled.setCursor(0, 36); //cursor for second title
oled.println("Gyroscope:");
oled.setCursor(0, 52); // cursor for second content
oled.print("X");
oled.print(g.gyro.x/10);
oled.print(" Y");
oled.print(g.gyro.y/10);
oled.print(" Z");
oled.print(g.gyro.z/10);
oled.display();
//end of code block for displaying on LED
//Code block for serial cross reference in terminal
Serial.println("Accelerometer:");
Serial.print(a.acceleration.x/10);
Serial.print(",");
Serial.print(a.acceleration.y/10);
Serial.print(",");
Serial.println(a.acceleration.z/10);
Serial.println("");
Serial.println("Gyroscope:");
Serial.print(g.gyro.x/10);
Serial.print(",");
Serial.print(g.gyro.y/10);
Serial.print(",");
Serial.print(g.gyro.z/10);
Serial.println("");
Serial.println("");
//end of code block for display in terminal
delay(500); // Delay
}