MicroPython MPU6050 on a Pico: acceleration and rotation
MicroPython on a Raspberry Pi Pico: an MPU6050 on I2C bus 1 (SCL GP15, SDA GP14) prints raw and scaled acceleration and rotation once a second. The whole build, a Raspberry Pi Pico 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.
Intermediate About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6.50. On Mokxi they're free.
What you'll learn
- Scanning and reading an I2C device
- Waking a sensor by writing a register
- Raw counts versus real units
- Using a helper file as a driver
- Difficulty: Intermediate
- Time: About 15 minutes
- Board: Raspberry Pi Pico
- Parts: 3
How it works
An MPU6050 motion sensor talks to the Pico over I2C bus 1. SDA goes to GP14 and SCL goes to GP15, with 3.3 V and ground for power. At the start, the program scans the bus and prints the addresses it finds.
A helper file, mpu6050.py, wakes the sensor by writing 0 to register 0x6B. Then it reads 14 bytes: acceleration, temperature and rotation, each a signed 16-bit number.
Once a second the main program prints the raw numbers. It also prints acceleration in g, by dividing by 16384, and rotation in degrees per second, by dividing by 131.
Parts list
3 parts, plus the jumper wires. They retail for about $6.50; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × Accelerometer and gyroscope, MPU6050In Desk Buddy Kit, Laser Sword Kit, Escape Box Kit.
- 1 × Power, 3.3 V
Kits that can build it
This starter kit has 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 SDA | Raspberry Pi Pico pin GP14 |
| Accelerometer and gyroscope, MPU6050 pin SCL | Raspberry Pi Pico pin GP15 |
| Accelerometer and gyroscope, MPU6050 pin VCC | 3.3 V |
| Accelerometer and gyroscope, MPU6050 pin GND | Ground |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# An MPU6050 on I2C bus 1 (SCL GP15, SDA GP14): raw readings, and the same
# turned into g and degrees per second, once a second. mpu6050.py is in
# the next tab.
from machine import I2C, Pin
import time
from mpu6050 import MPU6050
i2c = I2C(1, scl=Pin(15), sda=Pin(14), freq=400000)
print("I2C:", [hex(a) for a in i2c.scan()])
mpu = MPU6050(i2c)
while True:
accel, gyro, temp = mpu.read()
print("accel raw", accel, "gyro raw", gyro)
print("accel g %.2f %.2f %.2f" % tuple(a / 16384 for a in accel))
print("gyro dps %.1f %.1f %.1f" % tuple(g / 131 for g in gyro))
time.sleep(1)
# mpu6050.py: an MPU6050 accelerometer and gyroscope on I2C.
# Register 0x6B wakes it up; fourteen bytes from 0x3B are the acceleration,
# the temperature and the rotation, each a signed 16-bit number, high byte first.
import struct
class MPU6050:
def __init__(self, i2c, addr=0x68):
self.i2c = i2c
self.addr = addr
i2c.writeto_mem(addr, 0x6B, bytes([0])) # out of sleep
def read(self):
data = self.i2c.readfrom_mem(self.addr, 0x3B, 14)
ax, ay, az, t, gx, gy, gz = struct.unpack(">hhhhhhh", data)
return (ax, ay, az), (gx, gy, gz), t / 340 + 36.53
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The scan prints an empty list. This project
Check SDA is on GP14 and SCL on GP15, and that VCC and GND are connected. - Readings stay at zero. This project
The sensor starts asleep. Make sure mpu6050.py is present so it can write 0 to register 0x6B. - 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. - The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it. - A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.