Working with DHT22 and AHT21 humidity and temp. sensors

Hii, all, I am new to this platform. I am working on a little project for which I want to connect 4 temperature sensors, 3 temperature and humidity sensors and 2 pressure sensors and possible also some airflow sensors to my arduino UNO. (I am trying to dry a organic material, and measure its water content based on the air that flows through it)

I am currently testing just the basics, so I am playing around with a DHT22 Temp. and humid. sensor and an LCD display. This works fine. I also tested an AHT sensor and printed the data onto the serial control and this also works fine. However, when I try to combine them I have some trouble. The LCD does not show the DHT data anymore.

I am quite new to coding so it is probably a beginner mistake, but I can't seem to find the solution because I don't get any errors.

Here is my code:

// include libraries
#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(0, 1, 8, 9, 10, 11);
Adafruit_AHTX0 aht;

// defining the pin for the DHT sensor
#define datapin 7  // Digital  pin the DHT is connected to
#define DHTTYPE DHT22
DHT dht(datapin, DHTTYPE);

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

void setup() {
  // setup the DHT to start measuring
  dht.begin();
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.createChar(0, customChar);
  Serial.begin(115200);
  if (!aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
}

void loop() {
  //measure temp and humidity with IC2
  sensors_event_t humidityA, tempA;
  aht.getEvent(&humidityA, &tempA);  //populate temp and humidity objects with fresh data
  Serial.println(String("T.a:") + String(tempA.temperature));
  Serial.println(String("RH.a:") + String(humidityA.relative_humidity));

  // measure temperature and humidity with DHT
  float tempD = dht.readTemperature();
  float humidityD = dht.readHumidity();
  // print the output on LCD screen
  lcd.setCursor(0, 0);  //setting for first lcd row
  // Print a message to the LCD.

  lcd.print(String("T.d:") + String(tempD) + String(" C"));
  lcd.write(byte(0));  // write special character behind C
  // setting second lcd row
  lcd.setCursor(0, 1);
  lcd.print(String("RH.a:") + String(humidityD) + String("%"));

  delay(2000);
}

Welcome to the forum

LiquidCrystal lcd(0, 1, 8, 9, 10, 11);

Does this mean that you are using pins 0 and 1 to drive the LCD ?

If you are using pins 0 and 1 because there are no other pins left,
you have 2 alternatives for the LCD.
Or use an LCD_I2C module,
or use the analog pins (From A0 to A5) pins 14 to 19,
like digital pins.

Yes, I used a tutorial for this.

The LCD is connected to pin 0 and 1 and 8,9,10 and 11. digital
The DHT22 is connected to 7 digital
And the AHT21 is connected to SDA and SCL right now. But my plan is to change this to a I2C module. I ordered it so it's on its way.

There are other pins left! So I will check to which pins I can connect it. I did not know pin 0 and 1 might cause a problem?

mmm weird.
I have changed the pins from the LCD, rs and E from 0 and 1 to 12 and 13 digital pins.

The LCD now shows the data from the DHT22, however now the data from the AHT21 sensor are not printed in the serial monitor anymore..

Any ideas?

Maybe its the wiring maybe?

again here is my code to be sure:


// include libraries
#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)
Adafruit_AHTX0 aht;

// defining the pin for the DHT sensor
#define datapin 7  // Digital  pin the DHT is connected to
#define DHTTYPE DHT22
DHT dht(datapin, DHTTYPE);

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

void setup() {
  // setup the DHT to start measuring
  dht.begin();
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.createChar(0, customChar);

  Serial.begin(115200);
  if (!aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
}

void loop() {
  //measure temp and humidity with IC2
 // sensors_event_t humidityA, tempA;
  //aht.getEvent(&humidityA, &tempA);  //populate temp and humidity objects with fresh data
  //Serial.println(String("T.a:") + String(tempA.temperature));
  //Serial.println(String("RH.a:") + String(humidityA.relative_humidity));

  // measure temperature and humidity with DHT
  float tempD = dht.readTemperature();
  float humidityD = dht.readHumidity();
  // print the output on LCD screen
  lcd.setCursor(0, 0);  //setting for first lcd row
  // Print a message to the LCD.

  lcd.print(String("T.d:") + String(tempD) + String(" C"));
  lcd.write(byte(0));  // write special character behind C
  // setting second lcd row
  lcd.setCursor(0, 1);
  lcd.print(String("RH.a:") + String(humidityD) + String("%"));

  delay(2000);
}

The code to read the sensor is commented out

 //aht.getEvent(&humidityA, &tempA);  //populate temp and humidity objects with fresh data

and you do not have any code to display the data to the LCD

Ah you are right haha..

It works now!

Thanks for all the help.

That's good news

For the benefit of anyone else reading this topic please post your working code

1 Like

Here is the working code:


// include libraries
#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)
Adafruit_AHTX0 aht;

// defining the pin for the DHT sensor
#define datapin 7  // Digital  pin the DHT is connected to
#define DHTTYPE DHT22
DHT dht(datapin, DHTTYPE);

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

void setup() {
  // setup the DHT to start measuring
  dht.begin();
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.createChar(0, customChar);

  Serial.begin(115200);
  if (!aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
}

void loop() {
  //measure temp and humidity with IC2
 sensors_event_t humidityA, tempA;
  aht.getEvent(&humidityA, &tempA);  //populate temp and humidity objects with fresh data
  Serial.println(String("T.a:") + String(tempA.temperature));
  Serial.println(String("RH.a:") + String(humidityA.relative_humidity));

  // measure temperature and humidity with DHT
  float tempD = dht.readTemperature();
  float humidityD = dht.readHumidity();
  // print the output on LCD screen
  lcd.setCursor(0, 0);  //setting for first lcd row
  // Print a message to the LCD.

  lcd.print(String("T.d:") + String(tempD) + String(" C"));
  lcd.write(byte(0));  // write special character behind C
  // setting second lcd row
  lcd.setCursor(0, 1);
  lcd.print(String("RH.a:") + String(humidityD) + String("%"));

  delay(2000);
}

Thank you

As an exercise you could consider only printing the text once in setup() and only printing one or both of the values if they change. It would be good practice for you

1 Like

Thanks that does sound like a good exercise, however I am also planning to create a plot/ graph about the changes in time. So then I would need to collect data also when it does not change but stay constant! So I think I will first watch some IOT and arduino cloud tutorials.

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