DHT22 AM2302 Temperature Sensor and Humidity Sensor for Arduino Microcontroller

DHT22 AM2302 temperature and humidity sensor

You will find the DHT22 behind the 15th door of the AZ advent calendar and that three times. With this sensor it is possible to measure the humidity and temperature of the ambient air. It has four contacts that are used to supply power from 3.3V to 6V and for data transmission. The pin assignment looks like this if you look at the sensor with the labeled side of the grid:


The pin assignment is as follows:

Pin code

function

1

VDD (voltage +)

2

Data signal

3

not used

4

GND (voltage -)


You may already know the DHT11 sensor, which looks similar. However, this provides a lower accuracy (no floating point numbers), as well as smaller measurement value ranges.

Please refer to the data sheet for further details . A description of how to connect the sensor to a Uno micro controller can be found here or here (the links are also on the Shop page available).

The AZ advent calendar already contained the Uno and Nano micro controllers, as well as the LCD display with I²C backpack. I want to show you how you can read three DHT sensors together and show them on the display.

You may have several terrariums or want to display the values ​​from several rooms centrally. Take another look at the Advent contributions to doors three, four and five for information on using the components.

Note: the maximum length of the cable between sensor and micro controller is not specified in the data sheets. It should be possible to use cables longer than 15 m without any problems. Information on the 1-Wire data bus can be found e.g. here.

The sensors, the display and the micro controller are connected as follows:


Pull-up resistors are required for the connections to the data pins in order to obtain a clear voltage level. The library, the installation of which I will show below, turns on the internal pull-up resistors. The Adafruit DHT library works this way too. If you are using a different library that does not use the internal pull-ups, you should use external resistors. As a rule, 4.7 kOhms are used, or 10 kOhms as shown here.


According to the data sheet, a 100 nF capacitor is recommended between VDD and GND on the sensors, which were not used here. I deliberately chose the breadboard in this Fritzing circuit diagram in order to point out once again that the outer (bus) strips may be interrupted in the middle. To do this, have a look again in the Advent contribution to door number one.

In order to be able to use the display via the I²C interface, I use the library LiquidCrystal_I2C which you can install via the library management. This is an extended variant of the library LiquidCrystal:


You can find various libraries for the DHT22 by entering DHT in the search. I chose DHTNEW because it can automatically differentiate between the sensor variants.

 

Information on the library can be found here.

After the installation some sample sketches are available again. I recommend the HelloWorld-Sketch for the I²C display and the dhtnew_minimum-Sketch for the DHT sensor. I used both as a basis. The examples for the display are not very extensive. Since the library is very similar to the "normal" LiquidCrystal library, you can have a look at the examples there. Except for the initialization, everything should work the same way.

Here is the source code that I created to show the humidity (in %) and temperature (in ° C) on the two-line display. I created the symbol for ° C online with the LCD Custom Char Generator. So I could use ° C as one sign and saved space on the display. In addition, I can show you once again how you can use your own characters.

Here is the source code:

#include
#include
#include
 
#if defined (ARDUINO) && ARDUINO> = 100
#define printByte (args) write (args);
#else
#define printByte (args) print (args, BYTE);
#endif
 
DHTNEW mySensor_A (6);
DHTNEW mySensor_B (7);
DHTNEW mySensor_C (8);
const int maxSensors = 3;
unsigned long changeIntervall = 2000;
unsigned long changeTime = 0;
String line_1;
String line_2;
 
LiquidCrystal_I2C lcd (0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
 
// https://maxpromer.github.io/LCD-Character-Creator/
// Definition of own characters for the display
byte degree [] = {
B11000,
B11011,
B00100,
B00100,
B00100,
B00100,
B00100,
B00011
};
 
void setup ()
{
Serial.begin (115200);
lcd.init ();
lcd.backlight ();
lcd.createChar (0, degree);
}
 
void loop ()
{
// non-blocking interval
if (millis () - changeTime> changeIntervall) {
 
// Read out sensor values
mySensor_A.read ();
mySensor_B.read ();
mySensor_C.read ();
 
// Output serial monitor
Serial.println ("\ t 1 \ t 2 \ t 3");
Serial.print ("HUM in%: \ t");
Serial.print (mySensor_A.getHumidity (), 1);
Serial.print ("\ t");
Serial.print (mySensor_B.getHumidity (), 1);
Serial.print ("\ t");
Serial.println (mySensor_C.getHumidity (), 1);
Serial.print ("TEM in ° C: \ t");
Serial.print (mySensor_A.getTemperature (), 1);
Serial.print ("\ t");
Serial.print (mySensor_B.getTemperature (), 1);
Serial.print ("\ t");
Serial.println (mySensor_C.getTemperature (), 1);
Serial.print ("\ n");
 
 
// output LCD
// create formatted lines:
line_1 = ("%") + String (mySensor_A.getHumidity (), 1) + ("")
+ String (mySensor_B.getHumidity (), 1) + ("")
+ String (mySensor_C.getHumidity (), 1);
line_2 = ("") + String (mySensor_A.getTemperature (), 1) + ("")
+ String (mySensor_B.getTemperature (), 1) + ("")
+ String (mySensor_C.getTemperature (), 1);
 
// output on the display:
lcd.setCursor (0,0);
lcd.print (line_1);
lcd.setCursor (0,1);
// Custom character:
lcd.printByte (0);
lcd.setCursor (1,1);
lcd.print (line_2);
    
changeTime = millis ();
  }
}

The three sensors are read out and displayed in the serial monitor and on the LCD display. This happens "without interruption" in 2-second intervals. This means that you can insert additional program code that can run in such a way that it does not have to wait for the sensors to be read out.

Note: The DHT sensors should be queried in a cycle of no less than two seconds.

You can also find the source code here as a download


We wish you a Merry Advent season.

Andreas Wolter

for AZ-Delivery Blog

Specials

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN