Arduino Uno R4 DHT22 Simulator: Wiring, Code and a Live Circuit
This is a DHT22 temperature and humidity sensor wired to an Arduino Uno R4 Minima, running live: the board reads the sensor every two seconds and prints both numbers on the serial monitor. Open it in the editor and drag the sensor’s sliders: the next reading follows them.
The Arduino Uno R4 is a 5 V board, so the sensor runs from its 5V pin and DATA goes to pin 2. Below are the wiring, the sketch, and the mistakes people make with this pair.
- 5 V logic
- Library: DHT.h (Adafruit)
- Data on pin 2
- Powered from 5V
Wiring the DHT22 to the Arduino Uno R4
Every connection in the circuit above, with the board’s own pin names.
| DHT22 pin | Arduino Uno R4 pin | Why |
|---|---|---|
| VCC (+) | 5V | The DHT22 is happy anywhere from 3.3 to 6 V; 5 V gives the longest cable run. |
| DATA (out) | pin 2 | One wire, both directions: the board pulls it low to ask, the sensor answers with 40 bits. |
| GND (-) | GND | A shared ground, or the board cannot read the sensor’s levels. |
On a 5 V board the whole circuit is 5 V: the module’s own pull-up holds DATA at 5 V between readings and the Renesas RA4M1 (Cortex-M4) reads that directly. A bare four-pin DHT22 has no pull-up, so add a 10 k resistor from DATA to 5V.
Pins to leave alone on the Arduino Uno R4
An Uno sketch’s source builds for the R4, but a compiled Uno program will not run on it: different chip. The R4’s pins source less current than an Uno’s, about 8 mA each, so do not drive a relay coil or a motor straight from one.
On a real bench
The Uno R4 Minima fits every Uno shield and every Uno breadboard layout, so wire the DHT22 exactly as you would on an Uno R3. It has a USB-C socket, and its VIN accepts up to 24 V where an Uno R3 stops at 12.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
#include "DHT.h"
#define DHT_PIN 2 // DATA, pin 2
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(9600);
dht.begin();
Serial.println("DHT22 on the Arduino Uno R4");
}
void loop() {
delay(2000); // the DHT22 measures every 2 s
float celsius = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(celsius) || isnan(humidity)) { // a failed read is NAN
Serial.println("No reading from the DHT22");
return;
}
Serial.print("Temperature: ");
Serial.print(celsius, 1);
Serial.print(" C Humidity: ");
Serial.print(humidity, 1);
Serial.println(" %");
}
DHT dht(DHT_PIN, DHT_TYPE) names the pin and the sensor type, and dht.begin() releases the line so the module’s pull-up holds it high. The Adafruit DHT library’s calls compile here as written, on the Arduino Uno R4 as on every board in the simulator.
readTemperature() and readHumidity() each return a float, or NAN when the sensor did not answer. Both come from the same 40-bit frame, so a failed read is NAN in both, and the sketch checks isnan() on both before printing anything. A failed reading is normal on a real bench now and then, and the first one after power-up always fails.
The two-second delay is the sensor’s own pace: a DHT22 starts a new measurement at most every two seconds and hands back the last one in between. Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 is on pins 0 and 1, which on the R4 are free for other serial devices.
Common mistakes with the DHT22 on the Arduino Uno R4
Reading faster than every two seconds
A DHT22 needs two seconds between measurements. Ask sooner and it returns the previous reading, or nothing at all, so a fast loop looks like a stuck sensor. Keep the delay(2000), or time the reads with millis().
Leaving out the pull-up on a bare sensor
The blue three-pin module has a pull-up built in. The bare four-pin DHT22 does not, and without a 10 k resistor from DATA to 5V every reading fails. In the editor, set the sensor’s pullup property to false to see exactly that.
Expecting an Uno’s pin current
The R4’s pins are 5 V like an Uno’s, but each one sources only about 8 mA where an Uno’s gives 20. A signal line to the DHT22’s DATA line is fine; an LED or a coil hung straight off the pin is not.
Questions and answers
Can I simulate a DHT22 with an Arduino Uno R4 Minima online?
Yes. The circuit at the top of this page is an Arduino Uno R4 Minima with a DHT22, running in your browser on a simulated Renesas RA4M1 (Cortex-M4). The sensor’s sliders set the temperature and humidity it reports, so you can test a thermostat or an alarm threshold without heating anything. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Which Arduino Uno R4 pin should the DHT22 DATA wire use?
Any digital pin that can be both an output and an input. This circuit uses pin 2. Avoid pins 0 and 1, which carry the USB serial port.
How do I use a DHT11 instead?
Change DHT22 to DHT11 in the #define and wire it the same way. The DHT11 reads whole degrees and whole percent, from 0 to 50 C, and it can be asked once a second. In the editor, set the sensor’s model property to dht11.
Why does the serial monitor say there is no reading?
Either the sketch asked within the sensor’s first second after power-up, the DATA wire is on a different pin from the one in the #define, or the pull-up is missing. On an Arduino Uno R4 Minima check that DHT_PIN is 2.
Build your own DHT22 project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.