yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
IMU Sensor Module📖
1. Introduction to the IMU Module
  • Introduction to IMU Module
2. Multi-master communication case
1. PC Communication
  • For PC
2. Serial communication
  • For STM32F103C8T6
  • For MSPM0G3507
  • For MSPM0G3519new
  • For ESP32
  • For Arduino
  • For Raspberry Pi Pico
  • For Raspberry Pi 5
  • For Jetson
  • For RDK
3. IIC Communication
  • For STM32F103C8T6
  • For MSPM0G3507
  • For MSPM0G3519new
  • For ESP32
  • For Arduino
  • For Raspberry Pi Pico
  • For Raspberry Pi 5
  • For Jetson
  • For RDK
3. Application Cases of Microcontroller-Based Cars
  • 1. Autonomous Driving Car
4. ROS Application Cases
ROS1 Applications
  • 1. IMU data printing
  • 2. Rviz Visualization
  • 3. IMU-integrated radar mapping and navigation
ROS2 Applications
  • 1. IMU data printing
  • 2. Rviz Visualization
  • 3. IMU-integrated radar mapping and navigation
Download
  • Code
  • VM_File
  • Docker_Image
  • 3D_Model_File
  • Hardware_DataSheet
  • Communication_Protocol
  • PC_Software
Download ZIP

Welcome to IMU Sensor Module repository

For Raspberry Pi Pico

Pico-IIC Data Reading

Pico-IIC Data Reading1. Connecting the Device2. Key Code Analysis3. Reading IMU Data

This example uses the Pico2 development board, a Windows computer, several DuPont wires, and an IMU attitude sensor.

1. Connecting the Device

IMU Attitude SensorPico2
SDAGP4
SCLGP5
GNDGND
5V3V3(OUT)

image-20251023213811826

2. Key Code Analysis

Please refer to the source code in the documentation for specific code.

x
 # Get accelerometer triaxial data, return accel=[a_x, a_y, a_z]
    def get_accelerometer_data(self):
        values = self._read_data(self.FUNC_RAW_ACCEL, 6)
        # 转化单位为g
        accel_ratio = 16 / 32767.0
        a_x = struct.unpack('h', bytearray(values[0:2]))[0]*accel_ratio
        a_y = struct.unpack('h', bytearray(values[2:4]))[0]*accel_ratio
        a_z = struct.unpack('h', bytearray(values[4:6]))[0]*accel_ratio
        accel = [a_x, a_y, a_z]
        if self._delay_time > 0:
            time.sleep(self._delay_time)
        return accel
​
  
    # Get the gyro triaxial data, return gyro=[g_x, g_y, g_z]
    def get_gyroscope_data(self):
        values = self._read_data(self.FUNC_RAW_GYRO, 6)
        # 转化单位为rad/s
        AtoR = math.pi / 180.0
        gyro_ratio = (2000 / 32767.0) * AtoR
        g_x = struct.unpack('h', bytearray(values[0:2]))[0]*gyro_ratio
        g_y = struct.unpack('h', bytearray(values[2:4]))[0]*gyro_ratio
        g_z = struct.unpack('h', bytearray(values[4:6]))[0]*gyro_ratio
        gyro = [g_x, g_y, g_z]
        if self._delay_time > 0:
            time.sleep(self._delay_time)
        return gyro
​
​
    # Get the quaternion of the IMU, return quat=[w, x, y, z]
    def get_imu_quaternion_data(self):
        values = self._read_data(self.FUNC_QUAT, 16)
        q0 = struct.unpack('f', bytearray(values[0:4]))[0]
        q1 = struct.unpack('f', bytearray(values[4:8]))[0]
        q2 = struct.unpack('f', bytearray(values[8:12]))[0]
        q3 = struct.unpack('f', bytearray(values[12:16]))[0]
        quat = [q0, q1, q2, q3]
        if self._delay_time > 0:
            time.sleep(self._delay_time)
        return quat
​
  # Get the board's attitude angle, returning euler=[roll, pitch, yaw]
# ToAngle=True returns the angle, ToAngle=False returns the radians。
    def get_imu_attitude_data(self, ToAngle=True):
        values = self._read_data(self.FUNC_EULER, 12)
        roll = struct.unpack('f', bytearray(values[0:4]))[0]
        pitch = struct.unpack('f', bytearray(values[4:8]))[0]
        yaw = struct.unpack('f', bytearray(values[8:12]))[0]
        if ToAngle:
            RtoA = 180.0 / math.pi
            roll = roll * RtoA
            pitch = pitch * RtoA
            yaw = yaw * RtoA
        euler = [roll, pitch, yaw]
        if self._delay_time > 0:
            time.sleep(self._delay_time)
        return euler
​
​
​

get_accelerometer_data(): Gets the accelerometer's three-axis data, returning accel=[a_x, a_y, a_z]

get_gyroscope_data(): Gets the gyroscope's three-axis data, returning gyro=[g_x, g_y, g_z]

get_imu_quaternion_data(): Gets the IMU's quaternion, returning quat=[w, x, y, z]

get_imu_attitude_data(): Gets the board's attitude angle, returning euler=[roll, pitch, yaw]

3. Reading IMU Data

After the program runs, the IMU data can be displayed in the terminal below.

Note: The above is for reading data from a 10-axis IMU; 6-axis data is without a magnetometer and barometer; 9-axis data is without a barometer.

For Arduino
For Raspberry Pi 5

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文