Blog
Write a new blog entry

i2c Multiplexer (PCA9540B)
created: 16 May 2013 16:21
tags:
The PCA9540B is a 1-of-2 bidirectional translating multiplexer, controlled via the I2C-bus. It basically helps you use two I2C driven components that has the same address. It splits the I2C channels to two parts so that you could address each I2C outputs individually.

Here's a very good example of its use. The 128x64 OLED Display from seeedstudio I2C driver contains only one address. Unlike different types of I2C components (i.e. sainsmart, ywrobot lcd), this OLED does not have any other address, the multiplexer comes handy if you are attempting to control both OLEDs individually.

The sketch shown below is using a GOFi2cOLED library which can be downloaded here. It is a very cool library for 128x64 OLEDs since it has the function for the changing font size.

Lesson learned: Do NOT FORGET Wire.begin(); before starting to communicate within the i2c bus. I spent about a day trying to figure out that this function was the only problem and trust me it was a PIA!

#include <Wire.h>
#include <GOFi2cOLED.h>
 
GOFi2cOLED GOFoled;
 
void setup()
{
  Wire.begin(); // never forget this!!
 
  // communicate to the i2c multiplexer (address = 0x70)
  Wire.beginTransmission(0x70);
  Wire.write(0x04); // select channel 0
  Wire.endTransmission();    
 
  // initialise OLED (connected to channel 0)
  GOFoled.init(0x3C);
  GOFoled.clearDisplay();
 
  // communicate to i2c multiplexer (address = 0x70)
  Wire.beginTransmission(0x70);
  Wire.write(0x05); // select channel 1
  Wire.endTransmission();    
 
  // initialise OLED (connected to channel 1)
  GOFoled.init(0x3C);
  GOFoled.clearDisplay();
}
 
void loop()
{
  oledPrint(23, 1);  // print 23 on oled 1
  oledPrint(29, 2); // print 29 on oled 2
 
  delay(500);  
 
  oledPrint(22, 1); // print 22 on oled 1
  oledPrint(27, 2); // print 27 on oled 2
 
  delay(200);
}
 
void oledPrint(int floorNumber, int oledNumber)
{
  if(oledNumber == 1)
    oledNumber = 0x04;
  else if(oledNumber == 2)
    oledNumber = 0x05;
 
  Wire.beginTransmission(0x70);
  Wire.write(oledNumber);
  Wire.endTransmission();
 
  GOFoled.clearDisplay();
  GOFoled.setTextSize(9);
  GOFoled.setTextColor(WHITE);
  GOFoled.setCursor(15,0);
 
  GOFoled.print(String(floorNumber));
 
  GOFoled.display();
}

Output

Reference

http://www.kerrywong.com/2012/10/08/i2c-multiplexer-shield-testing/ : This guy is using a different kind of i2c Multiplexer, another family of i2c multiplexer though using similar approach.
http://www.kerrywong.com/blog/wp-content/uploads/2011/05/TempSensorSch.png : Do not forget the pull-up resistors from the Arduino Board to the multiplexer input. The OLEDs do not need any pull-ups since its soldered onboard.
Comments: 0, Rating: 0