The DHT22 is a digital temperature and relative humidity sensor used with microcontrollers. It communicates over a single-wire data interface and is commonly wired with VCC, GND, and a DATA line.
| Pin | Signal | Description |
|---|---|---|
GND |
ground | Ground reference for the DHT22 module power and data signal. |
VCC |
power | Positive supply input for the DHT22 module, typically 3.3V to 5V. |
DATA |
data | Single-wire digital data output from the DHT22 temperature and humidity sensor. |
| From | To | Wire |
|---|---|---|
board:5V |
x1:VCC |
red |
board:GND |
x1:GND |
black |
board:D2 |
x1:DATA |
green |
This example was written and compile-checked for the arduino-uno. Codey Online adapts it to any other supported board for you.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
dht.begin();
Serial.println("DHT22 sensor example");
}
void loop() {
delay(2000); // DHT22 needs a slow sampling rate
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature();
if (isnan(humidity) || isnan(temperatureC)) {
Serial.println("Failed to read from DHT22 sensor");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\tTemperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
}
Describe what you want to build. Codey Online writes the code, draws the wiring diagram, compiles it and flashes your board straight from the browser.
Start for free