# Connecting two AHT21B sensors to I2C and serial printing it

**URL:** <https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379>\
**Category:** Programming\
**Created:** [April 26, 2024, 10:15am UTC](https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379 "2024-04-26T10:15:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiesdesign](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@tiesdesign](https://forum.arduino.cc/u/tiesdesign)\
**Post date:** [April 26, 2024, 10:15am UTC](https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379/1 "2024-04-26T10:15:41Z")

</div>

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)

```arduino
#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
  }
}

```

---

<div class="post-metadata">

**Author:** ![tiesdesign](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@tiesdesign](https://forum.arduino.cc/u/tiesdesign)\
**Post date:** [April 26, 2024, 10:30am UTC](https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379/2 "2024-04-26T10:30:12Z")

</div>

Update,

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

However this is what I have now:

```arduino
   #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:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/4/6/e/46edee8bc762c4f681d4ff93f0694f31ffe5f7a0.png)

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

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [April 26, 2024, 11:48am UTC](https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379/3 "2024-04-26T11:48:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tiesdesign](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@tiesdesign](https://forum.arduino.cc/u/tiesdesign)\
**Post date:** [April 26, 2024, 12:05pm UTC](https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379/4 "2024-04-26T12:05:24Z")

</div>

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

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

```

?

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [April 26, 2024, 12:09pm UTC](https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379/5 "2024-04-26T12:09:26Z")

</div>

> [@tiesdesign](#):
>
> How would I do this? Also with

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

---

<div class="post-metadata">

**Author:** ![tiesdesign](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@tiesdesign](https://forum.arduino.cc/u/tiesdesign)\
**Post date:** [April 26, 2024, 12:39pm UTC](https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379/6 "2024-04-26T12:39:21Z")

</div>

> [@jim-p](#):
>
> maybe

Thanks it worked!

---

<div class="post-metadata">

**Author:** ![tiesdesign](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@tiesdesign](https://forum.arduino.cc/u/tiesdesign)\
**Post date:** [April 26, 2024, 12:40pm UTC](https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379/7 "2024-04-26T12:40:37Z")

</div>

Final code looks like this:

```arduino
#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:

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/2/6/d/26dd60ed05c7dde385e944e96fa70b952d4b7b2f.png)

very happy with it.

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [April 26, 2024, 1:54pm UTC](https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379/8 "2024-04-26T13:54:03Z")

</div>

Glad it worked.  
Have a nice day!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 23, 2024, 1:54pm UTC](https://forum.arduino.cc/t/connecting-two-aht21b-sensors-to-i2c-and-serial-printing-it/1252379/9 "2024-10-23T13:54:11Z")

</div>

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