MPU-6050 readings over I2C on an Arduino Uno
A GY-521 board with an MPU-6050 on A4 and A5: the sketch wakes the chip and prints its raw accelerometer, temperature and gyro readings with nothing but Wire. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- Talking to an I2C chip with Wire
- Waking a sensor by writing to a register
- Combining two bytes into a 16-bit number
- Reading accelerometer, temperature and gyro values
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
The MPU-6050 is a small chip that measures acceleration, temperature and rotation. It talks I2C at address 0x68, and on the Uno that means A4 for SDA and A5 for SCL.
The chip wakes up asleep. The sketch writes 0 to its power register at 0x6B to wake it. After that, each loop asks for 14 bytes starting at register 0x3B. That holds three accelerations, the temperature and three rotation rates, each a 16-bit number sent high byte first.
Flat and still, Z reads about 16384, which is 1 g, gravity. Tilt the board and X and Y take some of it.
Parts list
3 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Accelerometer and gyroscope, MPU6050In Desk Buddy Kit, Laser Sword Kit, Escape Box Kit.
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Accelerometer and gyroscope, MPU6050 pin VCC | Arduino Uno R3 pin 5V |
| Accelerometer and gyroscope, MPU6050 pin GND | Arduino Uno R3 pin GND |
| Accelerometer and gyroscope, MPU6050 pin SDA | Arduino Uno R3 pin A4 |
| Accelerometer and gyroscope, MPU6050 pin SCL | Arduino Uno R3 pin A5 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// An MPU-6050 on the I2C bus: raw acceleration, temperature and rotation.
//
// GY-521 VCC -> 5V GND -> GND SCL -> A5 SDA -> A4
//
// The chip answers at I2C address 0x68. It wakes up asleep, so the sketch
// first writes 0 to its power register (0x6B). After that, fourteen bytes
// starting at register 0x3B hold three accelerations, the temperature and
// three rotation rates, each a 16-bit number sent high byte first.
//
// At rest and flat, Z reads about 16384: that is 1 g, gravity, on the
// default range. Drag the board's pitch or roll and watch X and Y take some
// of it.
#include <Wire.h>
const int MPU = 0x68;
int16_t readWord() {
int16_t high = Wire.read();
int16_t low = Wire.read();
return (high << 8) | low;
}
void setup() {
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
Serial.println("Mokxi Uno: MPU-6050 on A4 (SDA) and A5 (SCL)");
}
void loop() {
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true);
int16_t ax = readWord();
int16_t ay = readWord();
int16_t az = readWord();
int16_t raw = readWord();
int16_t gx = readWord();
int16_t gy = readWord();
int16_t gz = readWord();
Serial.print("AcX = "); Serial.print(ax);
Serial.print(" | AcY = "); Serial.print(ay);
Serial.print(" | AcZ = "); Serial.print(az);
Serial.print(" | Tmp = "); Serial.print(raw / 340.0 + 36.53, 1);
Serial.print(" | GyX = "); Serial.print(gx);
Serial.print(" | GyY = "); Serial.print(gy);
Serial.print(" | GyZ = "); Serial.println(gz);
delay(500);
}
Code walkthrough
The key functions of the sketch, and what each one does.
readWord sketch.ino
Reads two bytes from the I2C bus. The first is the high byte and the second is the low byte. It shifts the high byte left by 8 and combines them with OR to make a signed 16-bit number. Everything the chip sends comes in pairs like this.
int16_t readWord() {
int16_t high = Wire.read();
int16_t low = Wire.read();
return (high << 8) | low;
}setup sketch.ino
Starts I2C with Wire.begin(), then writes 0 to the power management register 0x6B to wake the chip. It opens serial at 9600 baud.
void setup() {
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
Serial.println("Mokxi Uno: MPU-6050 on A4 (SDA) and A5 (SCL)");
}loop sketch.ino
Points the chip at register 0x3B, requests 14 bytes, and calls readWord seven times for three accelerations, the temperature and three rotation rates. It prints them, and converts the temperature with raw / 340.0 + 36.53. Then it waits half a second.
void loop() {
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true);
int16_t ax = readWord();
int16_t ay = readWord();
int16_t az = readWord();
int16_t raw = readWord();
int16_t gx = readWord();
int16_t gy = readWord();
int16_t gz = readWord();
Serial.print("AcX = "); Serial.print(ax);
Serial.print(" | AcY = "); Serial.print(ay);
Serial.print(" | AcZ = "); Serial.print(az);
Serial.print(" | Tmp = "); Serial.print(raw / 340.0 + 36.53, 1);
Serial.print(" | GyX = "); Serial.print(gx);
Serial.print(" | GyY = "); Serial.print(gy);
Serial.print(" | GyZ = "); Serial.println(gz);
delay(500);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Every reading is zero This project
The chip may still be asleep, or SDA and SCL may be swapped. SDA goes to A4 and SCL to A5. - Z does not read about 16384 when flat This project
The board may be tilted, or upside down. Flat and face up, gravity should show on Z. - The sensor is not found or reads all zeros. Accelerometer and gyroscope
Check SDA and SCL (A4 and A5 on an Uno), power, and ground. The I2C address is 0x68, or 0x69 if AD0 is high. The chip starts in sleep mode, so write 0 to the power register PWR_MGMT_1. - The values look wrong or the angles drift. Accelerometer and gyroscope
Raw readings are counts, not g or degrees per second. Divide by the scale for your range (16384 per g at 2 g). Gyro angles drift, so blend with the accelerometer using a filter. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.