Skip to content
Snippets Groups Projects
IMU_Library.py 1.21 KiB
Newer Older
import mpu6050
import time

# Create a new Mpu6050 object
mpu6050 = mpu6050.mpu6050(0x68)

# Define a function to read the sensor data
def read_sensor_data():
    # Read the accelerometer values
    accelerometer_data = mpu6050.get_accel_data()

    # Read the gyroscope values
    gyroscope_data = mpu6050.get_gyro_data()

    # Read temp
    temperature = mpu6050.get_temp()

    return accelerometer_data, gyroscope_data, temperature

# Start a while loop to continuously read the sensor data
while True:

    # Read the sensor data
    accelerometer_data, gyroscope_data, temperature = read_sensor_data()
    
    Ax = accelerometer_data['x']
    Ay = accelerometer_data['y']
    Az = accelerometer_data['z']
    
    Gx = gyroscope_data['x']
    Gy = gyroscope_data['y']
    Gz = gyroscope_data['z']
    
    # Print the results
    print(f"Accelerometer (g): X={Ax:.8f}, Y={Ay:.8f}, Z={Az:.8f}")
    print(f"Gyroscope (°/s): X={Gx:.8f}, Y={Gy:.8f}, Z={Gz:.8f}")
    print("-" * 50)


    # Print the sensor data
#     print("Accelerometer data:", accelerometer_data)
#     print("Gyroscope data:", gyroscope_data)
#     print("--------------------")
    #print("Temp:", temperature)

    # Wait for 1 second
    time.sleep(0.1)