Package: micropython-mpu9250
MicroPython MPU-9250 (MPU-6500 + AK8963) I2C driverMPU-9250 is a System in Package (SiP) which combines two chips: MPU-6500 which contains 3-axis gyroscope and 3-axis accelerometer and an AK8963 which is a 3-axis digital compass.UsageKevin Wheeler has anextensive videodescribing how to use this library. The short code snippets in this README should also help you to get started. First is simple example with never ending loop.importutimefrommachineimportI2C,Pinfrommpu9250importMPU9250i2c=I2C(scl=Pin(22),sda=Pin(21))sensor=MPU9250(i2c)print("MPU9250 id: "+hex(sensor.whoami))whileTrue:print(sensor.acceleration)print(sensor.gyro)print(sensor.magnetic)print(sensor.temperature)utime.sleep_ms(1000)By default the library returns 3-tuple of X, Y, Z axis values for either acceleration, gyroscope and magnetometer ie compass. Default units arem/s^2,rad/s,uTand°C. It is possible to also get acceleration values ingand gyro valuesdeg/s. See the example below. Note that both the MPU6500 and the AK8963 drivers are available as separate classes. MPU9250 is actually a composite of those two.importutimefrommachineimportI2C,Pinfrommpu9250importMPU9250frommpu6500importMPU6500,SF_G,SF_DEG_Si2c=I2C(scl=Pin(22),sda=Pin(21))mpu6500=MPU6500(i2c,accel_sf=SF_G,gyro_sf=SF_DEG_S)sensor=MPU9250(i2c,mpu6500=mpu6500)print("MPU9250 id: "+hex(sensor.whoami))whileTrue:print(sensor.acceleration)print(sensor.gyro)print(sensor.magnetic)print(sensor.temperature)utime.sleep_ms(1000)More realistic example usage with timer. If you getOSError: 26ori2c driver install errorafter soft reboot do a hard reboot.importmicropythonfrommachineimportI2C,Pin,Timerfrommpu9250importMPU9250micropython.alloc_emergency_exception_buf(100)i2c=I2C(scl=Pin(22),sda=Pin(21))sensor=MPU9250(i2c)defread_sensor(timer):print(sensor.acceleration)print(sensor.gyro)print(sensor.magnetic)print(sensor.temperature)print("MPU9250 id: "+hex(sensor.whoami))timer_0=Timer(0)timer_0.init(period=1000,mode=Timer.PERIODIC,callback=read_sensor)Magnetometer CalibrationFor real life applications you should almost alwayscalibrate the magnetometer. The AK8963 driver supports both hard and soft iron correction. Calibration function takes two parameters:countis the number of samples to collect anddelayis the delay in millisecods between the samples.With the default values of256and200calibration takes aproximately one minute. While calibration function is running the sensor should be rotated multiple times around each axis.NOTE! If using MPU9250 you will first need to open the I2C bypass access to AK8963. This is not needed when using a standalone AK8963 sensor.frommachineimportI2C,Pinfrommpu9250importMPU9250fromak8963importAK8963i2c=I2C(scl=Pin(22),sda=Pin(21))dummy=MPU9250(i2c)# this opens the bybass to access to the AK8963ak8963=AK8963(i2c)offset,scale=ak8963.calibrate(count=256,delay=200)sensor=MPU9250(i2c,ak8963=ak8963)After finishing calibration thecalibrate()method also returns tuples for both hard ironoffsetand soft ironscale. To avoid calibrating after each startup it would make sense to strore these values in NVRAM or config file and pass them to the AK8963 constructor. Below example only illustrates how to use the constructor.frommachineimportI2C,Pinfrommpu9250importMPU9250fromak8963importAK8963i2c=I2C(scl=Pin(22),sda=Pin(21))dummy=MPU9250(i2c)# this opens the bybass to access to the AK8963ak8963=AK8963(i2c,offset=(-136.8931640625,-160.482421875,59.02880859375),scale=(1.18437220840483,0.923895823933424,0.931707933618979))sensor=MPU9250(i2c,ak8963=ak8963)Gyro CalibrationTODOLicenseThe MIT License (MIT). Please seeLicense Filefor more information.