IMU Sample

This sample demonstrates reading 3D gyro and acceleration data.

To run the sample:

  1. Copy the sample code to a folder on the Raspberry Pi.

  2. Open terminal, and then go to the folder you copied the sample.

  3. Run the sample with the following command:

python3 IMU.py

Sample Code

IMU.py
#!/usr/bin/env python3

#This sample demonstrates reading 3D gyro and acceleration data.
#Install RC Driver HAT library with "pip3 install turta-rcdriverhat"

from time import sleep
from turta_rcdriverhat import Turta_IMU

#Initialize
imu = Turta_IMU.IMU()

try:
    while True:
        #Read gyro & accel data
        imu_all = imu.read_gyro_accel_xyz()

        #Print the readings
        print("Pitch.....: " + str(imu_all[0]).zfill(5))
        print("Roll......: " + str(imu_all[1]).zfill(5))
        print("Yaw.......: " + str(imu_all[2]).zfill(5))
        print("X-Accel...: " + str(imu_all[3]).zfill(5))
        print("X-Accel...: " + str(imu_all[4]).zfill(5))
        print("Z-Accel...: " + str(imu_all[5]).zfill(5))

        #Wait
        sleep(0.1)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it prints pitch, roll, yaw and acceleration data. The example output should be like this:

Pitch.....: 00012
Roll......: 00045
Yaw.......: 00023
X-Accel...: -0124
X-Accel...: 00453
Z-Accel...: 00172

To stop the Python script, just press CTRL + C.

Last updated