Switching I2C bus


I have a Pro Mini on a breadboard, with 8x I2C 32x182 Oled screens attached.

They all have the same address (0x3c), and I am driving them with the <Adafruit_GFX.h>
and the <Adafruit_SSD1306.h> libraries.

I need to send data to each screen, so I found a CD4051 Multiplexer in my workshop and used that.

I am switching the SDA.

Does it matter whether I switch the SDA or SCL? I just tried it and it ‘kinda’ worked.

I can talk to the displays, but there does appear to be an issue with data occasionally appearing on the wrong screen, even once I have switched data lines.
It’s certainly a bit of a headache getting it to work.

If you write to screens 1-4 with one piece of text. Fine. But then write a different bit of text to screen 5 and it will appear on just screen 5, but along with the text you sent to screens 1-4, even though they are not selected.

Just wondered if it was due to switching the wrong data line.

Code to follow as required

SDA is data and SCL is the clock if I'm not up side down. Swiching data must be least working attempt. Is there any select pin You can use on the Oleds?
Swiching Clock might not be good either. There will be a clockpulse either during switching on or switching off that will cause trouble.

I think I may have fixed it with a 4.7k pullup.

Yes, the SDA is the data and I thought that was the better option to switch.
I was wrong to call them both data lines, as one is the clock.

It's still a bit of a nightmare to work with, as 4 of the screens will be turned 90 degrees.
This means I need to rotate the data for those screens, which isn't usually an issue. But, the graphics and text seem to behave using different points of reference when the screen is rotated.

Anyway. progress is being made.

Your (unspecified) oled screen might have a CS pin (ChipSelect).
If so, then use that to enable/disable talking to the screens.

Is there no strapping field where You can change the adress? You have not attached any information about Your displays.

They are cheapy Chinese screens. Can't even remember where I got them (probably Ebay).

No CS pin. Just 0v, 5v, SCL and SDA. Kind of like this one.

Certainly something odd going on. I need to lift the code off my workshop laptop... take out sensitive data, and tidy it up (otherwise it gets ripped to bits).
Then I can post it up.

The data I am sending to one screen seems to be transferring over to the next screen when you switch to it. Even if you wait to ensure the data was received by the first screen.

Just checked the link. It's correct, only Vcc, GND, SDA and SCL.
What China is making cheaply is what is used World Wide…. Nothing wrong with them.

I must say that I hope some more circuit knowing helper will step in and tell. There probably are multiplexers to handle the several I2C buses You need, unless You replace 7 of the displays buying the ones that can have the address set.

What the hack.... Switching like You tried… It must be done when the Clock is inactive and it must send out an inactive Clock, without gliches, to the none selected displays.

All the screens have continual SCL clock connection. Only SDA is switched.

Chinese screens.... Hmm, if from Ebay, then high possibility they are line seconds.
I have 12 of these screens here new from some old project. 2 dead and 2 were a different brightness.

Just a bit more information about the issue I have.

8 screens initialise in a for loop.

8 screens clear in a for loop.

8 screens have border drawn around them in a for loop

You select a screen using the variable 'Screen' and then a short routine sets the CD4051 output to select that screen before it rotates the screen if required. This is the first thing you call before sending display data to the screen.

So, I send time to one screen, date to another, alarm time to another etc. ClearScreen command wipes everything, so I blank the centre of the required screen before updating with a black filled rectangle command.

There are two of these black filled rectangles depending on whether its the vertical or horizontal screen.
The rotate screen command doesn't rotate the graphic co-ords like the text does. That threw me for a bit.

But, if I switch to a different screen, send some text/graphics to be displayed and then return to the clock screen, it will add that text/graphic to the clock page, even though I have changed the SDA line.

So, this data must either still be available on the I2C bus, or I am missing something.

Waiting for that data to clear doesn't help. Neither does Wire.endTransmission(); etc.

this might help:

My friend, phoneystark2020, You just can't switch the SDA!!!!!!! The Clock, SCL, will clock all the 8 displays. I'm surpriced You don't get red devils on the 7 deselected displays.
You could try and switch the SCL but I'm not sure how it will work.

Exactly what I hoped "somebody" would tell! I wagely heard about them.

OK. But how is that multiplexer from Adafruit switching data if it's not switching the SDA line?
It must be.

Maybe I need to switch both the SDA and the SCL. I will try that tomorrow

Why do You want to switch SDA? That's the data line. Turning data line off and clock is clocking.... Come on... Never switch SDA!

It's most likely switching the SCL in a secure way in that board suggested by @SteveMann

Well thats why I am here! Never switched I2C before. First post did say I guessed

Go for that I2C expander!

Right, I just changed it over to constantly connected SDA to all screens and switched the SCL instead through the multiplexer.

Doesn't work. The data is corrupted.

At least when I switched the SDA (rightly or wrongly), it was clean data and the clock ran correctly.
It just suffered ghosted characters appearing on other screens.

I will experiment tomorrow with switching both.

Oh, and no buying parts. Lost my job.... gotta use what I have

So switching SCL didn't work. Just as I suspected.
Switching SDA and getting ghost characters is just what I told You earlier.
Let's hope some other helper suggests a solution. I'm out.

When you switch out SDA or SCL, do you have a pullup resistor on the LCD side so that the lines are not floating?
If a device see SCL and SDA is just sitting high, the LCD should not be doing anything as it wouldn't have received an address it should respond to.

Same with SDA - if data is toggling on SDA, but SCL is not toggling because of the switch and pullup resistor on the LCD side, the device should not be responding.

Pullup 4k7 on each switched SDA on each screen, so idle screens never have a floating pin.
Pullup 4k7 on the commoned SCL line to all the screens.

Switching the SCL through the CD4051 results in garbage.
Switching the SDA through the CD4051 results in correct data, and MOSTLY correct, but with some data transferring to the wrong screen when it's not selected.

I slowed the routine right down and had the Serial monitor spell out where I was in the routine and what screen it was talking to, and the data definitely appears to be still available on the bus once it's changed screens.

Once the data has been received by the screen, doesn't that clear that data from the bus?

I also tried (rightly or wrongly) do{}while (digitalRead(A4)==LOW); at the end of each screen print routine.
Thought that might make it wait until the data line was high again, but nope.