Advertisement
microrobotics

Python MPU-9250

Mar 29th, 2023 (edited)
1,443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. #Note that this code requires the "MPU9250" library
  2. import smbus
  3. import time
  4. from MPU9250 import MPU9250
  5.  
  6. # Define the I2C bus and MPU9250 address
  7. bus = smbus.SMBus(1)
  8. address = 0x68
  9.  
  10. # Create a new MPU9250 object
  11. mpu9250 = MPU9250(bus, address)
  12.  
  13. # Initialize the MPU9250
  14. mpu9250.begin()
  15.  
  16. # Print the sensor configuration
  17. print("Accelerometer scale: {} g".format(mpu9250.get_accel_scale()))
  18. print("Gyroscope scale: {} dps".format(mpu9250.get_gyro_scale()))
  19. print("Magnetometer scale: {} uT".format(mpu9250.get_mag_scale()))
  20. print("Sample rate divider: {}".format(mpu9250.get_sample_rate_div()))
  21.  
  22. # Read and print sensor data
  23. while True:
  24.     accel_data = mpu9250.read_accel_data()
  25.     gyro_data = mpu9250.read_gyro_data()
  26.     mag_data = mpu9250.read_mag_data()
  27.  
  28.     print("Accelerometer: ({:6.2f}, {:6.2f}, {:6.2f}) g".format(*accel_data))
  29.     print("Gyroscope: ({:6.2f}, {:6.2f}, {:6.2f}) dps".format(*gyro_data))
  30.     print("Magnetometer: ({:6.2f}, {:6.2f}, {:6.2f}) uT".format(*mag_data))
  31.  
  32.     time.sleep(0.1) # Wait for 100ms before reading data again
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement