Notifications
Clear all

I2C Multiplexer Arduino Code

1 Posts
1 Users
0 Likes
1,007 Views
Berner
(@berner)
Member
Joined: 5 years ago
Posts: 31
Topic starter  

I am trying to make a gadget that has a pair of CCS811 air quality sensors, and they have fixed I2C addresses of 0x5A. I have no issue making them work using the code in the Adafruit example library. Here is that code:

#include "Adafruit_CCS811.h"

Adafruit_CCS811 ccs;

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

Serial.println("CCS811 test");

if(!ccs.begin()){
Serial.println("Failed to start sensor! Please check your wiring.");
while(1);
}

// Wait for the sensor to be ready
while(!ccs.available());
}

void loop() {
if(ccs.available()){
if(!ccs.readData()){
Serial.print("CO2: ");
Serial.print(ccs.geteCO2());
Serial.print("ppm, TVOC: ");
Serial.println(ccs.getTVOC());
}
else{
Serial.println("ERROR!");
while(1);
}
}
delay(500);
}

To add the second sensor I need to use a TCA9548A multiplexer, and DWBS has a very nice YT video on this device. I am struggling with the code to make my sensors work with this device, and hoping someone with coding skills can spot my issue. I am confident my wiring is correct, and scanning the multiplexer does show my CCS811 on the TCA9548A. For simplicity and troubleshooting, I am just working with one sensor on the multiplexer, once I get data flowing, I will add in the second.

#include <Wire.h>
#include <SPI.h>
#include "Adafruit_CCS811.h"

/*Creating an object for each one of the sensors*/
Adafruit_CCS811 ccs;

#define TCAADDR 0x70

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

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

void setup() {
Serial.begin(9600);
Serial.println("CCS811 VOC Data");

tcaselect(3);
delay(5000);
if(!ccs.begin()){
Serial.println("Failed to start sensor! Please check your wiring.");
while(1);
}

// Wait for the sensor to be ready
while(!ccs.available());
}

void loop(void) {

tcaselect(3);
if(ccs.available()){
if(!ccs.readData()){
Serial.print("CO2: ");
Serial.print(ccs.geteCO2());
Serial.print("ppm, TVOC: ");
Serial.println(ccs.getTVOC());
}
else{
Serial.println("ERROR!");
while(1);
}
}

delay(1000);
}

   
Quote