twiggyxlr4
Posts: 45
Joined: Fri Jun 18, 2021 12:59 am

TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 12:46 pm

Hi everyone!

Over the past days I have been looking in vain over Google, YouTube, Reddit and other sites for info on how to use a TCA9548A multiplexer with two or more ssd1306 oled displays (128x32), but all I could find was Arduino and c++ code, but I work with python code ...

I've stumbled on a few TCA9548A python libraries, but not much documentation on them, so kind of useless .
I would appreciate any help I could get !

ame
Posts: 3742
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 5:08 pm

Specifically, what have you tried? What happened? What did you expect?

twiggyxlr4
Posts: 45
Joined: Fri Jun 18, 2021 12:59 am

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 8:08 pm

I am trying to use 2 or 3 oled displays (ssd1306) through this multiplexer since they all have the same address (3c)
When I run

Code: Select all

sudo i2cdetect -y 1 
I get the multiplexer address as 70 .
My problem now is finding a python library and probably a few examples that could show me how to effectively use this tca9548a multiplexer

ame
Posts: 3742
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 8:17 pm

Ok, but you wrote
I've stumbled on a few TCA9548A python libraries, but not much documentation on them, so kind of useless .
What was the most promising one, or the one you had highest expectations of, or the one you made the most progress with?

Everyone is going to Google "TCA9548A python" and ask you "did you try this?".

Think about what is happening. When the display is connected directly to the Pi i2c port you wrote a program that sent bytes to a specific i2c address.

Now, you have to send bytes to the multiplexer, and the multiplexer will send them on to the display. So, at the minimum you have to specify the multiplexer address, the multiplexer output port, and the display address on that port. Since there is no magic, you're going to have to do this yourself. But understanding what is going on should help you figure out what is missing.

It's good that you can detect the multiplexer itself.

ame
Posts: 3742
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 8:51 pm

Ok, I've done some reading (including a useful thread on this very forum).

My conclusion is that the multiplexer becomes "transparent" on the bus. Someone suggested it's like a rotary switch with eight positions. Just set the switch to the channel you want to use, then do I2C transactions with a device as you would if the device was directly connected.

Only one wrinkle: the multiplexer does not have pull-ups on board, so if your device does not have pull-ups you should add them. Don't forget, there should really only be one set of pull-ups on any individual I2C bus, and on the Pi, pull-ups are already present on the native I2C bus pins.

So, in your case you would attach the displays to three different channels of the multiplexer. Let's say 0, 1, and 2. Now, write to address 0x70 (the address of the multiplexer) to set which channel you are using (0, 1, or 2). Then, the display will be accessible at address 0x3C, so the code you previously used should just work.

Original code:
Write to display at address 0x3C

New code:
Write to multiplexer at address 0x70 to select channel 0
Write to display 0 at address 0x3C
Write to multiplexer at address 0x70 to select channel 1
Write to display 1 at address 0x3C
Write to multiplexer at address 0x70 to select channel 3
Write to display 3 at address 0x3C

Note that the display address is always 0x3C, but the actual physical display that you are writing to depends on which channel you chose on the multiplexer.

twiggyxlr4
Posts: 45
Joined: Fri Jun 18, 2021 12:59 am

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 8:58 pm

Nice of them had any real documentation

This is my code that uses dht22 sensor and ssd1306 display:

Code: Select all

#!/usr/bin/python3
from PIL import Image, ImageDraw, ImageFont
from gpiozero import LED
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import Adafruit_DHT

redLed = LED(23)
blueLed = LED(25)
greenLed = LED(18)
redLed.off()
blueLed.off()
greenLed.off()
sensor = Adafruit_DHT.DHT22
pin =4



# Raspberry Pi pin configuration:
RST = 24
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0

disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
# Initialize library.
#disp.begin()

# Clear display.
#disp.clear()
#disp.display()



font = ImageFont.truetype('/home/pi/pythonFiles/segoe.ttf', 38)
disp.begin()
disp.clear()
while True :
    try:
        #time.sleep(0.6)
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        
        if int(temperature) in range(16,21):
            greenLed.off()
            redLed.off()
            blueLed.on()
        if int(temperature) in range(21,24):
            greenLed.on()
            redLed.off()
            blueLed.off()
        if int(temperature) in range(24,50):
            redLed.on()
            blueLed.off()
            greenLed.off()
        #print(type(temperature))
        time.sleep(0.1)
        img = Image.open('back128.png').convert('1')
        d1 = ImageDraw.Draw(img)
        d1.text((20, 3), "{0:0.1f}*C {1:0.1f}%".format(temperature, humidity)[0:6], fill=(255), font=font)
        #print("Temp={0:0.1f}*C  Humidity={1:0.1f}%".format(temperature, humidity))
        # Display image.
        #time.sleep(1)
        
        disp.image(img)
        
        disp.display()
        time.sleep(20)
        disp.clear()
        #time.sleep(2)
        img2 = Image.open('back128.png').convert('1')
        d2 = ImageDraw.Draw(img2)
        d2.text((2, 3), "{0:0.1f}*C {1:0.1f}%H".format(temperature, humidity)[7:13], fill=(255),font=font)
        disp.image(img2)
        
        disp.display()
        time.sleep(10)
        disp.clear()
        
        #d1.text((5,5),text=' ',fill=(255))
        #time.sleep(0.1)
    except:
        print('error')
        disp.clear()
        continue

One library i've dOwnloaded , but had vEry few docs adafruit_tca9548a

ame
Posts: 3742
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 9:09 pm

Ok. Based on this:
https://stackoverflow.com/questions/413 ... c-tca9548a

I suggest you try the code in the first answer:

Code: Select all

#!/usr/bin/python
# intended for import to change channel of TCA9548A
# import then call I2C_setup(multiplexer_addr,multiplexer_channel)

import smbus
import time
import sys

channel_array=[0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000]

def I2C_setup(multiplexer,i2c_channel_setup):
    bus = smbus.SMBus(1)
    bus.write_byte(multiplexer,channel_array[i2c_channel_setup])
    time.sleep(0.01)
    #uncomment to debug #print("TCA9548A I2C channel status:", bin(bus.read_byte(multiplexer)))
And in the same directory:

Code: Select all

#!/usr/bin/python

import TCA9548A

# set specific multiplexer to a specific channel
# TCA9548A.I2C_setup( multiplexer_addr , multiplexer_channel )
TCA9548A.I2C_setup(0x70,0)
If you run this it should set the multiplexer to channel 0, so i2cdetect should see the multiplexer (at address 0x70) and any devices attached to channel 0. Basically, once you have set the channel it should remain at that setting, so you can just run the above script with different arguments to TCA9548A.I2C_setup().

So, with this script and your original code for driving the display you should be able to wire up the displays to three different channels, select each one in turn and run your original code without modification.

ame
Posts: 3742
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 9:17 pm

I suspect the lack of documentation is because it's such a simple device, and there's very little to do. You don't even need a driver or library. Once you've wired it up you just do this:

i2c.write(0x70, 1 << n)

Where 0x70 is the address of the multiplexer, n is the channel number from 0-7, and i2c.write() is whatever function you have for sending data onto the bus.

twiggyxlr4
Posts: 45
Joined: Fri Jun 18, 2021 12:59 am

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 9:54 pm

I will give it a try tomorrow morning when I get back home. Had a read through the guide. Seems fairly well documented so far.
Hopefully by tomorrow night it will be up and running!
Thank you !
Any other hints or guides would be appreciated!

twiggyxlr4
Posts: 45
Joined: Fri Jun 18, 2021 12:59 am

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 10:35 pm

I have tested your TCA9548A.py , tried setting different channels on my module and it seems to be working fine.
Milestone 1 achieved lol. Tomorrow I will try and implement the rest of my code and test it out.

ame
Posts: 3742
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 11:14 pm

Excellent. I'm glad you are making progress.

That's not my code by the way. It's copied from the Stackexchange link.

twiggyxlr4
Posts: 45
Joined: Fri Jun 18, 2021 12:59 am

Re: TCA9548A and multiple ssd1306 displays

Tue Sep 28, 2021 11:22 pm

I know it's not yours , but very much appreciated !

twiggyxlr4
Posts: 45
Joined: Fri Jun 18, 2021 12:59 am

Re: TCA9548A and multiple ssd1306 displays

Wed Sep 29, 2021 2:41 pm

ame wrote:
Tue Sep 28, 2021 11:14 pm
Excellent. I'm glad you are making progress.

That's not my code by the way. It's copied from the Stackexchange link.
Works like a charm ! Thank you so much for helping me out with this one ! (Bet I am not the only one in this situation)
Now onto rebuilding the code to include 3 displays !
Thank you for your support !

ame
Posts: 3742
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: TCA9548A and multiple ssd1306 displays

Wed Sep 29, 2021 3:10 pm

Great! Glad to hear it's working.

twiggyxlr4
Posts: 45
Joined: Fri Jun 18, 2021 12:59 am

Re: TCA9548A and multiple ssd1306 displays

Wed Sep 29, 2021 5:54 pm

ame wrote:
Wed Sep 29, 2021 3:10 pm
Great! Glad to hear it's working.
Got it working great now, have set up all 3 displays, each displaying it's own thing !

Thank you very much once again!

Return to “Python”