// Include Wire Library for I2C
#include <Wire.h>
// Include Adafruit Graphics & OLED libraries
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Include Adafruit AM2320 Temp Humid Library
#include <Adafruit_AM2320.h>
// Reset pin not used but needed for library
#define OLED_RESET 4
// Create an object for each OLED display
Adafruit_SSD1306 display1(OLED_RESET);
Adafruit_SSD1306 display2(OLED_RESET);
// Define object am2320
Adafruit_AM2320 am2320 = Adafruit_AM2320();
void TCA9548A(uint8_t bus)
{
Wire.beginTransmission(0x70); // TCA9548A address is 0x70
Wire.write(1 << bus); // send byte to select bus
Wire.endTransmission();
}
void displayTemp(float tem){
// Clear the display
display1.clearDisplay();
//Set the color - always use white despite actual display color
display1.setTextColor(WHITE);
//Set the font size
display1.setTextSize(2);
//Set the cursor coordinates
display1.setCursor(0,10);
display1.print("T: ");
display1.print(tem);
display1.print(" C");
}
void displayHumid(float hum){
// Clear the display
display2.clearDisplay();
//Set the color - always use white despite actual display color
display2.setTextColor(WHITE);
//Set the font size
display2.setTextSize(2);
//Set the cursor coordinates
display2.setCursor(0,10);
display2.print("H: ");
display2.print(hum);
display2.print(" %");
}
void setup() {
// Start Wire library for I2C
Wire.begin();
// Initialize Temp & Humid Sensor
am2320.begin();
// Set multiplexer to channel 1 and initialize OLED-0 with I2C addr 0x3C
TCA9548A(1);
display1.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// initialize OLED-1 with I2C addr 0x3C
TCA9548A(2);
display2.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void loop() {
// Delay to allow sensor to stabalize
delay(2000);
// Read temperature as Celsius
float t = am2320.readTemperature();
// Read Humidity
float h = am2320.readHumidity();
// Set multiplexer to channel 1 and display temperature
TCA9548A(1);
displayTemp(t);
display1.display();
// Set multiplexer to channel 2 and display temperature
TCA9548A(2);
displayHumid(h);
display2.display();
}