DHT20: the I2C humidity sensor in a DHT case
The DHT20 looks like a DHT11 with four legs, but inside it is an Aosong AHT20 and it speaks I2C at address 0x38, so the classic DHT sensor library cannot read it. Rob Tillaart鈥檚 DHT20 library is written for it: you start the Wire bus, call read(), and fetch the humidity and temperature the read stored.
Built in on Mokxi: #include <DHT20.h> and it builds, with nothing to install. Free, and no account needed.
What DHT20 does
Each measurement takes the chip about 80 ms. read() waits for that and returns a status code, DHT20_OK when the numbers are good. If you prefer not to block, requestData() starts a measurement, isMeasuring() tells you when it is done, and readData() followed by convert() collects it.
The library refuses to measure more than once a second, because the sensor heats itself when driven harder. A read inside that second returns DHT20_ERROR_LASTREAD and leaves the previous values in place, which is easy to mistake for a frozen sensor.
| Library Manager name | DHT20 |
|---|---|
| Author | Rob Tillaart |
| Include | #include <DHT20.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Humidity sensor, AHT20 |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <DHT20.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "DHT20" by Rob Tillaart from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add DHT20 to lib_deps in PlatformIO. Mokxi follows the calls of release 0.3.1, so a sketch written here builds there too.
What to know in the simulator: The DHT20 is an AHT20 in a DHT鈥檚 case and speaks the same, so the aht20 part is the one to wire.
ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
DHT20 DHT
Creates the sensor on the default Wire bus at 0x38.
DHT.begin()
Checks the chip is there and calibrated. Call Wire.begin() before it.
Wire.begin();
DHT.begin();int status = DHT.read()
Measures and waits for the result. Returns DHT20_OK, or an error such as DHT20_ERROR_CONNECT or DHT20_ERROR_CHECKSUM.
DHT.getHumidity()
The humidity from the last good read, in percent.
DHT.getTemperature()
The temperature from the last good read, in degrees Celsius.
DHT.isConnected()
True when something answers at 0x38. Handy in setup() to print a clear message instead of reading zeros.
DHT.requestData()
Starts a measurement and returns at once. Poll isMeasuring(), then call readData() and convert().
DHT.setTempOffset(-0.5)
Adds a fixed correction to every temperature, with setHumOffset() for humidity, once you have compared the sensor with a trusted thermometer.
Example: Temperature and humidity from a DHT20 (the aht20 part)
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <DHT20.h>
DHT20 DHT;
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.print("DHT20 found: ");
Serial.println(DHT.begin() ? "yes" : "no");
delay(1000);
}
void loop() {
int status = DHT.read();
if (status == DHT20_OK) {
Serial.print("Humidity: ");
Serial.print(DHT.getHumidity(), 1);
Serial.print(" % Temperature: ");
Serial.print(DHT.getTemperature(), 1);
Serial.println(" C");
} else {
Serial.print("status ");
Serial.println(status);
}
delay(1000);
}Wiring
- Arduino Uno R3 A4 to Humidity sensor, AHT20 SDA
- Arduino Uno R3 A5 to Humidity sensor, AHT20 SCL
- Arduino Uno R3 5V to Humidity sensor, AHT20 VCC
- Arduino Uno R3 GND to Humidity sensor, AHT20 GND
Common errors and how to fix them
read() returns DHT20_ERROR_CONNECT and every value is 0
Nothing answered at 0x38. Check SDA and SCL go to the board鈥檚 I2C pins, that Wire.begin() runs before DHT.begin(), and that the sensor has 3.3 or 5 V on its VDD pin.
The values never change between loops
You are reading faster than once a second, and the library returns DHT20_ERROR_LASTREAD with the old numbers. Space reads one second apart or more, and check the status before printing.
'DHT22' was not declared in this scope after swapping sensors
A DHT20 does not use the DHT sensor library or its type constants. Remove #include <DHT.h> and the DHT dht(pin, type) line, and use the DHT20 object with Wire instead.
Keep going
Learn
Try DHT20 without installing anything
Open the example, change the sketch, and keep your version in a free account.