I am trying to read out an analog digital converter (ADS1115; all three channels) via a multiplexer (TCA9548A) and then display the values, after a conversion to a pressure, on one of three OLED (SSD1306) displays. All components work by themselves. The ADC (ADS1115) was tested separately and so were the displays.
The multiplexer also works. When I run a "scanner" sketch, it recognizes the four devices on the multiplexer.
My code also works to show text on the displays. But as soon as I add the part of the code where the ADS is read, the displays just show me white dots. I assume that I am making a fairly simple mistake. Probably the Arduino does not "remember" the read out value while I change the I2C bus and then address the display.
I am new to programming. So if you have any helpful tips, I would be very grateful!
//I2C Communication
#include <Wire.h>
//Display
#include <SPI.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
//Analog Digital Converter
#include <Adafruit_ADS1X15.h>
Adafruit_ADS1115 ads;
//Display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//Voltage reading Channel 1
float truevoltage1;
float pressure_mbar1;
float truepressure1;
//Voltage reading Channel 2
float truevoltage2;
float pressure_mbar2;
float truepressure2;
//Voltage reading Channel 3
float truevoltage3;
float pressure_mbar3;
float truepressure3;
// 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(9600);
// Start I2C communication with the Multiplexer
Wire.begin();
//Initialise ADS1115
TCA9548A(2);
ads.setGain(GAIN_TWOTHIRDS);
// Init OLED display1 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 display2 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 display3 on bus number 5
TCA9548A(5);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Clear the buffer
display.clearDisplay();
}
void loop() {
int16_t adc0, adc1, adc2, adc3;
float volts0, volts1, volts2, volts3;
//Read Voltage Sensor 1 (Penning Gauge IKR_251)
TCA9548A(2);
adc1 = ads.readADC_SingleEnded(1);
volts1 = ads.computeVolts(adc1);
truevoltage1=volts1*2;
pressure_mbar1=pow(10,(truevoltage1-10.5));
//Write Pressure 1 to Display 1
TCA9548A(3);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(45, 50);
display.println("mbar");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(45, 10);
display.println(pressure_mbar1);
display.display();
//Read Voltage Sensor 2 (Pirani Gauge PSG-500-S)
adc2 = ads.readADC_SingleEnded(2);
volts2 = ads.computeVolts(adc2);
truevoltage2=volts2*2;
pressure_mbar2=pow(10,((truevoltage2-6.143)/1.286));
//Write Pressure 2 to Display 2
TCA9548A(4);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(45, 50);
display.println("mbar");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(45, 10);
display.println(pressure_mbar2);
display.display();
//Read Voltage Sensor 3 (Pirani Gauge PCR260)
adc3 = ads.readADC_SingleEnded(3);
volts3 = ads.computeVolts(adc3);
truevoltage3=volts3*2;
pressure_mbar3=pow(10,(truevoltage3-5.5));
//Write Pressure 2 to Display 2
TCA9548A(5);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(45, 50);
display.println("mbar");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(45, 10);
display.println(pressure_mbar3);
display.display();
delay(500)
}