Connecting two AHT21B sensors to I2C and serial printing it

Hi all, I am working on a project for which I want to connect various sensors to my arduino through serial communication and also I2C.

I wrote a little program (I am very new to programming) and it worked perfect for AHT21B humidity and temperature sensor connected to the SCL and SDA on my arduino, and a DHT22 humidity and temperature sensor connected to digital pins.

Now I recently bought a PCA9548A multiplexer to function as an I2C bus so that I can add another AHT21B sensor (they both have the same adress) and 2 BMP280 airpressure sensors that also both have the same adress.

I started with trying to connect both of the AHT21B sensors on channel 6 and 7. But I think something is going wrong. I do not get an error message, but while normally it would print all the data in CSV in my serial monitor (I then downloaded PLX-DAQ), right now it doesnt print anything in the serial monitor at all..

Can someone help me? I am very new so I think, something is going wrong with defining the sensors, or the multiplexer etc. I don't fully understand everything I wrote yet. (fyi, I know some code is grey, those are just ideas I had/ what I saw in tutorials but what I didnt use yet)

#include <LiquidCrystal.h>   //LCD screen library
#include <DHT.h>             //Temperature and Humidity sensor library
#include <Adafruit_AHTX0.h>  //Adafruit library for the AHT21 sensor
#include <Wire.h>            //Library to work with Inter-Integrated Circuit (I2C) Protocol

// initialize the library by associating any needed LCD interface pin with the arduino pin number it is connected to
LiquidCrystal lcd(12, 13, 8, 9, 10, 11);  // parameters (rs, e, d4,d5,d6,d7)

// defining the pin for the DHT sensor
#define datapinDHT22 7  // Digital  pin the DHT is connected to
#define DHTTYPE DHT22
DHT dht22(datapinDHT22, DHTTYPE);  // Initialise DHT sensor as object

// defining objects for the AHT temp and humidity sensors
Adafruit_AHTX0 aht1;
Adafruit_AHTX0 aht2;

//#define Multiplier 0x00 //TCA9548A module I2C adress 0x00
// TCA9548A I2C channel 2 contains AHT21B_V1.1 0x38
// TCA9548A I2C channel 3 contains AHT21B_V1.1 0x38
//#define addr1 0x38 // AHT21B_V1.1 device 1
//#define addr2 0x38 // AHT21B_V1.1 device 2

// make custom temperature character for LCD
byte customChar[] = {
  B01110,
  B01010,
  B01110,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000;  // 1000 milliseconds is 1 second. 60000 is 1 minute
float tempD;
float humidityD;

void setup() {
  Wire.begin();  // start the Wire library for I2C
  PCA9548A(7);   // call for channel 7
  aht1.begin();  // initialise temp and humid sensor 1

  PCA9548A(6);   // call for channel 6
  aht2.begin();  // initialise temp and humid sensor 2

  dht22.begin();                  // setup the DHT to start measuring
  lcd.begin(16, 2);               // set up the LCD's number of columns and rows:
  lcd.createChar(0, customChar);  //Create the custom character defined above.
  //Wire.begin();          // Enable I2C communication as master
  startMillis = millis();  // Capture the start time

  Serial.println("CLEARDATA");  // to clear the data before we send new data to excel
  Serial.println("LABEL,Time, Minutes, temperature.a, humidity.a, temperature.d, humidity.d");
}

void PCA9548A(uint8_t bus) {     // forms a switch for channels
  Wire.beginTransmission(0x70);  // adress of PCA9548A multiplexer;
  Wire.write(1 << bus);          // send byte to the selected bus
  Wire.endTransmission();
}

void loop() {
  currentMillis = millis();  //get the current "time" (Number of milliseconds since the program started)
  int minutes = int(currentMillis / 60000);

  sensors_event_t humidityA, tempA;   //measure temp and humidity with AHT21B 1
  aht1.getEvent(&humidityA, &tempA);  //populate temp and humidity objects with fresh data

  sensors_event_t humidityA2, tempA2;  // measure temp and humidty with AHT21B 2
  aht2.getEvent(&humidityA2, &tempA2);

  tempD = dht22.readTemperature();   // measure temp with DHT
  humidityD = dht22.readHumidity();  // measure humidity with DHT

  // print data on LCD screen
  lcd.setCursor(0, 0);                                           //setting for first lcd row
  lcd.print(String("T.d:") + String(tempD) + String(" C"));      // Print temperature data to the LCD.
  lcd.write(byte(0));                                            // write special character behind C
  lcd.setCursor(0, 1);                                           // setting second lcd row
  lcd.print(String("RH.a:") + String(humidityD) + String("%"));  // Print humidity data to the LCD

  if (currentMillis - startMillis >= period)  // test if period has elapsed
  {                                           //Serial.p  println(String("time, temperature.a, humidity.a"));
    Serial.print("DATA,TIME,");
    Serial.print(minutes);  // TIME
    Serial.print(",");
    Serial.print(tempA.temperature);  // temperature A form aht1
    Serial.print(",");
    Serial.print(humidityA.relative_humidity);  // humidity A from aht1
    Serial.print(",");
    Serial.print(tempA2.temperature);  // temperature A2 from aht2
    Serial.print(",");
    Serial.print(humidityA2.relative_humidity);  // humidity A2 from aht2
    Serial.print(tempD);                         // temperature d
    Serial.print(",");
    Serial.print(humidityD);  // humidity d
    Serial.println();
    startMillis = currentMillis;  //IMPORTANT to save the start time
  }
}

1 Like

Update,

I managed to print it. Noticed I forgot Serial.begin(9600);

However this is what I have now:

   #include <LiquidCrystal.h>   //LCD screen library
  #include <DHT.h>             //Temperature and Humidity sensor library
  #include <Adafruit_AHTX0.h>  //Adafruit library for the AHT21 sensor
  #include <Wire.h>            //Library to work with Inter-Integrated Circuit (I2C) Protocol

  // initialize the library by associating any needed LCD interface pin with the arduino pin number it is connected to
  LiquidCrystal lcd(12, 13, 8, 9, 10, 11);  // parameters (rs, e, d4,d5,d6,d7)

  // defining the pin for the DHT sensor
  #define datapinDHT22 7  // Digital  pin the DHT is connected to
  #define DHTTYPE DHT22
  DHT dht22(datapinDHT22, DHTTYPE);  // Initialise DHT sensor as object

  // defining objects for the AHT temp and humidity sensors
  Adafruit_AHTX0 aht1;
  Adafruit_AHTX0 aht2;

  //#define Multiplier 0x00 //TCA9548A module I2C adress 0x00
  // TCA9548A I2C channel 2 contains AHT21B_V1.1 0x38
  // TCA9548A I2C channel 3 contains AHT21B_V1.1 0x38
  //#define addr1 0x38 // AHT21B_V1.1 device 1
  //#define addr2 0x38 // AHT21B_V1.1 device 2

  // make custom temperature character for LCD
  byte customChar[] = {
    B01110,
    B01010,
    B01110,
    B00000,
    B00000,
    B00000,
    B00000,
    B00000
  };

  unsigned long startMillis;
  unsigned long currentMillis;
  const unsigned long period = 1000;  // 1000 milliseconds is 1 second. 60000 is 1 minute
  float tempD;
  float humidityD;

    void PCA9548A(uint8_t bus) {     // forms a switch for channels
    Wire.beginTransmission(0x70);  // adress of PCA9548A multiplexer;
    Wire.write(1 << bus);          // send byte to the selected bus
    Wire.endTransmission();
  }

  void setup() {
   Serial.begin(9600);
    Wire.begin();  // start the Wire library for I2C
    PCA9548A(7);   // call for channel 7
    aht1.begin();  // initialise temp and humid sensor 1

    PCA9548A(6);   // call for channel 6
    aht2.begin();  // initialise temp and humid sensor 2

    dht22.begin();                  // setup the DHT to start measuring
    lcd.begin(16, 2);               // set up the LCD's number of columns and rows:
    lcd.createChar(0, customChar);  //Create the custom character defined above.
    //Wire.begin();          // Enable I2C communication as master
    startMillis = millis();  // Capture the start time

    Serial.println("CLEARDATA");  // to clear the data before we send new data to excel
    Serial.println("LABEL,Time, Minutes, temperature.a, humidity.a, temperature.a2, humidity.a2, temperature.d, humidity.d");
  }



  void loop() {
    currentMillis = millis();  //get the current "time" (Number of milliseconds since the program started)
    int minutes = int(currentMillis / 60000);

    sensors_event_t humidityA, tempA;   //measure temp and humidity with AHT21B 1
    aht1.getEvent(&humidityA, &tempA);  //populate temp and humidity objects with fresh data

    sensors_event_t humidityA2, tempA2;  // measure temp and humidty with AHT21B 2
    aht2.getEvent(&humidityA2, &tempA2);

    tempD = dht22.readTemperature();   // measure temp with DHT
    humidityD = dht22.readHumidity();  // measure humidity with DHT

    // print data on LCD screen
    lcd.setCursor(0, 0);                                           //setting for first lcd row
    lcd.print(String("T.d:") + String(tempD) + String(" C"));      // Print temperature data to the LCD.
    lcd.write(byte(0));                                            // write special character behind C
    lcd.setCursor(0, 1);                                           // setting second lcd row
    lcd.print(String("RH.a:") + String(humidityD) + String("%"));  // Print humidity data to the LCD

    if (currentMillis - startMillis >= period)  // test if period has elapsed
    {                                           //Serial.p  println(String("time, temperature.a, humidity.a"));
      Serial.print("DATA,TIME,");
      Serial.print(minutes);  // TIME
      Serial.print(",");
      Wire.print(tempA.temperature);  // temperature A form aht1
      Serial.print(",");
      Wire.print(humidityA.relative_humidity);  // humidity A from aht1
      Serial.print(",");
      Wire.print(tempA2.temperature);  // temperature A2 from aht2
      Serial.print(",");
      Wire.print(humidityA2.relative_humidity);  // humidity A2 from aht2
      Serial.print(",");
      Serial.print(tempD);                         // temperature d
      Serial.print(",");
      Serial.print(humidityD);  // humidity d
      Serial.println();
      startMillis = currentMillis;  //IMPORTANT to save the start time
    }
    delay(1000); // Add a delay of 1 second (adjust as needed)
  }

And this is the data it outputs:

I definetly do something wrong with the Wire library... Because it doesnt give anything. not even zero now!

You need to switch to the correct PCA channel before you communicate with the AHTs like you did in setup()

Ah okay, so in the loop I have to refer to the channel when I use get.event?
How would I do this? Also with

PCA9548A(6);
sensors_event_t humidityA2, tempA2;  // measure temp and humidty with AHT21B 2
  aht2.getEvent(&humidityA2, &tempA2);

?

Yes just like that but put a small delay after PCA9548A(n) maybe delay (1);

1 Like

Thanks it worked!

Final code looks like this:

#include <LiquidCrystal.h>   //LCD screen library
#include <DHT.h>             //Temperature and Humidity sensor library
#include <Adafruit_AHTX0.h>  //Adafruit library for the AHT21 sensor
#include <Wire.h>            //Library to work with Inter-Integrated Circuit (I2C) Protocol

// initialize the library by associating any needed LCD interface pin with the arduino pin number it is connected to
LiquidCrystal lcd(12, 13, 8, 9, 10, 11);  // parameters (rs, e, d4,d5,d6,d7)

// defining the pin for the DHT sensor
#define datapinDHT22 7  // Digital  pin the DHT is connected to
#define DHTTYPE DHT22
DHT dht22(datapinDHT22, DHTTYPE);  // Initialise DHT sensor as object

// defining objects for the AHT temp and humidity sensors
Adafruit_AHTX0 aht1;
Adafruit_AHTX0 aht2;

//#define Multiplier 0x00 //TCA9548A module I2C adress 0x00
// TCA9548A I2C channel 2 contains AHT21B_V1.1 0x38
// TCA9548A I2C channel 3 contains AHT21B_V1.1 0x38
//#define addr1 0x38 // AHT21B_V1.1 device 1
//#define addr2 0x38 // AHT21B_V1.1 device 2

// make custom temperature character for LCD
byte customChar[] = {
  B01110,
  B01010,
  B01110,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000;  // 1000 milliseconds is 1 second. 60000 is 1 minute
float tempD;
float humidityD;

void PCA9548A(uint8_t bus) {     // forms a switch for channels
  Wire.beginTransmission(0x70);  // adress of PCA9548A multiplexer;
  Wire.write(1 << bus);          // send byte to the selected bus
  Wire.endTransmission();
}

void setup() {
  Serial.begin(9600);
  Wire.begin();  // Enable I2C communication as master
  PCA9548A(7);   // call for channel 7
  aht1.begin();  // initialise temp and humid sensor 1

  PCA9548A(6);   // call for channel 6
  aht2.begin();  // initialise temp and humid sensor 2

  dht22.begin();                  // setup the DHT to start measuring
  lcd.begin(16, 2);               // set up the LCD's number of columns and rows:
  lcd.createChar(0, customChar);  //Create the custom character defined above.

  startMillis = millis();  // Capture the start time

  Serial.println("CLEARDATA");  // to clear the data before we send new data to excel
  Serial.println("LABEL,Time, Minutes, temperature.a, humidity.a, temperature.a2, humidity.a2, temperature.d, humidity.d");
}

void loop() {
  currentMillis = millis();  //get the current "time" (Number of milliseconds since the program started)
  int minutes = int(currentMillis / 60000);

  PCA9548A(7);
  delay(1);
  sensors_event_t humidityA, tempA;   //measure temp and humidity with AHT21B 1
  aht1.getEvent(&humidityA, &tempA);  //populate temp and humidity objects with fresh data

  PCA9548A(6);
  delay(1);
  sensors_event_t humidityA2, tempA2;  // measure temp and humidty with AHT21B 2
  aht2.getEvent(&humidityA2, &tempA2);

  tempD = dht22.readTemperature();   // measure temp with DHT
  humidityD = dht22.readHumidity();  // measure humidity with DHT

  // print data on LCD screen
  lcd.setCursor(0, 0);                                       //setting for first lcd row
  lcd.print(String("T.d:") + String(tempD) + String(" C"));  // Print temperature data to the LCD.
  //lcd.print(String("T.A:") + tempA.temperature + String(" C"));      // Print temperature data to the LCD.
  lcd.write(byte(0));                                            // write special character behind C
  lcd.setCursor(0, 1);                                           // setting second lcd row
  lcd.print(String("RH.a:") + String(humidityD) + String("%"));  // Print humidity data to the LCD

  if (currentMillis - startMillis >= period)  // test if period has elapsed
  {                                           //Serial.p  println(String("time, temperature.a, humidity.a"));
    Serial.print("DATA,TIME,");
    Serial.print(minutes);  // TIME
    Serial.print(",");
    Serial.print(tempA.temperature);  // temperature A form aht1
    Serial.print(",");
    Serial.print(humidityA.relative_humidity);  // humidity A from aht1
    Serial.print(",");
    Serial.print(tempA2.temperature);  // temperature A2 from aht2
    Serial.print(",");
    Serial.print(humidityA2.relative_humidity);  // humidity A2 from aht2
    Serial.print(",");
    Serial.print(tempD);  // temperature d
    Serial.print(",");
    Serial.print(humidityD);  // humidity d
    Serial.println();
    startMillis = currentMillis;  //IMPORTANT to save the start time
  }
  delay(1000);  // Add a delay of 1 second (adjust as needed)
}

and the data it returns:

very happy with it.

1 Like

Glad it worked.
Have a nice day!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.