I2C multiplexer library?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
asuagar
Posts: 1
Joined: Mon Apr 15, 2019 10:01 am

I2C multiplexer library?

Post by asuagar » Mon Apr 15, 2019 10:16 am

Hello,

Is there a Micropython library for the TCA9548A or another multiplexer device? There is a library for the TCA9548A in CircuitPython. However, I am afraid that it will not work in MicroPython. Is there other way for multilplexing using MicroPython?

Thanks & regards!

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: I2C multiplexer library?

Post by Roberthh » Mon Apr 15, 2019 2:00 pm

Circuitpython is a Micropython variant. The only thing to adapt is typcially the way it speaks to I2C. There are minor differences in the I2C API. I have sucessfully ported Adafruit libraries, and it was usually easy.

doublevee
Posts: 75
Joined: Mon Jul 02, 2018 11:09 pm

Re: I2C multiplexer library?

Post by doublevee » Sun Apr 21, 2019 10:50 am

I have been using this mutliplexer in a product I am building and it is very easy to use. I'm not sure it needs a library at all? You select the I2C address for the mux using 3 external pins (can be IO driven or just hard wired). You then just write a single command to that address to select the output channel of the mux. The mux remains on that channel until either a power cycle or you issue a new channel command.

In brief, once you have your mux setup, it's a very small amount of code to add to select a particular device - almost a single line depending on your use case.

Happy to help further if needs be. I'm no expert at all but happy to try and help if needed.

sequel
Posts: 14
Joined: Wed Aug 03, 2016 1:52 pm

Re: I2C multiplexer library?

Post by sequel » Thu Oct 17, 2019 2:15 pm

I am trying to use a tca9548a to drive 2 temperature/humidity sensors (si7021) on an esp32 board, using the CircuitPython code (https://github.com/adafruit/Adafruit_Ci ... n_TCA9548A) and, i have no success so far.

Here's what i have been doing so far :

Code: Select all


import machine
import si7021
import tca9548a

scl_pin=22
sda_pin=21

i2c=machine.I2C(-1, machine.Pin(scl_pin), machine.Pin(sda_pin))

tca=tca9548a.TCA9548A(i2c)

As soon as i am trying to reach a sensor like this :

Code: Select all


si7021_obj1=si7021.SI7021(tca[0])

I am getting :

Code: Select all


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/si7021.py", line 46, in __init__
  File "/lib/si7021.py", line 50, in init
  File "/lib/si7021.py", line 87, in reset
  File "/lib/si7021.py", line 66, in _command
  File "/lib/tca9548a.py", line 39, in writeto
OSError: [Errno 19] ENODEV

I am not sure how to proceed to debug this further. The sensors are working great without the multiplexer.

@ doublevee

Could you kindly post some code snippets on how you manually use this multiplexer? That would help me troubleshoot further.

Thanks in advance.

sequel
Posts: 14
Joined: Wed Aug 03, 2016 1:52 pm

Re: I2C multiplexer library?

Post by sequel » Thu Oct 17, 2019 5:35 pm

Finally got it working with this simple code, if that can help someone :

tca9548a_test.py

Code: Select all

import machine
import ustruct

scl_pin=22
sda_pin=21

class TCA9548A():
    def __init__(self,address):
        self.address=address
        self.bus=machine.I2C(-1, machine.Pin(scl_pin), machine.Pin(sda_pin))

    def switch_channel(self,channel):
        self.bus.writeto(self.address,ustruct.pack('B',1 << channel))
and then :

Code: Select all

import tca9548a_test
import si7021

tca=tca9548a_test.TCA9548A(0x70)

for channel in range(0,2):
    tca.switch_channel(channel)
    sensor=si7021.SI7021(tca.bus)
    print('humidity on sensor # {} = {}%'.format(channel, sensor.humidity()))
    print('temperature on sensor # {} = {}°C'.format(channel, sensor.temperature()))
will give something like this :

humidity on sensor # 0 = 50.7894%
temperature on sensor # 0 = 19.28095°C
humidity on sensor # 1 = 57.13706%
temperature on sensor # 1 = 19.32385°C


Cheers!

Post Reply