The MPU6050 is an inertial unit that combines an accelerometer and a gyroscope. It is used to measure acceleration, inclination and angular velocity. In this tutorial, we will try to explain how to use an MPU6050 with a Raspberry Pi Pico using MicroPython.
The accelerometer makes it possible to know the acceleration and/or the gravitational field according to 3 axes: x, y and z. If you take a closer look, you can see a drawing of the x and the y axes on the module, while the z axis is perpendicular to the plane of the module. When the module is stationary, flat on a table, it will measure an acceleration of 1 g along the z axis, due to the force of gravity acting downwards. If the measured acceleration is zero along the 3 axes, the MPU-6050 module is in free fall!
The gyroscope measures the angular speed along the 3 axes. When the module is immobile, the 3 components of the angular velocity are, in principle, zero (a calibration is however necessary for the result to be exact).

Connections
The MPU-6050 module has 8 connectors, but only 4 of them are necessary for its operation (2 for power supply, and 2 for data transmission by I2C):
- VCC pin (MPU-6050) => 3.3V output pin (Raspberry Pi Pico).
- GND pin (MPU-6050) => GND pin (Raspberry Pi Pico).
- SCL pin (MPU-6050) => GP9 pin (Raspberry Pi Pico).
- SDA pin (MPU-6050) => GP8 pin (Raspberry Pi Pico).

Installing imu.py and vector3d.py libraries
You can find the necessary libraries on this Github repository. Also, you need to copy the “imu.py” and “vector3d.py” files to the flash memory of the Raspberry Pi Pico.
First script
This script displays the measurements from the accelerometer and the gyroscope. From the data of the accelerometer, the script tries to determine if one of the 3 axes (x, y or z) approaches the vertical. Based on the data from the gyroscope, the script indicates whether the MPU-6050 module is spinning.
Run this script, and orient the MPU-6050 module in different ways to see how the displayed values vary.
''' Using the MPU6050 inertial unit (accelerometer + gyrometer) with a Raspberry Pi Pico. For more info: bekyelectronics.com/raspberry-pi-pico-and-mpu-6050-micropython/ ''' from imu import MPU6050 # https://github.com/micropython-IMU/micropython-mpu9x50 import time from machine import Pin, I2C i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000) imu = MPU6050(i2c) # Temperature display print("Temperature: ", round(imu.temperature,2), "°C") while True: # reading values acceleration = imu.accel gyroscope = imu.gyro print ("Acceleration x: ", round(acceleration.x,2), " y:", round(acceleration.y,2), "z: ", round(acceleration.z,2)) print ("gyroscope x: ", round(gyroscope.x,2), " y:", round(gyroscope.y,2), "z: ", round(gyroscope.z,2)) # data interpretation (accelerometer) if abs(acceleration.x) > 0.8: if (acceleration.x > 0): print("The x axis points upwards") else: print("The x axis points downwards") if abs(acceleration.y) > 0.8: if (acceleration.y > 0): print("The y axis points upwards") else: print("The y axis points downwards") if abs(acceleration.z) > 0.8: if (acceleration.z > 0): print("The z axis points upwards") else: print("The z axis points downwards") # data interpretation (gyroscope) if abs(gyroscope.x) > 20: print("Rotation around the x axis") if abs(gyroscope.y) > 20: print("Rotation around the y axis") if abs(gyroscope.z) > 20: print("Rotation around the z axis") time.sleep(0.2)
Second script
In this second example, the Raspberry Pi Pico’s built-in LED lights up when you shake the MPU6050 module.
''' Using the MPU6050 inertial unit (accelerometer + gyroscope) with a Raspberry Pi Pico. The Pico LED lights up when the MPU6050 is shaken. For more info: bekyelectronics.com/raspberry-pi-pico-and-mpu-6050-micropython/ ''' from imu import MPU6050 # https://github.com/micropython-IMU/micropython-mpu9x50 import time from machine import Pin, I2C i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000) imu = MPU6050(i2c) # LED initially off Pin(25, Pin.OUT).value(0) while True: # acceleration reading acceleration = imu.accel.magnitude print (acceleration) # value at rest is 1 if abs(acceleration - 1) > 0.1: print("It is moving!") Pin(25, Pin.OUT).value(1) # turn on the LED else: Pin(25, Pin.OUT).value(0) # turn off the LED time.sleep(0.2)
That’s it, this is how you can use the MCU6050 unit with your Paspberry Pi Pico. You can check other tutorials from this link. If you want to establish wireless communication between two of your Picos using NRF24L01, click here.
Pingback: The 16x02 LCD with Arduino - Beky Electronics