SHT20 sensors and multiplexer

I'm trying to use three SHT20 sensors to control my environmental chamber with an Arduino mega. Asking a question in this forum recently, I noticed multiplexer is the part to be added to my board. So all sensors are connected as instructed here:

I need the sensors to monitor T&RH and then the Arduino to control the components for adjusting them in the specified range. Now my question is whether this job would be possible or not with SHT20 and muxer? As muxer can connect one sensor at a time to Arduino, how I can manage the code to have efficient monitoring of these parameters?

In addition, I could not find the right code to define SHT20 and multiplexer together following the examples on the web. As soon as I add the block for assigning muxer channels to sensors the uploaded code becomes almost dead and seems unable to pass the setup() function. The code below in void setup() is an example of this problem (I have defined the other libraries and command lines before this block).

   for (uint8_t t=0; t<8; t++) {
      tcaselect(t);
      Serial.print("TCA Port #"); Serial.println(t);
 
      for (uint8_t addr = 0; addr<=127; addr++) {
        if (addr == TCAADDR) continue;
      
        uint8_t data;
        if (! twi_writeTo(addr, &data, 0, 1, 1)) {
           Serial.print("Found I2C 0x");  Serial.println(addr,HEX);
        }
      }
    }
    Serial.println("\ndone");

Thanks.

Now my question is whether this job would be possible or not with SHT20 and muxer?

Probably yes. Based on the information you provided yet I don't see a problem.

As muxer can connect one sensor at a time to Arduino, how I can manage the code to have efficient monitoring of these parameters?

Temperature and humidity don't change that fast, so requesting the data from each sensor one after the other is no problem.

The code below in void setup() is an example of this problem (I have defined the other libraries and command lines before this block).

Post complete code.

So all sensors are connected as instructed here:
TCA9548A I2C Multiplexer : ID 2717 : $6.95 : Adafruit Industries, Unique & fun DIY electronics and kits

That's the shop page. There are no instructions about how to wire SHT20 sensors. Post the wiring diagram of your setup!

I need a reading of T&RH by 3 SHT20 sensors and use the output parameters in the loop(), but it seems the problem is when I assign the tcaselect() command.

The code I tried is below (there are also some functions for lcd menu and set clock that which should be used as is so did not upload them here):

#include <Wire.h> 
#include <LiquidCrystal.h>
extern "C" { 
#include "utility/twi.h"  // from Wire library, so we can do bus scanning
}
#include "DFRobot_SHT20.h"
DFRobot_SHT20  sht20;


#define TCAADDR 0x70
void tcaselect (uint8_t i) {
if (i > 7) return;

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

//Variables and setpoints
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
#define HiTEMP 24.85
#define LoTEMP 24.2
#define Compen 0.2  // the temperature defference which is supposed to be Compensated by heating pad 
#define HumSETPOINT 50 // These values are based on trial and error for acheiving the best humidity conditions
#define HumRESOLUTION 0.5

// A 1602 SainSmart LCD Keypad module has been used in this setup
LiquidCrystal lcd (8, 9, 4, 5, 6, 7);   // Initialize the library with the numbers of the interface pins

// Units SETUP *************************************************
int fan = 1;  // The pin number that fan is connected to
int HumFan = 13;
int HumFanSpeed = 22;
int SolHum = 2;
int SolMain = 3;
int SolEx =11;
int WB = 53;
int Hpad = 52;
#define HUMIDIFIER 12  //setting humidifier to pin 12
#define ON false      //turns ON the Relay Board Contact
#define OFF true      //turn OFF the Relay Board Contact
#define SensorHum  40 //For the water level switch in the humidifier
#define SensorMain  42
#define SensorEx  44

//*********************** LCD Initials ************************
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

// read the buttons
int read_LCD_buttons()
{
 adc_key_in = analogRead(0);      // read the value from the sensor 
 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
 // For V1.1 us this threshold
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 250)  return btnUP; 
 if (adc_key_in < 450)  return btnDOWN; 
 if (adc_key_in < 650)  return btnLEFT; 
 if (adc_key_in < 850)  return btnSELECT;  
 return btnNONE;  // when all others fail, return this...
}

#define MILLIS_OVERFLOW 34359738

// To control solenoid valve a time control is required
// **************** Clock Variables **********************
unsigned long currentMillis, previousMillis, elapsedMillis;
int seconds, minutes, hours;
boolean start= true;
boolean a = false;
int tcaI = 0;

void setup() {

Wire.begin(); //Initialize I2C Harware
Serial.begin(115200);

//*************INITIALIZING SENSORs*****
tcaselect(4);
sht20.initSHT20();
Serial.println("SHT20 Blue!");
sht20.checkSHT20();
tcaselect(6);
sht20.initSHT20();
Serial.println("SHT20 Green!");
sht20.checkSHT20();
tcaselect(7);
sht20.initSHT20();
Serial.println("SHT20 Yellow!");
sht20.checkSHT20();
//**********************************
  

  lcd.begin(16, 2);
  lcd.clear();
  pinMode(fan, OUTPUT);
  pinMode(HUMIDIFIER, OUTPUT);
  digitalWrite(HUMIDIFIER, OFF);
  pinMode(HumFan, OUTPUT);
  digitalWrite(HumFan, OFF);
  pinMode(SolMain, OUTPUT);
  pinMode(SolHum, OUTPUT);
  pinMode(SolEx, OUTPUT);
  pinMode(WB, OUTPUT);
  digitalWrite(WB, OFF);
  pinMode(Hpad, OUTPUT);
  digitalWrite(Hpad, OFF);
  pinMode (SensorHum, INPUT_PULLUP); //For the water level switch in the humidifier
  pinMode (SensorMain, INPUT_PULLUP); //For the water level switch in the sink
  pinMode (SensorEx, INPUT_PULLUP); //For the water level switch in the external waterbath

//FAN Initialization *************************************************
  analogWrite(fan, 255); // Fan needs to work very slowly and this is just to initialize the fan 
  delay(500);
  analogWrite(fan, 35);  



}

void loop() {

  digitalWrite(SolMain, OFF); // After reseting the setup each time Select Bottun should be pressed to reset the time for Solenoid Valve Control
  digitalWrite(SolHum, OFF);
  digitalWrite(SolEx, OFF);
  
  if (lcd_key == btnSELECT || start == false ) {
    setClock();
    start = false; 
  }


  lcd.setCursor(0, 1); // set the cursor to column 0, line 1

  //READING Humidity and Temperature***********************
  float Temp = 0;
  float HUM = 0;
  for (int i=1;i<5;i++){
      if(i=2){  //skiping sht10 for now
            i++; 
          }
      else
   {
   tcaselect(i+3);
   Temp =+ sht20.readTemperature();
   HUM =+ sht20.readHumidity();
   delay(200);
    }
  }
  delay(100);
float hum = HUM/3;
float temp = temp/3;  
 

Serial.println(hum);
// LCD Display and Menu 
lcd_key = read_LCD_buttons();  // read the buttons
 
switch (lcd_key)               // depending on which button was pushed, we perform an action
 {
   case btnRIGHT:
     {
     mainMenuDraw();
     drawCursor();
     lcd.setCursor(0, 0);
     lcd.print("Elapsed Time:");
     lcd.setCursor(7,1);
     lcd.print(hours);
     lcd.print(":");
     lcd.print(minutes);
     lcd.print(":");
     lcd.print(seconds);

     break;
     }
   case btnLEFT:
     {
     mainMenuDraw();
     drawCursor();
     lcd.setCursor(0, 0);
     lcd.print("Fan Speed:");
     lcd.setCursor(7,1);
     break;
     }
   case btnSELECT:
     {
     mainMenuDraw();
     drawCursor();
     if (lcd_key == btnSELECT && start == false) {
     lcd.setCursor(0,0);
     lcd.print("TEST Running !");
     }
     else {
     lcd.setCursor(0,0);
     lcd.print("TEST STARTED!");
     delay(2500);
     a = true;
     }
     break;
     }
   case btnUP:
     {
     mainMenuDraw();
     drawCursor();
     if (a == true){
     lcd.setCursor(0, 0);
     lcd.print("Next Sampling:");
     lcd.setCursor(7,1);

     lcd.print(59 - minutes);
     lcd.print(":");
     lcd.print(59 - seconds);
     }
     else{
     lcd.setCursor(0, 0);
     lcd.print("TEST Has Not ");
     lcd.setCursor(0, 1);
     lcd.print("Been Started Yet!");
     }
     break;
     }
   case btnNONE:
     {
       mainMenuDraw();
       drawCursor();
       lcd.setCursor(0,0);
       lcd.print("Temp:");
       lcd.setCursor(0,1);
       lcd.print("Humidity:");
       lcd.setCursor(10,0);
       lcd.print(temp,1);
       lcd.setCursor(14,0);
       lcd.print("C");
       lcd.setCursor(10,1);
       lcd.print(hum,1);
       lcd.setCursor(14,1);
       lcd.print("%"); 
       if (a == true){
        lcd.setCursor(15,1);
        lcd.print((char)126);

       }
     break;
     }
 } 
 
  Serial.print(hours);
  Serial.print(":");
  Serial.print(minutes);
  Serial.print(":");
  Serial.print(seconds);
  Serial.print(",");
  Serial.print(hum);
  Serial.print(",");
  Serial.println(temp);

}

As this seems to be an impossible task to use multiple SHT20s with muxer for this application (because nobody has left a reply), it's highly appreciated if someone can tell me if using SHT20 with regular pins of Arduino could be an option..

Thank you

I need a reading of T&RH by 3 SHT20 sensors and use the output parameters in the loop(), but it seems the problem is when I assign the tcaselect() command.

Still not seen the wiring diagram.

Remove the

sht20.initSHT20();

calls, the just call Wire.begin().

What output does that sketch make?

int fan = 1;  // The pin number that fan is connected to

Serial.begin(115200);


  pinMode(fan, OUTPUT);


  analogWrite(fan, 255); // Fan needs to work very slowly and this is just to initialize the fan
  delay(500);
  analogWrite(fan, 35);

You cannot use pin 1 for the serial interface and for controlling the fan!

Still not seen the wiring diagram.

I attached a sketch from the wiring.

What output does that sketch make?

That is to assign a pin number for each component

You cannot use pin 1 for the serial interface and for controlling the fan!

Does this mean Arduino is using pin 1 for serial interface by default and it's not available for other tasks?
I will change that..

What output does that sketch make?

These fans are working so fast, I needed to lower their working speeds while they're on, but at the beginning, they needed a kick to be able to run in low current. This is the reason for that block..

Below is the updated setup and loop blocks according to your comments

void setup() {
 
  lcd.begin(16, 2);
  lcd.clear();
 
  Wire.begin(); //Initialize I2C Harware
  while (!Serial);
  Serial.begin(115200);

//*************INITIALIZING SENSORs*******************************
tcaselect(5);
Serial.println("SHT20 Blue!");
sht20.checkSHT20();
tcaselect(6);
Serial.println("SHT20 Green!");
sht20.checkSHT20();
tcaselect(7);
Serial.println("SHT20 Yellow!");
sht20.checkSHT20();
//**********************************************************************

  pinMode(fan, OUTPUT);
  pinMode(HUMIDIFIER, OUTPUT);
  digitalWrite(HUMIDIFIER, OFF);
  pinMode(HumFan, OUTPUT);
  digitalWrite(HumFan, OFF);
  pinMode(SolMain, OUTPUT);
  pinMode(SolHum, OUTPUT);
  pinMode(SolEx, OUTPUT);
  pinMode(WB, OUTPUT);
  digitalWrite(WB, OFF);
  pinMode(Hpad, OUTPUT);
  digitalWrite(Hpad, OFF);
  pinMode (SensorHum, INPUT_PULLUP); //For the water level switch in the humidifier
  pinMode (SensorMain, INPUT_PULLUP); //For the water level switch in the sink
  pinMode (SensorEx, INPUT_PULLUP); //For the water level switch in the external waterbath

//FAN Initialization *************************************************
  analogWrite(fan, 255); // Fan needs to work very slowly and this is just to initialize the fan 
  delay(500);
  analogWrite(fan, 35);  


}

void loop() {
  lcd.print("Hello..");
  digitalWrite(SolMain, OFF); // After reseting the setup each time Select Bottun should be pressed to reset the time for Solenoid Valve Control
  digitalWrite(SolHum, OFF);
  digitalWrite(SolEx, OFF);
  
  if (lcd_key == btnSELECT || start == false ) {
    setClock();
    start = false; 
  }


  lcd.setCursor(0, 1); // set the cursor to column 0, line 1

 //READING Humidity and Temperature***********************
  float Temp = 0;
  float HUM = 0;
  for (int i=1;i<5;i++){
      if(i=2){  //skiping sht10 for now
            i++; 
          }
      else
   {
   tcaselect(i+3);
   Temp =+ sht20.readTemperature();
   HUM =+ sht20.readHumidity();
   delay(200);
    }
  }
  delay(100);
float hum = HUM/3;
float temp = temp/3;

Circuit and Wiring.pdf (1.65 MB)

That is to assign a pin number for each component

No, the output of the serial interface. You write several information to the serial interface. Please post what you get there.

Does this mean Arduino is using pin 1 for serial interface by default and it's not available for other tasks?

Yes. In your wiring diagram it's not used.

In the wiring diagram you have VCC twice on the Mega2560. Which pin is that? 5V or Vin?

Yes. In your wiring diagram it's not used.

The code for fan control was working fine before this modification. So I believe there should not be any issue with this part of the code if I just find a free digital pin on Arduino and use it for the main fan. So please disregard this part (main fan) in the wiring sketch..

In the wiring diagram you have VCC twice on the Mega2560. Which pin is that? 5V or Vin?

I meant 5V.

So do you think the parts for muxer in the wiring and the code are fine?

So do you think the parts for muxer in the wiring and the code are fine?

Given your "muxer" is a TCA9548A and you wired all address pin explicitly to GND, yes. That expects the RESET pin to be pulled-up on the TCA9548A.

Check the return value of the Wire.endTransmission() call. It should be 0. If you get another value your wiring needs an additional thorough check.