Arduino UNO R4 - DHT22

This tutorial instructs you how to use Arduino UNO R4 with DHT22 temperature and humidity sensor. In detail, we will learn:

Arduino UNO R4 and DHT22 sensor module

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×DHT22 Temperature Humidity Sensor Module
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4
1×(Recommended) Power Splitter For Arduino UNO R4

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of DHT22 Temperature and Humidity Sensor

DHT22
Temperature Range -40°C to 80°CW
Temperature Accuracy ± 0.5°C
Humidity Range 0% to 100%
Humidity Accuracy ± 2 to 5%
Reading Rate 0.5Hz (once every 2 seconds)
Operating Voltage3 to 5V

Pinout

There are two types of DHT22: the sensor and the module.

DHT22 temperature and humidity sensor Pinout

The DHT22 sensor includes four pins:

  • GND pin: connect this to GND (0V).
  • VCC pin: connect this to VCC (5V or 3.3V).
  • DATA pin: this pin allows the sensor to communicate with the Arduino UNO R4.
  • NC pin: this is not connected and can be ignored.

The DHT22 module includes three pins:

  • GND pin: connect to GND (0V)
  • VCC pin: connect to VCC (5V or 3.3V)
  • DATA pin: used for communication between the sensor and Arduino UNO R4

Some manufacturers offer the DHT22 sensor as a module with three pins labeled GND, VCC, and DATA (or alternatively as -, +, and OUT).

Wiring Diagram

Use a resistor between 5K and 10K Ohms to maintain the high data line and allow communication between the DHT22 sensor and the Arduino UNO R4.

Arduino UNO R4 - DHT22 Sensor Wiring

The wiring diagram between Arduino UNO R4 DHT22 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 - DHT22 Module Wiring

Most DHT22 sensor modules come with an integrated resistor, so there is no need to add one. This saves some effort in wiring or soldering.

The wiring diagram between Arduino UNO R4 DHT22 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

How To Program For DHT22 Temperature Sensor

Programming for both DHT22 sensor and module is the same.

  • Add the library:
#include <DHT.h>
  • Set the Arduino UNO R4 pin that connects to the DHT sensor:
#define DHT22_PIN 2
  • Create a DHT22 sensor object
DHT dht22(DHT22_PIN, DHT22);
  • Set up the sensor:
dht22.begin();
  • Read humidity.
float humi = dht22.readHumidity();
  • Read the temperature in Celsius.
float tempC = dht22.readTemperature();
  • Read the temperature in Fahrenheit:
float tempF = dht22.readTemperature(true);

Arduino UNO R4 Code - DHT22

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-dht22 */ #include <DHT.h> #define DHT22_PIN 2 // The Arduino UNO R4 pin connected to DHT22 DHT dht22(DHT22_PIN, DHT22); void setup() { Serial.begin(9600); dht22.begin(); // initialize the DHT22 sensor } void loop() { // wait a few seconds between measurements. delay(3000); // read humidity float humidity = dht22.readHumidity(); // read temperature as Celsius float tempC = dht22.readTemperature(); // read temperature as Fahrenheit float tempF = dht22.readTemperature(true); // check if any reads failed if (isnan(humidity) || isnan(tempC) || isnan(tempF)) { Serial.println("Failed to read from DHT22 sensor!"); } else { Serial.print("DHT22# Humidity: "); Serial.print(humidity); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(tempC); Serial.print("°C ~ "); Serial.print(tempF); Serial.println("°F"); } }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Click on the Libraries icon on the left side of the Arduino IDE.
  • Type "DHT" in the search box and look for the DHT sensor library by Adafruit.
  • Click the Install button to add the library.
Arduino UNO R4 DHT sensor library
  • You will need to install additional library dependencies.
  • Click the Install All button to install all necessary libraries.
Arduino UNO R4 Adafruit Unified sensor library
  • Choose and copy the code for your sensor, then open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to upload the code to your Arduino UNO R4.
  • Change the temperature around the sensor by making it hotter or colder.
  • Check the results on the Serial Monitor.
COM6
Send
DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 32.00% | Temperature: 28.00°C ~ 82.40°F DHT22# Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F DHT22# Humidity: 32.00% | Temperature: 29.00°C ~ 84.20°F DHT22# Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!