4 · Displays & buses
MPU6050 inertial unit
Expert30 min
Goal
Read the accelerometer and gyroscope over I²C.
Why it matters
Those six axes are the heart of every drone, phone or smartwatch.
Steps
- 01Drag in the MPU6050 (SDA A4, SCL A5, address 0x68).
- 02Load the starter sketch: it wakes the sensor and reads its registers.
- 03Run it, open the Scenario panel and tilt the virtual board.
- 04Watch the values follow the sliders in the serial monitor.
Starter sketch
// MPU6050 over I2C — raw register reads, no external library needed.
#include <Wire.h>
const uint8_t MPU = 0x68;
void writeReg(uint8_t reg, uint8_t val) {
Wire.beginTransmission(MPU);
Wire.write(reg);
Wire.write(val);
Wire.endTransmission();
}
int16_t readWord(uint8_t reg) {
Wire.beginTransmission(MPU);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom(MPU, (uint8_t)2);
int16_t hi = Wire.read();
int16_t lo = Wire.read();
return (hi << 8) | lo;
}
void setup() {
Serial.begin(9600);
Wire.begin();
writeReg(0x6B, 0x00); // wake up
writeReg(0x1B, 0x00); // gyro +/- 250 deg/s
writeReg(0x1C, 0x00); // accel +/- 2 g
Serial.println("MPU6050 ready");
}
void loop() {
float ax = readWord(0x3B) / 16384.0;
float ay = readWord(0x3D) / 16384.0;
float az = readWord(0x3F) / 16384.0;
float gz = readWord(0x47) / 131.0;
Serial.print("accel ");
Serial.print(ax); Serial.print(" ");
Serial.print(ay); Serial.print(" ");
Serial.print(az);
Serial.print(" gyroZ ");
Serial.println(gz);
delay(300);
}
This tutorial runs inside the Circuitly simulator: your wiring and your code are checked automatically at every step.