tutorialspinouttools
Sensors · ESP32-C3

MPU6050 on OLED: Accelerometer & Gyro

Free ESP32 & Arduino tutorial · wiring, code & parts · gokul.builds

Overview & Wiring

What it is: The MPU6050 is a 6-axis motion sensor: a 3-axis accelerometer plus a 3-axis gyroscope in one chip. It's the go-to sensor for tilt sensing, self-balancing robots, and gesture projects. How it works: I2C device at address 0x68 (0x69 if AD0 is tied high). The accelerometer measures g-force on each axis (gravity shows as ~9.8 on the "down" axis); the gyro measures rotation rate. Shares SDA/SCL with the OLED. Wiring (shared I2C bus with the OLED): MPU6050 SDA -> GPIO4 MPU6050 SCL -> GPIO5 MPU6050 VCC -> 3V3 MPU6050 GND -> GND OLED VCC=3V3, GND=GND, SDA=GPIO4, SCL=GPIO5 Libraries: "Adafruit MPU6050" (installs "Adafruit Unified Sensor" and "Adafruit BusIO"), plus "Adafruit SSD1306" and "Adafruit GFX Library". On screen / Serial: acceleration X/Y/Z in m/s². Tilt the board and watch gravity move between axes. Gotchas: Address is 0x68 by default. The raw gyro drifts over time — for stable angles you'd fuse accel + gyro with a complementary or Kalman filter (beyond this intro). Let it sit still a moment at power-on.

Circuit Diagram

MPU6050 on OLED: Accelerometer & Gyro wiring diagram for ESP32-C3

Parts You Need

ESP32-C3 SuperMiniBuy →
0.96" SSD1306 OLEDBuy →

Source Code (Arduino / ESP32)

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_MPU6050.h>

Adafruit_SSD1306 display(128, 64, &Wire, -1);
Adafruit_MPU6050 mpu;

void setup() {
  Serial.begin(115200);
  Wire.begin(4, 5);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  if (!mpu.begin(0x68)) {
    Serial.println("MPU6050 not found");
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 28); display.println("MPU6050 not found");
    display.display();
    while (1) delay(10);
  }
}

void loop() {
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);                 // accel, gyro, temperature

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);  display.println("MPU6050 accel m/s2");
  display.setTextSize(2);
  display.setCursor(0, 14); display.printf("X %.1f", a.acceleration.x);
  display.setCursor(0, 32); display.printf("Y %.1f", a.acceleration.y);
  display.setCursor(0, 50); display.setTextSize(1); display.printf("Z %.2f", a.acceleration.z);
  display.display();

  Serial.printf("aX %.2f  aY %.2f  aZ %.2f m/s2\n",
                a.acceleration.x, a.acceleration.y, a.acceleration.z);
  delay(200);
}
Browse all tutorials ESP32-C3 pinout Calculators