USING TCA9548A I2C MULTIPLEXER

TCA9548A RTC LCD MLX

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/TCA9548A.zip

#define TCAADDR 0x70 //=============
#include <Wire.h>
#include “RTClib.h”

#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

RTC_DS3231 rtc; //Make a RTC DS3231 object

#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin,BACKLIGHT_PIN,POSITIVE);
LCD *myLCD = &lcd;
float ta;
float to;
//Set the names of days
char daysOfTheWeek[7][12] = {“MINGGU”, “SENIN “, “SELASA”, “RABU “, “KAMIS “, “JUMAT “, “SABTU “};
int SENSOR_PIN = 0;
void setup() {
Wire.begin();
Serial.begin(9600);
Serial.println();

scan_i2c();
i2ctca_scanner();
/*
//I2C pins available for other I2C devices
tcaselect(0);
tcaselect(3);
tcaselect(4);
tcaselect(5);
tcaselect(6);
*/
tcaselect(1);
mlx.begin();

tcaselect(7);
lcd.begin(16,2); // initialize the lcd
lcd.home ();

tcaselect(2);
if (! rtc.begin()) {
Serial.println(“Couldn’t find RTC”);
while (1);
}
//Setup of time if RTC lost power or time is not set
if (rtc.lostPower()) {
//Sets the code compilation time to RTC DS3231
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}

void loop() {
int sensorValue = analogRead(SENSOR_PIN);

tcaselect(1);
ta=mlx.readAmbientTempC();
to=mlx.readObjectTempC();
Serial.print(“Ambient = “);
Serial.print(” “);
Serial.print(ta);
Serial.print(” “);
Serial.print(“*C”);
Serial.print(” “);
Serial.print(“Object = “);
Serial.print(” “);
Serial.print(to);
Serial.print(” “);
Serial.print(“*C”);
Serial.print(” “);

tcaselect(2);
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print(‘/’);
Serial.print(now.month(), DEC);
Serial.print(‘/’);
Serial.print(now.day(), DEC);
Serial.print(” (“);
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(“) “);
Serial.print(now.hour(), DEC);
Serial.print(‘:’);
Serial.print(now.minute(), DEC);
Serial.print(‘:’);
Serial.println(now.second(), DEC);

tcaselect(7);
if(now.hour()<10){
lcd.setCursor(0, 0);
lcd.print(“0”);
lcd.setCursor(1, 0);
lcd.print(now.hour());
}
if(now.hour()>=10){
lcd.setCursor(0, 0);
lcd.print(now.hour());
}
lcd.setCursor(2, 0);
lcd.print(“:”);
if(now.minute()<10){
lcd.setCursor(3, 0);
lcd.print(“0”);
lcd.setCursor(4, 0);
lcd.print(now.minute());
}
if(now.minute()>=10){
lcd.setCursor(3, 0);
lcd.print(now.minute());
}

lcd.setCursor(5, 0);
lcd.print(“:”);
if(now.second()<10){
lcd.setCursor(6, 0);
lcd.print(“0”);
lcd.setCursor(7, 0);
lcd.print(now.second());
}
if(now.second()>=10){
lcd.setCursor(6, 0);
lcd.print(now.second());
}
lcd.setCursor(9, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);

lcd.setCursor(0, 1);
lcd.print(“tA/tO=”);
lcd.setCursor(6, 1);
lcd.print(ta,1);
lcd.setCursor(11, 1);
lcd.print(“/”);
lcd.setCursor(12, 1);
lcd.print(to,1);
delay (1000);
}

void scan_i2c() {
byte error, address;
int nDevices;
Serial.println(F(“\nSimple I2C Scanner”));

nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0){
Serial.print(F(“I2C device: 0x”));
if (address<16) Serial.print(“0”);
Serial.print(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print(F(“I2C error: 0x”));
if (address<16) Serial.print(“0”);
Serial.println(address,HEX);
}
delay(5); //Scan not too fast
}
if (nDevices == 0)
Serial.println(F(“No I2C devices found\n”));
else
Serial.println(F(“done\n”));
}
void i2ctca_scanner() {
Serial.println(F(“I2C Mux Scanner”));

for (uint8_t t=0; t<8; t++) {
tcaselect(t);
Serial.print(F(“TCA Port #”));
Serial.println(t);
for (uint8_t addr = 0; addr<=127; addr++) {
if (addr == TCAADDR) continue;
Wire.beginTransmission(addr);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print(F(“Found I2C 0x”));
Serial.print(addr,HEX);
Serial.println(” “);
Serial.println(“”);
}
}
}
Serial.println(F(“done”));
}

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

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