DHT12 Humidity & Temperature Sensor
Contents
DHT12 Sensor Description


The ASAIR DHT12 is an entry level humidity and temperature sensor from the Chinese company, AOSONG Electronic Co. It is probably a discontinued product but still appears available (at time of writing, early 2025) especially through Chinese online stores where it is quite inexpensive.
The DHT12 is an upgraded version of the well known DHT11. The DHT12 has both I2C and 1-wire interfaces while the DHT11 has only the 1-wire interface.
The supply voltage for this sensor is 2.7V to 5.5V. Thus it is suitable for both 3.3V and 5V microcontrollers.
The claimed current draw for the DHT12 during the measurement cycle is 800 µA. This is fairly typical for this type of sensor.
Sensor Type and AccuracyRelative humidity is measured with a capacitive humidity sensor. This sensor consists of a hygroscopic dielectric material placed between a pair of electrodes that forms a small capacitor. The measured capacitance of the device changes as the dielectric material absorbs or loses moisture in proportion to the relative humidity of the surrounding air.
The datasheet doesn't state what type of temperature sensor technology is being used. Adafruit[1] suggest the DHT temperature sensors use a thermistor. A thermistors is a resistor that changes its resistance quite markedly with temperature. They are inexpensive, easy to use and can be quite accurate.
The product datasheet gives the following typical accuracies:
- Relative Humidity: ±5%.
- Temperature : ±0.5°C.
This sensor is relatively slow and doesn't have the accuracy of some of their competitors but their low price point make them very suitable for many projects.
Sensor Packaging
The DHT12 is encased in a robust plastic housing with dimensions of 12.3mm x 7.5mm x 4.7mm. The four pins have a 2.54mm spacing so the module is easily inserted into a breadboard.
The SDA and SCL pins require pullup resistors of around 5 to 10kΩ with a connection to VDD. External pullups aren't required when interfacing with the micro:bit. The micro:bit's I2C pins (pin19, pin20) have internal pullup resistors that work fine with this sensor.
PinoutThe DHT12 has four pins and the pinout is as expected for a sensor with i2C bus communications. Viewing the sensor from the front, the pins left to right are VDD, SDA, GND and SCL.
Reading Humidity and Temperature
The DHT12 is one of the easiest sensors in this category to use. It has an I2C bus with a 0x5C slave address that is fixed at the point of manufacture.
It also has a 1-wire interface that's compatible with the earlier DHT11 sensor. However it is much easier to use the DHT12's I2C interface when hooking it up with a micro:bit.
It measures and reports relative humidity (RH%) and temperature (°C) with a checksum for data validity. That's all it does. It doesn't have a heater, nor readable ID and the user can't reset or cause the chip to recalibrate. There is no status byte that can be read.
A single byte (0x00) is written to the sensor; presumably this is to set a register address pointer. Then five bytes are read. The format of these five bytes:
- Relative humidity integer value.
- Relative humidity decimal value.
- Temperature integer value.
- Temperature decimal value.
- Checksum; calculated as the sum of the first four bytes.
The product datasheet gives a couple of examples that make it very clear how this works. As an example:
Read from the DHT12 00111000 00001000 00011010 00000110 01100000 Relative Humidity First byte = 0b00111000 = 56 Second byte = 0b00001000 = 8 RH% = 56 + 8/10 = 56.8% Temperature Third byte = 0b00011010 = 26 Fourth byte = 0b00000110 = 6 Temperature = 26 + 6/10 = 26.6°C Checksum Fifth byte = 0b01100000 = 96 Checksum = 56 + 8 +26 + 6 = 96
If the temperature is negative then Bit[7] of the temperature's decimal byte (the fourth byte read from the sensor) will be 1. So it is important that this bit is masked out when retrieving the decimal part of the temperature and Bits[6:0] are masked out to determine if the temperature is negative.
No more than one reading should be taken every two seconds. This is a slow sensor. MicroPython will throw an OSError exception (no response from the I2C bus) if the DHT12 is pushed any harder.
And... that's all there is to this little sensor.
DH12 MicroPython Driver for micro:bit
A DHT12 MicroPython driver specifically for the micro:bit has been developed as part of this series on MicroPython for the microbit. The driver webpage also provides a detailed description of the driver's methods with sample code.
The driver allows a user to easily connect and control this sensor from a micro:bit.
The DHT12 driver is implemented as a class. It's easy for a knowledgeable user to extend the class.
As explained above; this sensor is very simple in that it only reads and reports humidity (RH%) and temperature (°C) with a simple checksum. This driver does that and also provides a simple function to convert °C to °F.