Some magnetometer sensors are incompatible with the pytrack shield
-
Re: Problem getting MAG data from Adafruit LSM303
I found the issue with my previous post above.
There are a few common magnetometers (Adafruit LSM303, Honeywell HMC5983 and GY-282) that will not work with the pytrack shield because the magnetometers use i2c device address 0x1E which is the same device address as the accelerometer on the pytrack (LIS2HH12). -
@oldguydave said in Some magnetometer sensors are incompatible with the pytrack shield:
At least the LIS2HH12 can be hardwired to a different address, but I do not expect that the PySense board supports that. You could try to use SPI to communicate to the HMC5983.
-
Another low cost solution is to use TCA9548A 1-to-8 I2C multiplexer, so you can use up to 8 I2C devices with same I2C address.
-
@rcolistete If you have to switch devices, you can also put the other sensor on different ports and switch the bus. It should be possible to just use another Pin for SDA and share the SCL Pin.
-
@robert-hh Thanks, I've forgot that ESP32 has 2 hardware I2C buses.
So in MicroPython for Pycom devices we can use up to 2 I2C devices with same address by configuring 2 I2C buses ('i2c0 = I2C(0)' and 'i2c1 = I2C(1)', for example). -
@robert-hh said in Some magnetometer sensors are incompatible with the pytrack shield:
@rcolistete If you have to switch devices, you can also put the other sensor on different ports and switch the bus. It should be possible to just use another Pin for SDA and share the SCL Pin.
Share the SCL pin ? Within the same or different I2C bus ?
-
@rcolistete That's one way. If you do not need to use them at the very same moment, you can also use one I2C device and just switch the GPIO pins assigned to it. I nevr tried that, but a repeated call of i2c.init() should be sufficient.
-
@rcolistete If you are low on available pins, you could have one device connected to P9-SDA and P10-SCL, and the other to P8-SDA and P10-SCL, and the switch the assigned pins to the I2C like
i2c = I2C(0, I2C.MASTER) # first device i2c.init(pins="P9", "P10")) # use it # switch to second device i2c.init(pins="P8", "P10"))Eventually you have to deinit() the device before you init it again. That would also work with separate pins for SCL.
Obviously you can use both I2C controllers, assigned statically to different GPIO ports. -
@rcolistete I tried this and it did not work.
def __init__(self, pytrack=None, sda='P22', scl='P21', timeout=None): from machine import I2C self.i2c = I2C(0, mode=I2C.MASTER, pins=(sda, scl));vs.
def __init__(self, pytrack=None, sda='P9', scl='P10', timeout=None): self.i2c1 = I2C(1, mode=I2C.MASTER, pins=(sda, scl), baudrate=100000);after the second instance is declared any access (i2c.read or i2c.write)to the first instance results in the code hanging and timing out with a i2c bus error.
the second instance works fine forever since it is the last declaration in the code. -
@robert-hh @robert-hh I think this solution will work for me. I will try it and get back to you next week.
By the way i appreciate all input, you both have provided great ideas!
-
@robert-hh I have successfully implemented a solution that represents a combination of both suggestions below. Here is the way I coded my solution in the modules I created to service each I2C device:
class HMC5983(): def __init__(self): from machine import I2C self.i2c1 = I2C(1, mode=I2C.MASTER, pins=('P9', 'P10'), baudrate=100000); def mag_test(self): self.i2c1.init(pins = ('P9','P10')); utime.sleep_ms(100); self.i2c1.readfrom_mem_into(MAG_DEV_ADDR, 0x03, magData); self.i2c1.deinit(); return magData; class PYTRACK(): def __init__(self, i2c=None, sda='P22', scl='P21'): if i2c is not None: self.i2c = i2c else: self.i2c = I2C(0, mode=I2C.MASTER, pins=(sda, scl)) def _read(self): self.i2c.init(pins = ('P22','P21')); self.reg = self.i2c.readfrom(GPS_I2CADDR, 64); self.i2c.deinit(); return self.reg;Thanks for all the advice!
-
@oldguydave That looks good. by chance, I looked up today a chip called CD4052, which can switch 4 two-wire signals. You could use that to switch four I2C devices on the same address. It's an analog switch (price ~50ct), but could be used for that purpose too.
-
@robert-hh I looked at that device but I'm trying to keep my system as simple as possible so I did not pursue that option. I does, however, look like a viable alternative in the event your s/w solution was inadequate for some reason.
thanks.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login