MPU9250 Library


#1

Hi there,
I was wondering if is possible to include the library of MPU9250.
Until now I work in Arduino IDE to program this sensor in ESP32 DevKit, but I believe this platform is stronger to do that.
Thanks.


#2

Hi @Marco_Branco,

support for the MPU9250 is not available, anyway developing a basic implementation should not be hard: you could take this Python library as a reference.

Let me know :slight_smile:


#4

Hi @LorenzoR,
I’m trying to build the MPU9250 library, however I had some problems that I cannot solve yet. Can you help me please?

The first question is: Can I change the sda and scl pins? Usually the ESP32 DevKitC have pin 25 and 26, but I need at this moment I need to define pins 21 and 22 for sda and scl, respectively. Is it possible?
Something like i2c = I2C(scl=Pin(22), sda=Pin(21)) will work?

While is not possible for to solve this pins issue I connected as recommended (pins 25 and 26) and used the library you recommend and give it the name “MPU9250”. My main code is:

from MPU9250 import MPU9250
import streams
import timers

# Constants
t=timers.timer()
t.start()
streams.serial()

#  Setup Sensor
try:
    print('Start...')
    mpu = MPU9250.MPU9250(I2C0) 
    mpu.start()
    print('MPU Activated!')
except Exception as e:
    print('ooops, something wrong:',e)

try:
    t.reset()
    while True:
        ttime = t.get()/1000
        Acc = mpu.readAccel() # this is line 42
        Gyr = mpu.readGyro()
        Mag = mpu.
        print(str(ttime), '\t', str(Acc[0]), '\t', str(Acc[1]), '\t', str(Acc[2]), '\t',
            str(Gyr[0]), '\t', str(Gyr[1]) +'\t', str(Gyr[2]), '\t',
            str(Mag[0]), '\t', str(Mag[1]) +'\t', str(Mag[2]), '\n')
except Exception as e:
     print('ooops, something wrong:',e)

I believe that at this moment I can connect to sensor, but when I try to load sensor acceleration, gyroscope or magnetometer data I have the following error:

ooops, something wrong: AttributeError at line 42 of main
raised at line 225 of MPU9250.MPU9250.readAccel

Below I leave the correspondent lines (line 225 - 233) where the error are generated, and other function that is used:

def readAccel(self):
        data = self._read(ACCEL_OUT)  # ACCEL_OUT = 0x3B
        x = self.dataConv(data[1], data[0])
        y = self.dataConv(data[3], data[2])
        z = self.dataConv(data[5], data[4])
        x = (x*self.ares)
        y = (y*self.ares)
        z = (z*self.ares)

        return {x,y,z}

def dataConv(self, data1, data2):
    value = data1 | (data2 << 8)
    if(value & (1 << 16 - 1)):
        value -= (1<<16)
    return value	

I cannot find the what is the problem.
Can you help me?
Thanks.


Help with Libraries
#5

Hello @Marco_Branco,

concerning your first question: unfortunately we do not support I2C on those pins (refer to the pinmap from Zerynth Studio).
If you really need them you could try to create a custom virtual machine following this tutorial:

Regarding the error: have you implemented the _read method?
Furthermore I suggest you to use a tuple instead of a set to return acceleration data (return (x,y,z)).

Let me know


#6

Hi @LorenzoR,
Thanks a lot for your response. Because I am not an expert, could you please tell me how can I implement the _read method?
I’m not having sucess reading the sensors because I always be stuck with an error in i2c library. For example:

in main.py I call the reading:

 Gyr = mpu.readGyro()

The error is:

ooops, something wrong: TimeoutError at line 47 of main
raised at line 243 of MPU9250.MPU9250.readGyro
raised at line 127 of i2c.read

I cannot understand what is wrong.
I try many ways to read sensors, like “_read_vector” from BNO055 library or “write_read”, and always give me an error reporting for i2c library.
Can you give an example or help?
Thanks.


#7

Hi @Marco_Branco,

here you can find an example of a complete i2c sensor library.
Please, notice that the BME280 class is child of the I2C one and, as a consequence, inherits the write_read method.

Let me know :wink: