MicroPython MPU6050 accelerometer and gyroscope on an ESP32-DevKitC V4
MicroPython on an ESP32: machine.I2C on GPIO 14 and 13 reads an MPU6050’s acceleration and rotation, raw and in g and degrees per second. The whole build, an ESP32-DevKitC V4 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $12.50. On Mokxi they're free.
What you'll learn
- How the I2C bus uses two wires
- Waking a sensor by writing to a register
- Reading several values in one transfer
- Raw sensor counts versus real units
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: ESP32-DevKitC V4
- Parts: 3
How it works
The MPU6050 talks over I2C, a two-wire bus. SCL goes to GPIO 14 and SDA to GPIO 13, at 400 kHz. The chip's address is 0x68. At power-up it sleeps, so the program first writes zero to a register to wake it up.
Then, once a second, the program reads 14 bytes in one go: acceleration, temperature and rotation. It unpacks them into seven numbers and prints the raw values. It also prints them converted to g (dividing by 16384) and degrees per second (dividing by 131).
Parts list
3 parts, plus the jumper wires. They retail for about $12.50; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 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 SCL | ESP32-DevKitC V4 pin 14 |
| Accelerometer and gyroscope, MPU6050 pin SDA | ESP32-DevKitC V4 pin 13 |
| 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.
# MPU6050: SCL on GPIO 14, SDA on GPIO 13, address 0x68. Wake it up,
# then read the accelerometer and gyroscope once a second, raw and in
# g and degrees per second (the default ranges: 2 g and 250 dps).
from machine import I2C, Pin
import struct
import time
i2c = I2C(0, scl=Pin(14), sda=Pin(13), freq=400000)
ADDR = 0x68
i2c.writeto_mem(ADDR, 0x6B, b"\x00") # clear the sleep bit
while True:
data = i2c.readfrom_mem(ADDR, 0x3B, 14) # accel, temperature, gyro
ax, ay, az, temp, gx, gy, gz = struct.unpack(">hhhhhhh", data)
print("accel", ax, ay, az, "gyro", gx, gy, gz)
print("g: %.2f %.2f %.2f dps: %.1f %.1f %.1f" % (ax / 16384, ay / 16384, az / 16384, gx / 131, gy / 131, gz / 131))
time.sleep(1)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- An error about the device not being found This project
Check SCL on GPIO 14 and SDA on GPIO 13. Do not swap them. Also check power and ground. - All readings are zero This project
The chip may still be asleep. The line that writes to register 0x6B wakes it, so keep it. - 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 board is not found, or upload fails with a connecting message. ESP32-DevKitC V4
Use a USB cable that carries data, not charge only. Pick the ESP32 Dev Module board. If it still fails, hold BOOT while uploading starts, then release. - Some pins never read right, or the board will not boot with a wire attached. ESP32-DevKitC V4
GPIO34 to 39 are input only and have no pull-ups. GPIO6 to 11 run the flash, so leave them alone. GPIO0, 2 and 12 are boot pins; avoid pulling them the wrong way at reset.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.