Home CodeMicropython MPU6050 Accelerometer and Gyroscope Sensor example with Pico and micropython

MPU6050 Accelerometer and Gyroscope Sensor example with Pico and micropython

by rp2040guy71

In this article we connect the ever popular MPU6050 Accelerometer and Gyroscope Sensor to a raspberry Pi Pico and we will then display the temperature and humidity to the repl window in Thonny using micropython

Lets look at the sensor first

Features

The MPU-60X0 is the world’s first integrated 6-axis MotionTracking device that combines a 3-axis gyroscope, 3-axis accelerometer, and a Digital Motion Processor™ (DMP) all in a small package.

With its dedicated I2C sensor bus, it directly accepts inputs from an external 3-axis compass to provide a complete 9-axis MotionFusion™ output. The MPU-60X0 MotionTracking device, with its 6-axis integration, on-board MotionFusion™, and run-time calibration firmware, enables manufacturers to eliminate the costly and complex selection, qualification, and system level integration of discrete devices, guaranteeing optimal motion performance for consumers.

The MPU-60X0 is also designed to interface with multiple noninertial digital sensors, such as pressure sensors, on its auxiliary I2C port. The MPU-60X0 is footprint compatible with the MPU-30X0 family.

The MPU-60X0 features three 16-bit analog-to-digital converters (ADCs) for digitizing the gyroscope outputs and three 16-bit ADCs for digitizing the accelerometer outputs. For precision tracking of both fast and slow motions, the parts feature a user-programmable gyroscope full-scale range of ±250, ±500, ±1000, and ±2000°/sec (dps) and a user-programmable accelerometer full-scale range of ±2g, ±4g, ±8g, and ±16g

You can read more about the sensor in the datasheet linked at the bottom of this article if you feel that way inclined.

Parts Required

 

Name Link
Pico Raspberry Pi Pico Development Board
MPU6050

Aliexpress link

Connecting cables Aliexpress product link

Lysee 3D Printer Parts & Accessories – AHT20 Temperature and Humidity Sensor Module DHT11 Upgrade I2C XD Humidity Sensor Probe – (Color: Green)

Ebay link

 

Schematic/Connection

To connect the display to your Raspberry PI Pico requires just 3 wires, you just need to wire the Vcc and GND PINs from display to VBuS and a GND PINs of RPI Pico, then the SCL and SDA pins from the module to suitable pins from your Raspberry PI Pico, in this case we use pins 6 and 7

Here is a layout in fritzing to show this

Code Example

I use thonny for all development

You will need to copy the MPU6050 library to your Pico or similar Rp2040 board

Here it is, download and unzip the python file – mpu6050 library

Now for the code example

 

import math
import utime
from machine import Pin, I2C
from mpu6050 import MPU6050

mpu = MPU6050(bus=1, scl=Pin(7), sda=Pin(6))


def main():
    last_Gx, last_Gy = 64, 64

    while True:
        g = mpu.readData()
        Gx, Gy = (int)(64 + g.Gx * 30), (int)(64 + g.Gy * -30)

        print("X:{:.2f}  Y:{:.2f}  Z:{:.2f}".format(g.Gx, g.Gy, g.Gz))
        utime.sleep_ms(500)
    pass


if __name__ == "__main__":
    main()

In the repl window I saw this is

>>> %Run -c $EDITOR_CONTENT
X:0.00 Y:0.00 Z:0.00
X:-2.00 Y:0.37 Z:0.32
X:-2.00 Y:0.37 Z:0.31
X:-2.00 Y:0.27 Z:0.43
X:-2.00 Y:-0.03 Z:-0.57
X:-1.02 Y:-0.85 Z:-0.44
X:-2.00 Y:-0.24 Z:-0.92
X:-2.00 Y:-0.40 Z:-0.62
X:-2.00 Y:0.02 Z:0.32

Links

Datasheet

mpu6050 library

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More