The DHT22 (also known as AM2302) is a popular digital sensor used to measure temperature and humidity in various DIY electronics projects. It's widely used with microcontrollers like Arduino, Raspberry Pi, and other embedded systems for sensing environmental conditions. The DHT22 is a more accurate and stable sensor compared to its sibling, the DHT11, which is why it is preferred for projects requiring higher precision.
Here’s the typical pin configuration for the DHT22 sensor (in a 4-pin module package):
PinNameDescription1VCCPower supply (3.3V to 6V)2DataData output (connected to the microcontroller)3NCNot Connected (can be left unconnected)4GNDGround (0V)
The DHT22 sensor uses a capacitive humidity sensor and a thermistor to measure the relative humidity and temperature, respectively. It uses a single-wire digital interface for communication with the microcontroller, which makes it simple to interface with devices like the Arduino and Raspberry Pi.
When you read from the DHT22, it outputs a 40-bit data stream, which includes:
The data is sent as a serial stream, and the microcontroller must read it bit by bit, assemble the data into usable values, and then calculate the humidity and temperature.
To connect the DHT22 sensor to an Arduino, follow these simple steps:
Here’s the basic connection diagram:
DHT22 Pin -> Arduino Pin ------------------------------ VCC -> 5V GND -> GND DATA -> Pin 2 (or any other available digital pin)
To read the temperature and humidity from the DHT22 sensor, you need the DHT sensor library for Arduino, which simplifies the process of communication with the sensor.
#include <DHT.h> // Define the pin where the DHT22 data line is connected #define DHTPIN 2 // Define the type of sensor (DHT22) #define DHTTYPE DHT22 // Initialize the DHT sensor DHT dht(DHTPIN, DHTTYPE); void setup() { // Start the serial communication Serial.begin(9600); // Initialize the DHT sensor dht.begin(); } void loop() { // Wait a few seconds between readings delay(2000); // Read temperature as Celsius float temperature = dht.readTemperature(); // Read humidity float humidity = dht.readHumidity(); // Check if readings failed, if so, display error message if (isnan(temperature) || isnan(humidity)) { Serial.println("Failed to read from DHT sensor!"); return; } // Print temperature and humidity values to the serial monitor Serial.print("Temperature: "); Serial.print(temperature); Serial.print(" °C"); Serial.print(" Humidity: "); Serial.print(humidity); Serial.println(" %"); }
DHT dht(DHTPIN, DHTTYPE)
: Initializes the DHT22 sensor on the defined pin and type.dht.readTemperature()
: Reads the temperature in Celsius.dht.readHumidity()
: Reads the humidity level as a percentage.If you're using a Raspberry Pi, you can interface with the DHT22 sensor using the GPIO pins. You'll need the Adafruit Python DHT library to read the sensor data.
To install the necessary libraries on Raspberry Pi:
sudo apt-get update sudo apt-get install build-essential python-dev python-openssl sudo pip3 install Adafruit-DHT
Once installed, use the following Python code to read the sensor:
import Adafruit_DHT # Set sensor type and pin sensor = Adafruit_DHT.DHT22 pin = 4 # GPIO pin (e.g., GPIO4) # Read temperature and humidity humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: print('Temp: {0:0.1f} C Humidity: {1:0.1f}%'.format(temperature, humidity)) else: print('Failed to get reading. Try again!')
This will print the temperature and humidity to the console. Make sure the DHT22 is connected to the GPIO pin you specify in the code.
Brand:- Generic
Origin:- China