lib for ICM20689 motionsensor for esp32

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
iwanski
Posts: 2
Joined: Tue Sep 17, 2019 9:34 am

lib for ICM20689 motionsensor for esp32

Post by iwanski » Tue Sep 17, 2019 5:03 pm

Hi there, has anyone got some sample code or even better a working library for the ICM20689 motionsensor?

With i2c.scan() I can see it's address [104] and the PRODUCTID_REG = const(0x98) gives response, but every other register that I'll try to read give's 00 back when I try to read them.

code:
# read the PRODUCTID_REG / status and Temp
whoami = self.i2c.readfrom_mem(ACC_I2CADDR, 117, 1)
print("who am i = " + str(whoami))

int_status = self.i2c.readfrom_mem(ACC_I2CADDR, 0x3A, 1)
print("status: " + str(int_status))

temp = self.i2c.readfrom_mem(ACC_I2CADDR, 0x41, 2)
print("temp: " + str(temp))

result:
I2C bus scan: [104]
who am i = b'\x98'
status: b'\x00\x00'
temp: b'\x00\x00'
Acceleration: (0.0, 0.0, 0.0)

http://www.invensense.com/wp-content/up ... .2-002.pdf

I'm new to programming MicroPython so I can use all the help i can get! please help :roll:
Thanks alot!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: lib for ICM20689 motionsensor for esp32

Post by jimmo » Wed Sep 18, 2019 12:18 am

It's worth adding constants to your code for the register numbers to make it easier to read.

Code: Select all

_REG_WHOAMI = const(0x75)
_REG_INTERRUPT_STATUS = const(0x3a)
_REG_TEMP_HIGH = const(0x41)
_REG_TEMP_LOW = const(0x42)
From the datasheet, it's plausible that on power-up the interrupt status register could be 0x00.

The temperature register, it's hard to tell from the datasheet if it does auto-increment for reading register values (it looks like it does for writes though?). You may have to issue separate reads for the high and low byte. Perhaps the low byte has data?

Also I only had a very brief look at the datasheet, but it would appear that this chip starts in sleep mode, you might have to explicitly wake it up before you can access the temperature sensor etc. Take a look at register 107 / 0x6b -- looks like if you write a zero to that register it should wake up. (Note that it does default to 0x40, which would appear to be "sleep mode").

iwanski
Posts: 2
Joined: Tue Sep 17, 2019 9:34 am

Re: lib for ICM20689 motionsensor for esp32

Post by iwanski » Wed Sep 18, 2019 5:45 am

@Jimmo, Thanks! (you're right, I promise i will tidy things up later on :) )

[quote=jimmo post_id=39517 time=1568765919 user_id=3071]
You may have to issue separate reads for the high and low byte. Perhaps the low byte has data?
[/quote]

I cannot do this in one read intruction? (i mean reading two bytes at once?) do you have some code example how to do this?

[quote=jimmo post_id=39517 time=1568765919 user_id=3071]
Take a look at register 107 / 0x6b -- looks like if you write a zero to that register it should wake up.
[/quote]
i will try this (did not noticed this, thanks!)

@Jimmo: I found this in the datasheet:
[The slave address of the ICM-20689 is b110100X which is 7 bits long. The LSB bit of the 7 bit address is determined by the logic level
on pin AD0. This allows two ICM-20689s to be connected to the same I2
C bus. When used in this configuration, the address of one of
the devices should be b1101000 (pin AD0 is logic low) and the address of the other should be b1101001 (pin AD0 is logic high).]

(does this mean that I write to address 104 but i have to read from address 105 ??? but an i2c.scan() only returns address 104) how to do this?

Thanks for your help!

rpr
Posts: 99
Joined: Sat Oct 27, 2018 5:17 pm

Re: lib for ICM20689 motionsensor for esp32

Post by rpr » Wed Sep 18, 2019 6:10 am

iwanski wrote:
Wed Sep 18, 2019 5:45 am


@Jimmo: I found this in the datasheet:
[The slave address of the ICM-20689 is b110100X which is 7 bits long. The LSB bit of the 7 bit address is determined by the logic level
on pin AD0. This allows two ICM-20689s to be connected to the same I2
C bus. When used in this configuration, the address of one of
the devices should be b1101000 (pin AD0 is logic low) and the address of the other should be b1101001 (pin AD0 is logic high).]

(does this mean that I write to address 104 but i have to read from address 105 ??? but an i2c.scan() only returns address 104) how to do this?

Thanks for your help!
The I2C interface should take care of the read/write. You only have to use the 7-bit address 104 as long as pin AD0 is held low.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: lib for ICM20689 motionsensor for esp32

Post by jimmo » Wed Sep 18, 2019 11:26 am

iwanski wrote:
Wed Sep 18, 2019 5:45 am
I cannot do this in one read intruction? (i mean reading two bytes at once?) do you have some code example how to do this?
I think you probably can, but what I was saying is that the datasheet was ambiguous -- it only explicitly said this would work for _writing_.

But if it is necessary, you would just do it as two separate reads:

Code: Select all

_REG_TEMP_HIGH = const(0x41)
_REG_TEMP_LOW = const(0x42)
temp_low = self.i2c.readfrom_mem(ACC_I2CADDR, _REG_TEMP_HIGH, 1)
temp_high = self.i2c.readfrom_mem(ACC_I2CADDR, _REG_TEMP_LOW, 1)
temp = temp_high[0] <<8 + temp_low[0]
print("temp: " + str(temp))
iwanski wrote:
Wed Sep 18, 2019 5:45 am
(does this mean that I write to address 104 but i have to read from address 105 ??? but an i2c.scan() only returns address 104) how to do this?
Yeah, exactly as rpr said, you only ever use the address returned by scan(), and the MicroPython API will take care of doing the low level bus operations to the correct read or write address.

Post Reply