mpu9250 example

Active today
Viewed 17 times

so I’ve recently set up my raspberry pi. Am following a tutorial for the MPU9250, from https://github.com/tuupola/micropython-mpu9250.

I have did a install of the micropython-mpu9250, and tried the sample code in the readme to get my readings. However, i have encountered that error and cannot run my program.

I have made sure that i2c exists and is enabled using i2cdetect -y 1

Any solutions?

 New contributor
  • Welcome @GGenesis, Nice to meet you. I have drafted an answer for you. Please feel free to ask any newbie questions. Happy microPython programming and cheers! 🙂 – tlfong01 2 hours ago    

2 Answers


It’s preferable that the code listing is in your question and that the error message is cut&pasted into your question.

At a guess you should be importing I2C not i2c.

  • alright, will take note ! thank you ! 🙂 have tried both but have not worked out yet, but i will try the suggested answer ! 🙂 – GGenesis 38 mins ago




(1) I have installed micropython-mpu9250.

(2) I have detected the i2c bus using “i2cdetect -y 1”.


How come my program don’t run?



(1) What does the command “i2cdetect …” do?

The commend “i2cdetect -y 1” is used to detect if there is any working I2C devices connected to the bus, and display the devices’ address as two hexadecimal digits.

For example, if you have a device MPU9250 with address “0x68”. So i2cdetect -y 1 should display at the “60” (7th row) row two digits “68”.

If you have installed a driver for the device MPU9250, for example, by using the text editor “nano” editing the file /boot/config.txt by adding a line like this:

dtoverlay=i2c ... 

then i2cdetect might display “UU” instead of “68”.

And even i2cdect detects the device, it does not mean the device should work correctly. It only means i2cdetect “pings” (say hello to) the device, and the device replies “I am here”. In other words, MPU9250 only says he is present, not guaranteeing that he is OK to work.

(2) Importing micropython python modules

First you need to import the correct python modules. Below are the example statements from the microPython MPU9250 I2C Driver Git HubGitHub:

import micropython
import utime
from machine import I2C, Pin, Timer
from mpu9250 import MPU9250

Note that the example is not using the Rpi default I2C pins GPIO 2, 3 (40 pin header physical pin numbers 3, 5). This implies the usual import command “import smbus” won’t work. You must first import “micropython”, then from “machine import I2C …”.

(3) Setting up an I2C bus and give it a name, eg “i2c123”

Then you need to setup the i2c bus, like the following example in gitHub:

i2cBusMotherAmy = I2C(scl = Pin(22), sda = Pin(21))

(4) Creating an object (instance) of the micropython class for device MPU9250

Now the time has come for the I2C bus mother “i2cMotherAmy” to “give birth” a new mpu9050 “baby”,and give him a name, eg “mpu9250BabyJonathan”:

mpu925BabyJonathan = MPU9250(i2cBusMotherAmy)

Now you can “teach” baby MPU9250 Jonathan to do something!

(5) Other possible I2C programming problems

I have been using Rpi3B+ stretch playing with I2C 3/6/9 DOF sensors and found a big program, ie, for Rpi3B+, I2C speed is a flat rate of 100kHz and cannot be changed (though “official” instructions says otherwise!). Furthermore, the python I2C (smbus) module does not entertain “bus stretching” which is required in some cases. A get around is to lower I2C speed, but not for Rpi3B+.

To solve the problem, you need to use Rpi4B buster, which allows lower I2C speed to as low as 10kHz, and problem solved. See Reference 4 below for more details.

(6) MPU9250 make easy for newbies

There is not that many tutorials for 9-DOF MPU9250. For newbies I would recommend to try 6-DOF MPU6500, or even 3-DOF sensors. MPU9250 actually has MPU6500 and another 3-DOF magneto sensor glued together inside (Ref 3, Appendix A (the pink and orange rectangles of MPU9250 block diagram)). So the knowledge and skills acquired in MPU6500 can directly transfer to MPU9250.

In other words, eat the big 9-DOF elephant bite by bite, in three 3-DOF bites. This way, the very steep MPU9250 learning curve becomes three shallow ones.


(1) MicroPython MPU-9250 (MPU-6500 + AK8963) I2C driver GitHub

(2) MPU9250 Datasheet – Invensense

(3) MPU9250/MPU6250 Newbie Advice – tlfong01 2018nov

(4) Rpi4B buster BNO055 9-DOF Sensor and 16 Channel PWM Driver CircuitPython Programming Problem – tlfong01 2019jun

(5) MPU9250 Learning Notes – tlfong01 2019mar


Appendix A – MPU9250 Block Diagram

mpu9250 block diagram

Appendix B – MPU9250 Module Schematic


End of answer

  • hi, thank you so much !! 🙂 will try and update if needed ! yes i’m new to this hahah so appreciate it ! – GGenesis 39 mins ago
  • @GGenesis, you are welcome. Happy programming. Cheers. – tlfong01 12 mins ago   

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: