Raspberry Pi Pico · DHT22

Raspberry Pi Pico DHT22 Simulator: Wiring, Code and a Live Circuit

This is a DHT22 temperature and humidity sensor wired to a Raspberry Pi Pico, 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 Raspberry Pi Pico is a 3.3 V board, so the sensor is powered from 3V3 and DATA goes to GP15 (physical pin 20). Below are the wiring with the board’s real pin names, the sketch, and the mistakes people make with this pair.

  • 3.3 V logic
  • Library: DHT.h (Adafruit)
  • Data on GP15 (physical pin 20)
  • Powered from 3V3(OUT)
Raspberry Pi Pico · DHT22live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.

Wiring the DHT22 to the Raspberry Pi Pico

Every connection in the circuit above, with the board’s own pin names.

DHT22 pinRaspberry Pi Pico pinWhy
VCC (+)3V3(OUT)The sensor runs from 3.3 V, and so does its pull-up, so DATA never rises above what the pin can take.
DATA (out)GP15 (physical pin 20)One wire, both directions: the board pulls it low to ask, the sensor answers with 40 bits.
GND (-)GNDA shared ground, or the board cannot read the sensor’s levels.

The three-pin module carries a pull-up resistor from DATA to VCC. Powered from VBUS (5 V from USB), that resistor would hold a Raspberry Pi Pico pin at 5 V, which the RP2040 is not rated for. From 3V3 the whole line stays at 3.3 V and the sensor still reads normally.

Pins to leave alone on the Raspberry Pi Pico

The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.

On a real bench

Solder headers on the Pico and press it into the breadboard across the center channel; the DHT22 goes in beside it. GND pins sit every fifth pin down both sides (pins 3, 8, 13, 18 and so on), so there is always one close. The first upload needs BOOTSEL held while plugging in.

The code

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

DHT22 on Raspberry Pi Pico GP15 (physical pin 20)
#include "DHT.h"

#define DHT_PIN 15     // DATA, GP15 (physical pin 20)
#define DHT_TYPE DHT22

DHT dht(DHT_PIN, DHT_TYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
  Serial.println("DHT22 on the Raspberry Pi Pico");
}

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 Raspberry Pi Pico 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(115200) opens the serial monitor. Serial is the USB port at 115200 baud in this sketch; on a real Pico it appears once the board has enumerated.

Common mistakes with the DHT22 on the Raspberry Pi Pico

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().

Powering the module from VBUS (5 V from USB)

It works for a while, which is the trap. The module’s pull-up then holds DATA at 5 V, above the RP2040’s absolute maximum, and the pin can be damaged slowly. Use 3V3, as in the circuit above.

Counting physical pins instead of GP numbers

The sketch says 15, which means GP15, and that is physical pin 20 on the header. Counting 15 pins down the side lands on a different GPIO, and the part never answers.

Questions and answers

Can I simulate a DHT22 with a Raspberry Pi Pico online?

Yes. The circuit at the top of this page is a Raspberry Pi Pico with a DHT22, running in your browser on a simulated RP2040. 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 Raspberry Pi Pico pin should the DHT22 DATA wire use?

Any digital pin that can be both an output and an input. This circuit uses GP15 (physical pin 20). Write the GP number in the sketch, not the physical pin number.

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 a Raspberry Pi Pico check that DHT_PIN is 15.

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.