0

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?

  • 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 Oct 30 '19 at 1:44
0

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.

| improve this answer | |
  • alright, will take note ! thank you ! :) have tried both but have not worked out yet, but i will try the suggested answer ! :) – GGenesis Oct 30 '19 at 4:03
-1

Question

Setup

(1) I have installed micropython-mpu9250.

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

Problem

How come my program don't run?


Answer

i2cdetect


(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, and give it a name, say "Nile", like the following example in gitHub:

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

Here, I2C is the class which can create/instantiate objects, and "i2cBustNile" is the bus object.


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

Now the time has come for the class "MCU9250" to "give birth" a new mpu9050 "baby" object,and give him a name, eg "mpu9250BabyJonathan":

mpu925BabyJonathan = MPU9250(i2cBusNile)

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.


References

(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


Appendices

Appendix A - MPU9250 Block Diagram

mpu9250 block diagram


Appendix B - MPU9250 Module Schematic

schematic


Appendix C - MicroPython PyBoard and AdaFruit Circuit Python Modules

I started learning microPython in 2015, using PyBoard V1.0, then BBC MicroBit. I found micropython's REPL (Read-Evaluate-Print-Loop) and Mu Editor (also available in Rpi4B GUI Desktop) very slow, so I switched to Rpi IDLE python 3.5.3 and now Thonny 3.7.3 which I found good. For absolute newbies, I would recommend AdaFruit CircuitPython Modules. (You can install microPython application in Rpi, but again there are many version incompatibility problems. :()

pyboard


End of answer

| improve this answer | |
  • hi, thank you so much !! :) will try and update if needed ! yes i'm new to this hahah so appreciate it ! – GGenesis Oct 30 '19 at 4:02
  • @GGenesis, you are welcome. Happy programming. Cheers. – tlfong01 Oct 30 '19 at 4:29
  • Hello ! I'm still having the issue of no module found ! if I did an import micropython at the start, using python test.py shows ImportError : no module micropython found. However, by using the command micropython test.py, i get the error ImportError : cannot import name I2C . – GGenesis Oct 30 '19 at 6:49
  • @GGenesis, Well, if the "import micropython" statement of your test.py program shows ImportError, then there might be some version problem. Please confirm which micropython version you are using. Does your micropython starts with something like below: "MicroPython v1.11-126-g7c2e83324 on 2019-07-07; linux version Use Ctrl-D to exit, Ctrl-E for paste mode >>> "? – tlfong01 Oct 30 '19 at 7:48
  • Hello ! Will update you as soon as I can work on my project :) ! Meanwhile, i brought this issue up with the git and they told me UNIX doesn't support i2c – GGenesis Oct 31 '19 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.