guillez4
Posts: 10
Joined: Mon Apr 05, 2021 10:25 pm

I2C IMU problem

Tue May 11, 2021 5:29 pm

Hi!

Thanks in advance for your attention.
I am trying to read my IMU sensor data, I have already tried this sensor with an Arduino and it works fine. However when I use this sensor with my pico I get strange values as you see in the photos Image, Image.
In the first photo the IMU is placed flat to the surface and I get 1g of acceleration in Z axis, which is normal, but in the X,Y axis I get strange values.
Captura2.PNG
Captura2.PNG (6.47 KiB) Viewed 205 times
In the second photo the IMU is placed perpendicular to the surface and I get 1g of acceleration in Y axis, which is normal, but in the X,Z axis I get strange values.
Captura2.PNG
Captura2.PNG (6.47 KiB) Viewed 205 times
Someone could tell me of this strange behaviour?

Here is my code:

Code: Select all

import os
import utime
from machine import Pin, I2C
from micropython import const


# SETUP IMU
ISM330DHCXAddress = const(0x6B)                            # Device address
ACC_CONFIG_CTRL1_G = const(0x10)                           # Accel configuration register address
GYR_CONFIG_CTRL2_G = const(0x11)                           # Gyro configuration register address
ISM330DHCX_STATUS_REG = const(0x1E)                        # New data available register
ISM330DHCX_OUTX_L_G = const(0x22)                          # X gyro data register address
ISM330DHCX_OUTY_L_G = const(0x24)                          # Y gyro data register address
ISM330DHCX_OUTZ_L_G = const(0x26)                          # Z gyro data register address
ISM330DHCX_OUTX_L_A = const(0x28)                          # X accel data register address
ISM330DHCX_OUTY_L_A = const(0x2A)                          # Y accel data register address
ISM330DHCX_OUTZ_L_A = const(0x2C)                          # Z accel data register address
i2c=I2C(0,scl=Pin(1),sda=Pin(0),freq=100000)
i2c.writeto_mem(ISM330DHCXAddress,ACC_CONFIG_CTRL1_G, b'\x44')
i2c.writeto_mem(ISM330DHCXAddress,GYR_CONFIG_CTRL2_G, b'\x4C')

while True:
    dataAvailable=i2c.readfrom_mem(ISM330DHCXAddress,ISM330DHCX_STATUS_REG , 1)
    if dataAvailable[0]==7:
        data=i2c.readfrom_mem(ISM330DHCXAddress,ISM330DHCX_OUTX_L_G , 12)
        rawGyrX = data[0] | data[1] << 8
        rawGyrY = data[2] | data[3] << 8
        rawGyrZ = data[4] | data[5] << 8
        rawAccX = data[6] | data[7] << 8
        rawAccY = data[8] | data[9] << 8
        rawAccZ = data[10] | data[11] << 8
        
        print('AccX: ' + str(rawAccX*0.000488) + ' AccY: ' + str(rawAccY*0.000488)+' AccZ: ' + str(rawAccZ*0.000488))
        print('AccX: ' + str(rawGyrX*0.07) + ' GyrY: ' + str(rawGyrY*0.07) + ' GyrZ: ' + str(rawGyrZ*0.07)+'\n')
And here the pin conections:
Image
rasp.jpeg
rasp.jpeg (116.57 KiB) Viewed 199 times
Attachments
Captura1.PNG
Captura1.PNG (6.06 KiB) Viewed 205 times

danjperron
Posts: 3790
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: I2C IMU problem

Tue May 11, 2021 7:02 pm

Well did you forget that it is 16 bits Signed!

Code: Select all

def  toshort(v1):
    if v1 > 32767:
        return v1 - 65536
    else:
       return v1

danjperron
Posts: 3790
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: I2C IMU problem

Tue May 11, 2021 7:11 pm

or better

Code: Select all

import struct

... blablabla
... 
GyroFactor = 0.07
AccFactor = 0.000488


data=i2c.readfrom_mem(ISM330DHCXAddress,ISM330DHCX_OUTX_L_G , 12)
signed_data =  struct.unpack('<6h',bytes(data))
gX , gY , gZ = [ i * GyroFactor for i in signed_data[:3]]
accX, accY, accZ = [ i * AccFactor for i in signed_data[3:]]

Last edited by danjperron on Tue May 11, 2021 7:30 pm, edited 2 times in total.

guillez4
Posts: 10
Joined: Mon Apr 05, 2021 10:25 pm

Re: I2C IMU problem

Tue May 11, 2021 7:19 pm

And how do I convert to a 16bit signed without struct, like low level?

danjperron
Posts: 3790
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: I2C IMU problem

Tue May 11, 2021 7:31 pm

What do you think is the two last post before your last one!

Way simpler with structure.

Then check the first one I post! it is just a function to convert the 16 bits unsigned to signed toshort()

guillez4
Posts: 10
Joined: Mon Apr 05, 2021 10:25 pm

Re: I2C IMU problem

Tue May 11, 2021 8:29 pm

Thanks!

Return to “SDK”