Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 05: Gyroscope & Accelerometer Module (MPU6050)
In this lesson, you will learn how to use the Raspberry Pi Pico W with the MPU6050 module, which combines a gyroscope and accelerometer. You’ll discover how to connect the MPU6050 to the Raspberry Pi Pico W and read its acceleration and gyroscopic data using MicroPython. The lesson will guide you through writing a script to continuously display the X, Y, and Z values of both the accelerometer and gyroscope.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Raspberry Pi Pico W |
|
Wiring
Code
Note
Open the
05_mpu6050_module.pyfile under the path ofuniversal-maker-sensor-kit-main/pico/Lesson_05_MPU6050_Moduleor copy this code into Thonny, then click “Run Current Script” or simply press F5 to run it. For detailed tutorials, please refer to Open and Run Code Directly.Here you need to use the
imu.pyandvector3d.py, please check if it has been uploaded to Pico W, for a detailed tutorial refer to Upload the Libraries to Pico.Don’t forget to click on the “MicroPython (Raspberry Pi Pico)” interpreter in the bottom right corner.
# Import libraries
from imu import MPU6050
from machine import I2C, Pin
import time
# Initialize I2C for MPU6050
i2c = I2C(1, sda=Pin(20), scl=Pin(21), freq=400000) # I2C bus 1, SDA pin 20, SCL pin 21, 400kHz
# Create MPU6050 object
mpu = MPU6050(i2c)
# Main loop to read and print sensor data
while True:
# Print accelerometer data (x, y, z)
print("-" * 50)
print("x: %s, y: %s, z: %s" % (mpu.accel.x, mpu.accel.y, mpu.accel.z))
time.sleep(0.1)
# Print gyroscope data (x, y, z)
print("X: %s, Y: %s, Y: %s" % (mpu.gyro.x, mpu.gyro.y, mpu.gyro.z))
time.sleep(0.1)
# Delay between readings
time.sleep(0.5)
Code Analysis
Importing Libraries and Initializing I2C
The code starts by importing necessary libraries. The
imulibrary is used to read the values of the MPU6050 sensor, andmachineallows controlling the hardware features of the Raspberry Pi Pico W. I2C is initialized using specific pins (SDA and SCL) for data communication.For more information about the
imulibrary, please visit micropython-IMU/micropython-mpu9x50.from imu import MPU6050 from machine import I2C, Pin import time i2c = I2C(1, sda=Pin(20), scl=Pin(21), freq=400000)
Creating MPU6050 Object
An object of the MPU6050 sensor is created by passing the initialized I2C. This object will be used to access sensor data.
mpu = MPU6050(i2c)
Reading and Printing Sensor Data in a Loop
The code then enters an infinite loop where it continually reads and prints accelerometer and gyroscope data.
time.sleepis used to create a delay between successive readings.while True: print("-" * 50) print("x: %s, y: %s, z: %s" % (mpu.accel.x, mpu.accel.y, mpu.accel.z)) time.sleep(0.1) print("X: %s, Y: %s, Y: %s" % (mpu.gyro.x, mpu.gyro.y, mpu.gyro.z)) time.sleep(0.1) time.sleep(0.5)