Source: https://mokxi.com/libraries/dht20
Updated: 2026-11-02

Libraries / Sensors
Arduino library · Sensors

# 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’s DHT20 library is written for it: you start the Wire bus, call read(), and fetch the humidity and temperature the read stored.

Open the example in the editor See the calls

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’s 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’s 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.

### 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.

sketch.ino · Arduino Uno R3

### 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

Open it in the editor, wired and ready to run

## 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’s 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

- I2C Explained: Addresses, Acknowledges and Pull-Ups
- Arduino DHT22 and DHT11: Temperature and Humidity

### Parts

- Humidity sensor, AHT20

### More sensors libraries

- DHT sensor library
- VL53L0X
- Adafruit_VL53L0X
- HX711 Arduino Library
- MPU6050
- Adafruit MPU6050
- BME280
- Adafruit BMP280 Library

Every built-in library · Open the editor

## Try DHT20 without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
