SSD1306 and TCA9548A not working with multiple displays


```cpp
/*#include "Adafruit_GFX.h"
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
*/
#include <Adafruit_SSD1306.h>
#include <spi.h>
#include <Wire.h>



#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display3(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display4(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display5(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
/*
  Tell DCS-BIOS to use a serial connection and use the default Arduino Serial
  library. This will work on the vast majority of Arduino-compatible boards,
  but you can get corrupted data if you have too many or too slow outputs
  (e.g. when you have multiple character displays), because the receive
  buffer can fill up if the sketch spends too much time updating them.
  
  If you can, use the IRQ Serial connection instead.
*/
#define DCSBIOS_DEFAULT_SERIAL
#define ENABLE_PULLUPS
#include "DcsBios.h"

/*typedef DcsBios::RotaryEncoderT<POLL_EVERY_TIME, DcsBios::FOUR_STEPS_PER_DETENT> FourStepRotaryEncoder; // A custom rotary encoder with four quadrature steps per physical detent.
FourStepRotaryEncoder tacan1("TACAN_1", "DEC", "INC", 0, 1);*/


void TCA9548A (uint8_t bus){
Wire.beginTransmission(0x70);
Wire.write(1<<bus);
Wire.endTransmission();

}



// cue
void onUfcOptionCueing1Change(char* newValue) {
  TCA9548A(0);

  display1.setCursor(0, 6);
  display1.print (newValue);
  display1.display();
}

DcsBios::StringBuffer<1> ufcOptionCueing1Buffer(FA_18C_hornet_UFC_OPTION_CUEING_1, onUfcOptionCueing1Change);

void onUfcOptionCueing2Change(char* newValue) {
  TCA9548A(1);

  display2.setCursor(0, 6);
  display2.print (newValue);
  display2.display();
}

DcsBios::StringBuffer<1> ufcOptionCueing2Buffer(FA_18C_hornet_UFC_OPTION_CUEING_2, onUfcOptionCueing2Change);

void onUfcOptionCueing3Change(char* newValue) {
  TCA9548A(2);

  display3.setCursor(0, 6);
  display3.print (newValue);
  display3.display();
}

DcsBios::StringBuffer<1> ufcOptionCueing3Buffer(FA_18C_hornet_UFC_OPTION_CUEING_3, onUfcOptionCueing3Change);

void onUfcOptionCueing4Change(char* newValue) {
  TCA9548A(3);

  display4.setCursor(0, 6);
  display4.print (newValue);
  display4.display();

}

DcsBios::StringBuffer<1> ufcOptionCueing4Buffer(FA_18C_hornet_UFC_OPTION_CUEING_4, onUfcOptionCueing4Change);

void onUfcOptionCueing5Change(char* newValue) {
  TCA9548A(4);

  display5.setCursor(0, 6);
  display5.print (newValue);
  display5.display();
}

DcsBios::StringBuffer<1> ufcOptionCueing5Buffer(FA_18C_hornet_UFC_OPTION_CUEING_5, onUfcOptionCueing5Change);

  
  // end of cueing

  //-------------------------------------------


  //option

void onUfcOptionDisplay1Change(char* newValu6e) {
  TCA9548A(0);

 display1.setCursor(30, 6);
display1.print (newValu6e);
display1.display();
}
DcsBios::StringBuffer<4> ufcOptionDisplay1Buffer(FA_18C_hornet_UFC_OPTION_DISPLAY_1, onUfcOptionDisplay1Change);


void onUfcOptionDisplay2Change(char* newValu6e) {
  TCA9548A(1);

 display2.setCursor(30, 6);
display2.print (newValu6e);
display2.display();
}
DcsBios::StringBuffer<4> ufcOptionDisplay2Buffer(FA_18C_hornet_UFC_OPTION_DISPLAY_2, onUfcOptionDisplay2Change);


void onUfcOptionDisplay3Change(char* newValu6e) {
  TCA9548A(2);

 display3.setCursor(30, 6);
display3.print (newValu6e);
display3.display();
}
DcsBios::StringBuffer<4> ufcOptionDisplay3Buffer(FA_18C_hornet_UFC_OPTION_DISPLAY_3, onUfcOptionDisplay3Change);


void onUfcOptionDisplay4Change(char* newValu6e) {
  TCA9548A(3);

   display4.setCursor(30, 6);
display4.print (newValu6e);
display4.display();
}
DcsBios::StringBuffer<4> ufcOptionDisplay4Buffer(FA_18C_hornet_UFC_OPTION_DISPLAY_4, onUfcOptionDisplay4Change);


void onUfcOptionDisplay5Change(char* newValu6e) {
  TCA9548A(4);

 display5.setCursor(30, 6);
display5.print (newValu6e);
display5.display();
}
DcsBios::StringBuffer<4> ufcOptionDisplay5Buffer(FA_18C_hornet_UFC_OPTION_DISPLAY_5, onUfcOptionDisplay5Change);

void setup() {
  Serial.begin (9600);
   Wire.begin();

   TCA9548A(0);
display1.clearDisplay();
  display1.setTextSize(4);
display1.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
 display1.setTextColor(WHITE, BLACK);

TCA9548A(1);
display2.clearDisplay();
  display2.setTextSize(4);
display2.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
 display2.setTextColor(WHITE, BLACK);

  TCA9548A(2);
display3.clearDisplay();
display3.setTextSize(4);
display3.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
 display3.setTextColor(WHITE, BLACK);

 TCA9548A(3);
display4.clearDisplay();
 display4.setTextSize(4);
display4.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
 display4.setTextColor(WHITE, BLACK);


  TCA9548A(4);
display5.clearDisplay();  
display5.setTextSize(4);
display5.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
   display5.setTextColor(WHITE, BLACK);


 
DcsBios::setup();
}


void loop(){

  DcsBios::loop();
  }


Hi.
I have 5 SSD1306 displays connected to an TCA9548A and that to the arduino nano clone.
My wiring is correct because if i upload a copy of this code but with only one Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); instead of 5 and i change everything bellow to display.(whatever setCursor, print, etc.) all screens turn on but what i'm passing to them from the game (DCS) is repeated or distributed by several screens.
The way i have it in this example it usually only turns on 1 display and many times it only shows garbled pixels.


note: all displays are also connected grd-grd and vin-5v

Can You post a link to the datasheet of the display?
How is the controller powered?

the display where bought on aliexpress, i can't find the datasheet.
Everything is powered through usb

That's not good.
Make a test connecting 2 displays, then 3,... and tell what it gives.
I suspect USB 5 volt might be overloaded.

without changing the code, just unplug one or two displays?

i uploaded this sketch, made some changes and they all worked

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/tca9548a-i2c-multiplexer-esp32-esp8266-arduino/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_SSD1306 display3(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_SSD1306 display4(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Select I2C BUS
void TCA9548A(uint8_t bus){
  Wire.beginTransmission(0x70);  // TCA9548A address
  Wire.write(1 << bus);          // send byte to select bus
  Wire.endTransmission();
  Serial.print(bus);
}

void setup() {
  Serial.begin(115200);

  // Start I2C communication with the Multiplexer
  Wire.begin();

  // Init OLED display on bus number 2 (display 1)
  TCA9548A(2);
  if(!display1.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  } 
  // Clear the buffer
  display1.clearDisplay();

  // Init OLED display on bus number 3
  TCA9548A(3);
  if(!display2.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  } 
  // Clear the buffer
  display2.clearDisplay();

  // Init OLED display on bus number 4
  TCA9548A(4);
  if(!display3.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  } 
  // Clear the buffer
  display3.clearDisplay();

  // Init OLED display on bus number 5
  TCA9548A(5);
  if(!display4.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  } 
  // Clear the buffer
  display4.clearDisplay();

  // Write to OLED on bus number 2
  TCA9548A(2);
  display1.setTextSize(8);
  display1.setTextColor(WHITE);
  display1.setCursor(45, 10);
  // Display static text
  display1.println("1");
  display1.display(); 
  
  // Write to OLED on bus number 3
  TCA9548A(3);
  display2.setTextSize(8);
  display2.setTextColor(WHITE);
  display2.setCursor(45, 10);
  // Display static text
  display2.println("2");
  display2.display(); 
  
  // Write to OLED on bus number 4
  TCA9548A(4);
  display3.setTextSize(8);
  display3.setTextColor(WHITE);
  display3.setCursor(45, 10);
  // Display static text
  display3.println("3");
  display3.display(); 
  
  // Write to OLED on bus number 5
  TCA9548A(5);
  display4.setTextSize(8);
  display4.setTextColor(WHITE);
  display4.setCursor(45, 10);
  // Display static text
  display4.println("4");
  display4.display();
}
 
void loop() {
  
}

Yes, try that. DIsconnect display 5 vol , SDA and SCL.


```cpp
#include <Adafruit_SSD1306.h>
#include <Wire.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define DCSBIOS_DEFAULT_SERIAL
#define ENABLE_PULLUPS
#include "DcsBios.h"

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display3(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display4(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display5(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);




void TCA9548A (uint8_t bus){
Wire.beginTransmission(0x70);
Wire.write(1<<bus);
Wire.endTransmission();
//Serial.print(bus);
}



// cue
void onUfcOptionCueing1Change(char* newValue) {
  TCA9548A(0);



    display1.setTextSize(2);

 display1.setTextColor(WHITE, BLACK);
  display1.setCursor(0, 6);
  display1.print (newValue);
 display1.display(); 
}

DcsBios::StringBuffer<1> ufcOptionCueing1Buffer(FA_18C_hornet_UFC_OPTION_CUEING_1, onUfcOptionCueing1Change);

void onUfcOptionCueing2Change(char* newValue) {
  TCA9548A(1);
 
  display2.setTextSize(2);
  display2.setTextColor(WHITE, BLACK);
  display2.setCursor(0, 6);
  display2.print (newValue);
  display2.display();
}

DcsBios::StringBuffer<1> ufcOptionCueing2Buffer(FA_18C_hornet_UFC_OPTION_CUEING_2, onUfcOptionCueing2Change);

void onUfcOptionCueing3Change(char* newValue) {
  TCA9548A(2);
 //display3.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display3.setTextSize(2);

 display3.setTextColor(WHITE, BLACK);

  display3.setCursor(0, 6);
  display3.print (newValue);
display3.display();
}

DcsBios::StringBuffer<1> ufcOptionCueing3Buffer(FA_18C_hornet_UFC_OPTION_CUEING_3, onUfcOptionCueing3Change);

void onUfcOptionCueing4Change(char* newValue) {
  TCA9548A(3);
 // display4.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display4.setTextSize(2);

 display4.setTextColor(WHITE, BLACK);
  display4.setCursor(0, 6);
  display4.print (newValue);
display4.display();

}

DcsBios::StringBuffer<1> ufcOptionCueing4Buffer(FA_18C_hornet_UFC_OPTION_CUEING_4, onUfcOptionCueing4Change);

void onUfcOptionCueing5Change(char* newValue) {
  TCA9548A(4);
//display5.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display5.setTextSize(2);

   display5.setTextColor(WHITE, BLACK);
  display5.setCursor(0, 6);
  display5.print (newValue);
display5.display();
}

DcsBios::StringBuffer<1> ufcOptionCueing5Buffer(FA_18C_hornet_UFC_OPTION_CUEING_5, onUfcOptionCueing5Change);

  
  // end of cueing

  //-------------------------------------------


  //option

void onUfcOptionDisplay1Change(char* newValu6e) {
  
  TCA9548A(0);
  // display1.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
    display1.setTextSize(2);

 display1.setTextColor(WHITE, BLACK);
 display1.setCursor(30, 6);
display1.print (newValu6e);
display1.display();
}
DcsBios::StringBuffer<4> ufcOptionDisplay1Buffer(FA_18C_hornet_UFC_OPTION_DISPLAY_1, onUfcOptionDisplay1Change);


void onUfcOptionDisplay2Change(char* newValu6e) {
  TCA9548A(1);
 //display2.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
    display2.setTextSize(2);

 display2.setTextColor(WHITE, BLACK);
 display2.setCursor(30, 6);
display2.print (newValu6e);
display2.display();
}
DcsBios::StringBuffer<4> ufcOptionDisplay2Buffer(FA_18C_hornet_UFC_OPTION_DISPLAY_2, onUfcOptionDisplay2Change);


void onUfcOptionDisplay3Change(char* newValu6e) {
  TCA9548A(2);
  //display3.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display3.setTextSize(2);

 display3.setTextColor(WHITE, BLACK);

 display3.setCursor(30, 6);
display3.print (newValu6e);
display3.display();
}
DcsBios::StringBuffer<4> ufcOptionDisplay3Buffer(FA_18C_hornet_UFC_OPTION_DISPLAY_3, onUfcOptionDisplay3Change);


void onUfcOptionDisplay4Change(char* newValu6e) {
  TCA9548A(3);
  //display4.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display4.setTextSize(2);

 display4.setTextColor(WHITE, BLACK);
   display4.setCursor(30, 6);
display4.print (newValu6e);
display4.display();
}
DcsBios::StringBuffer<4> ufcOptionDisplay4Buffer(FA_18C_hornet_UFC_OPTION_DISPLAY_4, onUfcOptionDisplay4Change);


void onUfcOptionDisplay5Change(char* newValu6e) {
  TCA9548A(4);
 

  display5.setTextSize(2);
//display5.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
   display5.setTextColor(WHITE, BLACK);
 display5.setCursor(30, 6);
display5.print (newValu6e);
display5.display();

}
DcsBios::StringBuffer<4> ufcOptionDisplay5Buffer(FA_18C_hornet_UFC_OPTION_DISPLAY_5, onUfcOptionDisplay5Change);

void setup() {
  Serial.begin (11500);
   Wire.begin();

   TCA9548A(0);
   display1.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display1.clearDisplay();



TCA9548A(1);
display2.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display2.clearDisplay();



  TCA9548A(2);
  display3.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display3.clearDisplay();


 TCA9548A(3);
 display4.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display4.clearDisplay();
 


  TCA9548A(4);
  display5.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display5.clearDisplay();



 
DcsBios::setup();
}


void loop(){

  DcsBios::loop();
  }


it's not hardware. This sketch works fine except that cueing changes clear the options.
If i ommit cueing changes everything works but i can't see what cueing option is selected

That's outside my territory.
Wait a little for a helper used the enviroment You run.

thanks for trying to help

What You can do is installing temporary serial.printl printing strategic variables.
Maybe You over stress some library. Debugging code by dry reading code, not having the hardware, is difficult and very time consuming.

Nano clone. So you have 2K of RAM to work with.

5 instances of Adafruit_SSD1306. Each of which will try to allocate 1K of RAM at runtime when displayX.begin(); is executed. And your code doesn't check to see if displayX.begin(); succeeds or not. (Hint: the first one may or may not, but after the first one, none of them will.)