Built-in project · Raspberry Pi Pico W

Pico W MPU6050 acceleration and rotation

MicroPython on a Pico W: an MPU6050 on I2C1 (SDA GP6, SCL GP7) prints its acceleration in g and its rotation in degrees a second. The whole build, a Raspberry Pi Pico W 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 $8.50. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading a sensor over I2C in MicroPython
  • Accelerometers measure gravity and motion
  • Gyroscopes measure rotation speed
  • Turning raw counts into real units
  • Difficulty: Intermediate
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 3

How it works

An MPU6050 motion sensor connects to the Pico W over I2C. SDA goes to GP6 and SCL goes to GP7, with 3.3 V and ground for power.

The sensor starts asleep. The helper file mpu6050.py wakes it by writing 0 to register 0x6B. It then reads 14 bytes of raw data at once.

Every 500 ms the main program prints acceleration in g for x, y and z, and rotation in degrees per second. Lying flat, one axis reads about 1 g from gravity. Rotation readings are near zero until you turn the board.

Parts list

3 parts, plus the jumper wires. They retail for about $8.50; on Mokxi they're free.

Kits that can build it

These 2 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 pinConnects to
Accelerometer and gyroscope, MPU6050 pin SDARaspberry Pi Pico W pin GP6
Accelerometer and gyroscope, MPU6050 pin SCLRaspberry Pi Pico W pin GP7
Accelerometer and gyroscope, MPU6050 pin VCC3.3 V
Accelerometer and gyroscope, MPU6050 pin GNDGround

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.

main.py · Raspberry Pi Pico W
# 6-axis motion tracking: an MPU6050 on I2C1 (SDA GP6, SCL GP7).
# mpu6050.py, in its own tab, wakes it and reads acceleration in g
# and rotation in degrees a second.
from machine import I2C, Pin
from mpu6050 import MPU6050
import time

i2c = I2C(1, sda=Pin(6), scl=Pin(7), freq=400000)
mpu = MPU6050(i2c)

while True:
    ax, ay, az = mpu.accel()
    gx, gy, gz = mpu.gyro()
    print("accel x %.2f y %.2f z %.2f g" % (ax, ay, az))
    print("gyro  x %.1f y %.1f z %.1f deg/s" % (gx, gy, gz))
    time.sleep_ms(500)
mpu6050.py · Raspberry Pi Pico W
# mpu6050.py: read an MPU6050's accelerometer and gyroscope over I2C.
# At power-up the chip is asleep; writing 0 to register 0x6B wakes it.
# The default ranges are +-2 g (16384 counts a g) and +-250 degrees a
# second (131 counts a degree a second).
import struct

class MPU6050:
    def __init__(self, i2c, addr=0x68):
        self.i2c = i2c
        self.addr = addr
        i2c.writeto_mem(addr, 0x6B, bytes([0]))

    def raw(self):
        data = self.i2c.readfrom_mem(self.addr, 0x3B, 14)
        return struct.unpack(">hhhhhhh", data)   # ax ay az temp gx gy gz

    def accel(self):
        ax, ay, az = self.raw()[0:3]
        return ax / 16384, ay / 16384, az / 16384

    def gyro(self):
        gx, gy, gz = self.raw()[4:7]
        return gx / 131, gy / 131, gz / 131

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The program fails with an I2C error. This project
    Check SDA is on GP6 and SCL on GP7, since this uses I2C bus 1, and that VCC and GND are connected.
  • Readings seem scaled wrong. This project
    The helper assumes the default ranges: 16384 counts per g and 131 counts per degree per second.
  • 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.
  • Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
    Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
  • WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
    Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.