0

Arduino and TCA9548 driving two OLED's
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Arduino and TCA9548 driving two OLED's

by rizla420 on Wed Sep 28, 2016 1:08 pm

I'm trying to drive two OLED's using the TCA9548 i2c Mux. I have everythign hooked up correctly. I ran the i2c MUX scanner. The address of the MUX is 0x70 and the OLED's have the same address of 0x3C. I was able to successfully get the 2nd display at address '1' on the mux to show, but for some reason my 1st display is completely dark. I verified it works by putting it on the same pins as the working one and they both work and display the same info. I'm kind of lost. Its my first time using the OLEDs, the Mux, as well as Wire. SO its ALL new and somewhat overwhelming, but i've managed to fumble along.

Here's my code
Code: Select all | TOGGLE FULL SIZE

#include <Adafruit_NeoPixel.h>
#include "WS2812_Definitions.h"
#include "U8glib.h"
#include "Wire.h"

#define rgbPIN 4
#define LED_COUNT 6

//Analog Mux control digital pins
int s0 = 5;
int s1 = 6;
int s2 = 7;
int s3 = 8;

//TCA9548A i2c MUX for OLED Displays
#define dispMUX         0x70  //Multiplexer Address
#define DISPLAY     0x3C  //OLED Display Address, they all have the same address

//74HC4067 Analog MUX "SIG" pin analog input pin
int SIG_pin = 18;

//SET OLED
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);  // I2C / TWI

void drawOn(void) {
  // graphic commands to redraw the complete screen should be placed here 
  u8g.setFont(u8g_font_helvB14);
  u8g.drawStr( 0, 14, "LINE 1");
  u8g.drawStr( 0, 34, "LINE 2");
  u8g.drawStr( 0, 54, "LINE 3");

}
void drawOff(void) {
  // graphic commands to redraw the complete screen should be placed here 
  u8g.setFont(u8g_font_helvB14);
  u8g.drawStr( 0, 14, "LINE 1");
  u8g.drawStr( 0, 34, "LINE 2");
  u8g.drawStr( 0, 54, "LINE 3");

}

void draw(char str[]) {
  // Example
u8g.setFont(u8g_font_helvB14);
 u8g.drawStr( 5, 40, str);
}


// Create an instance of the Adafruit_NeoPixel class called "leds".
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, rgbPIN, NEO_GRB + NEO_KHZ800);

void setup()
{
/* ------------------ START SERIAL COMM --------------------- */
  Serial.begin(115200);
 //   while (!Serial)
 //     {
 //       ; // wait for serial port to connect. Needed for Micro
  //    }

/* ------------------ START I2C --------------------- */
  Wire.begin();
  mux(0);  //select Display on channel 0
  initializeDISP(DISPLAY);
  mux(1);  //select Display on channel 1
  initializeDISP(DISPLAY);
  mux(0xFF);  //disable all channels. This is not required

/* ------------------ START LEDS --------------------- */
  leds.begin();  // Call this to start up the LED strip.



/* ----------------- START PIN SETUP ----------------- */

  //Address PINS for Analog READ MUX PINS
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
 
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
 
  //RGB LED Control PIN
  pinMode(rgbPIN, OUTPUT);

/* ================ END PIN SETUP ================ */

/* ---------------- START OLED U8GLIB SETUP ---------------- */
    //OLED U8GLIB SETUP OPTIONS

  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }   

/* ================ END OLED U8GLIB SETUP ============= */
} //END OF MAIN SETUP FUNCTION

/*  FUNCTION DEFINITIONS for sketch */

void mux(byte channel)
{
 byte controlRegister = 0x04; 
 controlRegister |= channel;
 Wire.beginTransmission(dispMUX);
 if (channel == 0xFF){Wire.write(0x00);} //deselect all channels
 else {Wire.write(controlRegister);}     //set to selected channel
 Wire.endTransmission();
}

void initializeDISP(uint8_t address)
{
 Serial.println("Initiating Quick Start");
 Wire.beginTransmission(DISPLAY);
 Wire.write(0x06);
 Wire.write(0x40);
 Wire.write(0x00);
 Wire.endTransmission(); 
 u8g.begin();
}

void sendDISP(int _disp, char rootStr[])
{
 /* Not sure what these lines are for... */
 Wire.beginTransmission(_disp);
 Wire.write(0x04);
 Wire.endTransmission();
   
   // picture loop

   u8g.firstPage(); 
   do {
     draw(rootStr);
   } while( u8g.nextPage() );
}

//ANALOG MUX READ FUNCTION
int readMux(int channel){
  int controlPin[] = {s0, s1, s2, s3};

  int muxChannel[16][4]={
    {0,0,0,0}, //channel 0
    {1,0,0,0}, //channel 1
    {0,1,0,0}, //channel 2
    {1,1,0,0}, //channel 3
    {0,0,1,0}, //channel 4
    {1,0,1,0}, //channel 5
    {0,1,1,0}, //channel 6
    {1,1,1,0}, //channel 7
    {0,0,0,1}, //channel 8
    {1,0,0,1}, //channel 9
    {0,1,0,1}, //channel 10
    {1,1,0,1}, //channel 11
    {0,0,1,1}, //channel 12
    {1,0,1,1}, //channel 13
    {0,1,1,1}, //channel 14
    {1,1,1,1}  //channel 15
  };

  //loop through the 4 sig
  for(int i = 0; i < 4; i ++){
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }

  //read the value at the SIG pin
  int val = analogRead(SIG_pin);

  //return the value
  return val;
}


/* ------------------- START MAIN LOOP ------------------- */
/* ####################################################### */
/* ------------------------------------------------------- */
void loop()
{
    // read the input on analog pin 0:
//  int tcrt1 = analogRead(A0);
//  int tcrt2 = analogRead(A1);

int tcrt1=readMux(0);//first sensor
int tcrt2=readMux(1);// second sensor
int thresholdVal=200;


//PRINT CURRENT VALUE OF TCRT SENSORS
Serial.println(tcrt1);
Serial.println("\n");
Serial.println(tcrt2);

     
     


//LEDs are assigned in a clockwise direction the PCB

if (tcrt1 > thresholdVal){
Serial.println("SENSOR1 Active!");
  Serial.println("\n");
leds.setPixelColor(0,255,0,0);
leds.setPixelColor(1,255,0,0);
leds.setPixelColor(2,255,0,0);

   
      mux(0);
     sendDISP(DISPLAY, "Sen1: ACT");


}else{
leds.setPixelColor(0,0,255,0);
leds.setPixelColor(1,0,255,0);
leds.setPixelColor(2,0,255,0);

      mux(0);
     sendDISP(DISPLAY, "Sen1: NOT ACT");


}

if (tcrt2 > thresholdVal ){
  Serial.println("SENSOR2 Active!");
  Serial.println("\n");
leds.setPixelColor(3,255,0,0);
leds.setPixelColor(4,255,0,0);
leds.setPixelColor(5,255,0,0);

      mux(1);
     sendDISP(DISPLAY, "Sen2: ACT");
     

}else{
leds.setPixelColor(3,0,255,0);
leds.setPixelColor(4,0,255,0);
leds.setPixelColor(5,0,255,0);

      mux(1);
     sendDISP(DISPLAY, "Sen2: NOT ACT");
     

}


//SHOW LEDS
leds.show();



}

/* ------------------- END MAIN LOOP ------------------- */



I had another post with similar code, but for a different issue. I couldnt figure out how to delete it.

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Arduino and TCA9548 driving two OLED's

by rizla420 on Wed Sep 28, 2016 1:19 pm

Hmm... I made a motification to the initializeDSP function. Something about the hex value looked like it was possibly fat fingered so i changed it to:

void initializeDISP(uint8_t address)
{
Serial.println("Initiating Quick Start");
Wire.beginTransmission(DISPLAY);
Wire.write(0x06);
//Wire.write(0x40); //THIS IS WHAT I COMMENTED OUT
Wire.write(0x04); //ADDED THIS LINE BELOW
Wire.write(0x00);
Wire.endTransmission();
u8g.begin();
}


I guess I dont really understand what those values the Wire.write is doing..

So, now i have the 1st display working, but its showing the same output as the 2nd, but its not updating when i play with the sensor. The second one toggles accordingly... hmm.. i'll figure this out eventually.

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Arduino and TCA9548 driving two OLED's

by adafruit_support_bill on Wed Sep 28, 2016 1:20 pm

You have calls to set the mux in front of some, but not all of your calls to the u8g library functions.

adafruit_support_bill
 
Posts: 67362
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino and TCA9548 driving two OLED's

by rizla420 on Wed Sep 28, 2016 1:25 pm

adafruit_support_bill wrote:You have calls to set the mux in front of some, but not all of your calls to the u8g library functions.


Im not sure what you mean. I thought I did intialize the mux's here:

Wire.begin();
mux(0); //select Display on channel 0
initializeDISP(DISPLAY);
mux(1); //select Display on channel 1
initializeDISP(DISPLAY);
mux(0xFF); //disable all channels. This is not required

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Re: Arduino and TCA9548 driving two OLED's

by adafruit_support_bill on Wed Sep 28, 2016 1:41 pm

But what mux channel is active here?

Code: Select all | TOGGLE FULL SIZE
/* ================ END PIN SETUP ================ */

/* ---------------- START OLED U8GLIB SETUP ---------------- */
    //OLED U8GLIB SETUP OPTIONS

  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }   

/* ================ END OLED U8GLIB SETUP ============= */
} //END OF MAIN SETUP FUNCTION

adafruit_support_bill
 
Posts: 67362
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino and TCA9548 driving two OLED's

by rizla420 on Wed Sep 28, 2016 1:43 pm

adafruit_support_bill wrote:But what mux channel is active here?

Code: Select all | TOGGLE FULL SIZE
/* ================ END PIN SETUP ================ */

/* ---------------- START OLED U8GLIB SETUP ---------------- */
    //OLED U8GLIB SETUP OPTIONS

  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }   

/* ================ END OLED U8GLIB SETUP ============= */
} //END OF MAIN SETUP FUNCTION



ooooh, good question.. I guess its whatever the last one that was active?? hmm.. need to play around with this..

Edit: After looking at the original forum post i was basing mine off of. They dont initialize it either for each muxed interface. I *think* that just sets the parameters for u8glib, then when you call the begin() function for each mux thats what initilizes it or so i think... clearly incorrectly.

rizla420
 
Posts: 332
Joined: Thu Feb 05, 2009 9:03 pm

Please be positive and constructive with your questions and comments.