Need help with TCA9548A to use SCL2 and SDA2

Hi folks I am trying to connect multiple i2c displays towards arduino mega 2560

I have got following sketch below:

then I wrote following program to scan for devices within the multiplexor:


#include "Wire.h"

#define TCAADDR 0x70

void tcaselect(uint8_t i) {
  if (i > 7) return;

  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
}


// standard Arduino setup()
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("\nTCA9548A Scanner ready!");

  for (uint8_t t = 0; t < 8; t++) {
    tcaselect(t);
    Serial.print("TCA Port #"); Serial.println(t);

    for (uint8_t addr = 0; addr <= 127; addr++) {
      
      // Don't report on the TCA9548A itself!
      if (addr == TCAADDR) continue;

      // See whether a device is on this address
      Wire.beginTransmission(addr);

      // See if something acknowledged the transmission
      int response = Wire.endTransmission();
      if (response == 0) {
        Serial.print("Found I2C 0x");  Serial.println(addr, HEX);
      }
    }

    // Slow the loop scanner down a bit
    delay(1000);
  }
  Serial.println("\nScan completed.");
}

void loop()
{
}

the output is:

TCA9548A Scanner ready!
TCA Port #0
TCA Port #1
TCA Port #2
Found I2C 0x27
TCA Port #3
TCA Port #4
TCA Port #5
TCA Port #6
TCA Port #7

its correct that is my display.
(I will connect more displays once I know this one is working)

now I need help with the code part for the multiplexor.

How can I display "Hello World" on this display?

my atempt is:

#include "Wire.h" // For I2C
#include "LCD.h" // For LCD
#include "LiquidCrystal_I2C.h" // Added library*
//Set the pins on the I2C chip used for LCD connections
//ADDR,EN,R/W,RS,D4,D5,D6,D7


LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
void setup()  
{
  lcd.begin(20,4); 
  lcd.setCursor(3,0);
  lcd.print("Hello World!");
  lcd.setCursor(8,1);
  lcd.print("****");
  lcd.setCursor(0,2);
  lcd.print("This is a demo text");
  lcd.setCursor(8,3);
  lcd.print("****");
}
void loop()
{
}

I believe I need to invoke that
SDA2 and SCL2 somehow but how?
I believe I need to do something similar like


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

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(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
  TCA9548A(2);
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  } 
  // Clear the buffer
  display.clearDisplay();

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

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

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

  // Write to OLED on bus number 2
  TCA9548A(2);
  display.clearDisplay();
  display.setTextSize(8);
  display.setTextColor(WHITE);
  display.setCursor(45, 10);
  // Display static text
  display.println("1");
  display.display(); 

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

I have been looking little bit on this tutorial TCA9548A I2C Multiplexer: ESP32, ESP8266, Arduino | Random Nerd Tutorials

but you people who knows. any suggestions?

Thank you

You may have to instantiate a dedicated lcd() for each multiplexed bus.

Looks like you are mixing the invocation of an I2C and a non-I2C display here?

The I2C object is usually instantiated with something like:

LiquidCrystal_I2C lcd(I2Caddr, cols, rows);

For example a 1602 display would be started with:

LiquidCrystal_I2C lcd(0x27, 16, 2);

No, there is only one I2C bus on the Mega 2560 and that is being used to connect the multiplexer. The switching is handled by the multiplexer itself. In your example it is done with the

void TCA9548A(uint8_t bus)

function using the call:

TCA9548A(n);

where n is the I2C channel on the multiplexer. It looks like once the channel is switched you then write to or manipulate the currently switched in display. You then call the function again to switch channel and work with another display.

Thank you very much!

#include "Wire.h"                    // For I2C
#include "LCD.h"                     // For LCD
#include "LiquidCrystal_I2C.h"       // Added library*
#define TCAADDR 0x70                 // TCA9548A Encoder address
LiquidCrystal_I2C lcd(0x27, 20, 4);  // Set the LCD I2C address
void setup() {
  screenBig();
}
void loop() {
}

void tcaselect(uint8_t i) {
  if (i > 7) return;
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
}

void screenBig() {

  tcaselect(2);
  lcd.begin(20, 4);
  lcd.setCursor(3, 0);
  lcd.print("Hello World!");
  lcd.setCursor(8, 1);
  lcd.print("****");
  lcd.setCursor(0, 2);
  lcd.print("This is a demo text");
  lcd.setCursor(8, 3);
  lcd.print("****");
}

any better suggestions this works fine!

How could I invoke if else statement to verify the (2) is working or is that needed?

Use the procedure of the I2C scanner. Do a beginTransmission(addr) and check endTransmission() for any errors.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.