RPi Pico B.Tech/Diploma Beginner

Interfacing MPU6050 with Raspberry Pi Pico & MicroPython

Interfacing MPU6050 with Raspberry Pi Pico & MicroPython Code to measure acceleration, gyroscope, temperature, tilt angles, pitch, roll, yaw

  • Suitable forB.Tech/Diploma RPi Pico students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

Overview
This guide is on utilizing the powerful MPU6050 MEMS module with the versatile Raspberry Pi Pico with MicroPython. In this guide, we will delve into the world of motion sensing and measurement by learning how to obtain accelerometer, gyroscope, and temperature values using the MicroPython firmware. Using the accelerometer and gyroscope data we can calculate the tilt angle as well as pitch, roll, and yaw values.
Th guide will take you through understanding the key aspects of the MPU6050, such as its pinout diagram and pin configuration. We will then explore the process of uploading the MPU6050 MicroPython library to the Raspberry Pi Pico using the Thonny IDE. Lastly, we will demonstrate how to acquire accurate and reliable readings from the MPU6050 module, enabling you to harness its full potential in your projects.
You may refer to previous guide about Accelerometer Sensor:
ADXL335 Analog Accelerometer with Raspberry Pi Pico
ADXL345 Digital Accelerometer with Raspberry Pi Pico
Bill of Materials
S.N.
Components
Quantity
Purchase Link

Raspberry Pi Pico

Amazon
AliExpress

MPU6050 Module

Amazon
AliExpress

Breadboard

Amazon
AliExpress

Connecting Wires

Amazon
AliExpress
MPU6050 Accelerometer Gyroscope Sensor
The MPU6050 is a popular, low-cost, and compact 6-axis MotionTracking sensor, designed by InvenSense.
The sensor combines a 3-axis gyroscope, 3-axis accelerometer, and a Digital Motion Processor (DMP) in a single integrated circuit (IC) package. The MPU6050 is widely used in various applications such as robotics, smartphones, gaming devices, and wearable technology, thanks to its accurate motion tracking capabilities and high sensitivity.
The MPU6050 sensor works by combining the functionalities of a 3-axis accelerometer, a 3-axis gyroscope, and a Digital Motion Processor (DMP) to measure and process motion data. The accelerometer measures linear acceleration, while the gyroscope measures angular velocity. Together, these sensors provide a comprehensive understanding of an object’s motion in six degrees of freedom (DOF).
The data is converted into digital signals, processed by the DMP, and communicated to a host microcontroller via the I2C interface for further analysis and application-specific processing.
MPU6050 Features & Speicifications
The
MPU6050
has a wide range of features and specifications that make it suitable for motion tracking and sensing applications:
3-axis gyroscope with a programmable full-scale range of
±250, ±500, ±1000
, and
±2000
degrees per second (dps).
3-axis accelerometer with a programmable full-scale range of
±2g, ±4g, ±8g
, and
±16g
16-bit analog-to-digital converters
(ADCs) for both accelerometer and gyroscope, providing high-resolution data.
Digital Motion Processor (DMP)
for on-chip motion processing, reducing the load on the host processor.
I2C (Inter-Integrated Circuit) and SPI (Serial Peripheral Interface) communication interfaces.
Power supply voltage range:
2.375V
to
3.46V
Low power consumption:
3.9mA
in normal mode and
5μA
in sleep mode.
Temperature sensor with a range of –
40°C
to
85°C
Interrupt functionality for data-ready and motion detection.
Small package size:
4x4x0.9mm
QFN.
MPU6050 I2C Interface
The MPU6050 uses the
I2C interface
for communication with
microcontrollers
or other devices. I2C is a synchronous, bidirectional, two-wire communication protocol, which allows multiple devices to be connected to a single bus. The two wires in the I2C bus are
Serial Data Line
(SDA) and
Serial Clock Line
(SCL).
The MPU6050 acts as a slave device on the I2C bus, and its
7-bit I2C address
can be either
0x68
or
0x69
, depending on the logic level of the
AD0
pin.
MPU6050 Pinout
The MPU6050 module is typically mounted on a breakout board to facilitate easier access to its pins and connections. The breakout board may have additional components like voltage regulators or pull-up resistors for the I2C lines, which makes it more user-friendly for prototyping purposes.
The pinout for the MPU6050 module consists of the following pins:
VCC
: Power supply pin, usually accepts input voltage in the range of 3.3V to 5V (depending on the onboard voltage regulator). The MPU6050 IC itself has a supply voltage range of 2.375V to 3.46V.
GND
: Ground reference pin.
SCL
: I2C serial clock line, used for communication with the host microcontroller.
SDA
: I2C serial data line, used for communication with the host microcontroller.
XDA
: Auxiliary I2C data line, for connecting external I2C sensors.
XCL
: Auxiliary I2C clock line, for connecting external I2C sensors.
AD0
(or ADDR): I2C address selection pin. Depending on its logic level (0: 0x68, 1: 0x69), the 7-bit I2C address of the MPU6050 can be changed.
INT
: Interrupt pin, used to indicate data-ready or motion-detection events to the host microcontroller.
Interfacing MPU6050 with Raspberry Pi Pico
Now, let us interface MPU6050 Gyroscope Accelerometer sensor with Raspberry Pi Pico using the MicroPython Code. The connection is straightforward.
Make the connections between the Raspberry Pi Pico and the MPU6050:
Connect the VCC pin of the MPU6050 to the 3.3V (Pin 36) of the Raspberry Pi Pico.
Connect the GND pin of the MPU6050 to the GND (Pin 38) of the Raspberry Pi Pico.
Connect the SDA pin of the MPU6050 to the SDA (Pin 1 – GP0) of the Raspberry Pi Pico.
Connect the SCL pin of the MPU6050 to the SCL (Pin 2 – GP1) of the Raspberry Pi Pico.
MPU6050 MicroPython Library
There are several MicroPython libraries available for the MPU6050. But we have created our own library for measuring various accelerometer and gyroscope parameters.
Copy the following code code and paste it on your Thonny IDE. Save the file as ‘
mpu6050.py
‘ on your Raspberry Pi Pico.

from
machine
import
Pin
I2C
import
utime
PWR_MGMT_1
=
0x6B
SMPLRT_DIV
=
0x19
CONFIG
=
0x1A
GYRO_CONFIG
=
0x1B
ACCEL_CONFIG
=
0x1C
TEMP_OUT_H
=
0x41
ACCEL_XOUT_H
=
0x3B
GYRO_XOUT_H
=
0x43
def
init_mpu6050
(
i2c
address
=
0x68
)
:
i2c
writeto_mem
(
address
PWR_MGMT_1
b
'\x00'
)
utime
sleep_ms
(

)
i2c
writeto_mem
(
address
SMPLRT_DIV
b
'\x07'
)
i2c
writeto_mem
(
address
CONFIG
b
'\x00'
)
i2c
writeto_mem
(
address
GYRO_CONFIG
b
'\x00'
)
i2c
writeto_mem
(
address
ACCEL_CONFIG
b
'\x00'
)
def
read_raw_data
(
i2c
addr
address
=
0x68
)
:
high
=
i2c
readfrom_mem
(
address
addr

)
[

]
low
=
i2c
readfrom_mem
(
address
addr
+

)
[

]
value
=
high
<<

low
if
value
>
32768
:
value
=
value
65536
return
value
def
get_mpu6050_data
(
i2c
)
:
temp
=
read_raw_data
(
i2c
TEMP_OUT_H
)
/
340.0
+
36.53
accel_x
=
read_raw_data
(
i2c
ACCEL_XOUT_H
)
/
16384.0
accel_y
=
read_raw_data
(
i2c
ACCEL_XOUT_H
+

)
/
16384.0
accel_z
=
read_raw_data
(
i2c
ACCEL_XOUT_H
+

)
/
16384.0
gyro_x
=
read_raw_data
(
i2c
GYRO_XOUT_H
)
/
131.0
gyro_y
=
read_raw_data
(
i2c
GYRO_XOUT_H
+

)
/
131.0
gyro_z
=
read_raw_data
(
i2c
GYRO_XOUT_H
+

)
/
131.0
return
{
'temp'
:
temp
'accel'
:
{
'x'
:
accel_x
'y'
:
accel_y
'z'
:
accel_z
}
'gyro'
:
{
'x'
:
gyro_x
'y'
:
gyro_y
'z'
:
gyro_z
}
}
MicroPython Code to measure Gyroscope, Accelerometer Values
Now let us create a simple MicroPython Code to get the value of the Gyroscope, Accelerometer in the X, Y & Z axis along with the temperature value.
Paste the following code to the Thonny Editor and save it to the Raspberry Pi Pico with the name ‘
main.py
‘.

from
machine
import
Pin
I2C
import
utime
from
mpu6050
import
init_mpu6050
get_mpu6050_data
i2c
=
I2C
(

scl
=
Pin
(

)
sda
=
Pin
(

)
freq
=
400000
)
init_mpu6050
(
i2c
)
while
True
:
data
=
get_mpu6050_data
(
i2c
)
(
"Temperature: {:.2f} °C"
format
(
data
[
'temp'
]
)
)
(
"Acceleration: X: {:.2f}, Y: {:.2

Technologies used

Raspberry PiBLEMPU6050PythonAngularI2CSPIADCMicroPython

Hardware components

  • Raspberry Pi
  • MPU6050

Software & tools

  • Thonny
  • MicroPython

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.