ESP32 and DHT12 humidity temperature sensor example

The DHt12 is an upgraded version of the  DHT11 humidity temperature sensor, it is fully downward compatible, more precise and most importantly it adds an I2C interface.

Features:

compact size
low power consumption
low voltage operation
Standard I2C and 1-wire interface.

Sensing range
Temperature: -20 ~ +60 C
Humidity: 20-95 RH
Humidity:
Resolution: 0.1%RH
Repeat: -+ 1%RH
Precision 25C @ -+5RH
Temperature:
Resolution: 0.1C
Repeat: -+0.2C
Precision: 25C @ -+0.5C
Power: DC 2.7-5.5V
Normal current 1mA
Standby current 60uA
Sample cycle: > 2 seconds

Pin interface: 1. VDD 2. SDA 3. GND 4. SCL (connect to GND when use as 1-wire)

 

Layout

This shows how to connect the DHT12 to a Wemos LOLIN32

Code

You will need to install the following library –  https://github.com/xreef/DHT12_sensor_library

 

 

[codesyntax lang=”cpp”]

#include "Arduino.h"

#include <DHT12.h>

// Set dht12 i2c comunication on default Wire pin
DHT12 dht12;

void setup()
{
Serial.begin(112560);
// Start sensor handshake
dht12.begin();
}
int timeSinceLastRead = 0;

void loop()
{
// Report every 2 seconds.
if(timeSinceLastRead > 2000) {
// Reading temperature or humidity takes about 250 milliseconds!
// Read temperature as Celsius (the default)
float t12 = dht12.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f12 = dht12.readTemperature(true);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h12 = dht12.readHumidity();

bool dht12Read = true;
// Check if any reads failed and exit early (to try again).
if (isnan(h12) || isnan(t12) || isnan(f12)) {
Serial.println("Failed to read from DHT12 sensor!");

dht12Read = false;
}

if (dht12Read){
// Compute heat index in Fahrenheit (the default)
float hif12 = dht12.computeHeatIndex(f12, h12);
// Compute heat index in Celsius (isFahreheit = false)
float hic12 = dht12.computeHeatIndex(t12, h12, false);
// Compute dew point in Fahrenheit (the default)
float dpf12 = dht12.dewPoint(f12, h12);
// Compute dew point in Celsius (isFahreheit = false)
float dpc12 = dht12.dewPoint(t12, h12, false);

Serial.print("DHT12=> Humidity: ");
Serial.print(h12);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t12);
Serial.print(" *C ");
Serial.print(f12);
Serial.print(" *F\t");
Serial.print(" Heat index: ");
Serial.print(hic12);
Serial.print(" *C ");
Serial.print(hif12);
Serial.print(" *F");
Serial.print(" Dew point: ");
Serial.print(dpc12);
Serial.print(" *C ");
Serial.print(dpf12);
Serial.println(" *F");
}
timeSinceLastRead = 0;
}
delay(100);
timeSinceLastRead += 100;

}

[/codesyntax]

 

 

 

Output

Open the serial monitor and you should readings something like this

DHT12=> Humidity: 33.70 % Temperature: 20.20 *C 68.36 *F Heat index: 19.16 *C 66.48 *F Dew point: 3.60 *C 38.48 *F
DHT12=> Humidity: 39.70 % Temperature: 20.60 *C 69.08 *F Heat index: 19.75 *C 67.55 *F Dew point: 6.34 *C 43.41 *F
DHT12=> Humidity: 44.60 % Temperature: 21.20 *C 70.16 *F Heat index: 20.54 *C 68.97 *F Dew point: 8.64 *C 47.55 *F
DHT12=> Humidity: 47.80 % Temperature: 21.80 *C 71.24 *F Heat index: 21.28 *C 70.31 *F Dew point: 10.25 *C 50.45 *F
DHT12=> Humidity: 48.90 % Temperature: 22.40 *C 72.32 *F Heat index: 21.97 *C 71.55 *F Dew point: 11.16 *C 52.08 *F
DHT12=> Humidity: 53.90 % Temperature: 22.90 *C 73.22 *F Heat index: 22.65 *C 72.78 *F Dew point: 13.14 *C 55.66 *F
DHT12=> Humidity: 55.10 % Temperature: 23.60 *C 74.48 *F Heat index: 23.45 *C 74.22 *F Dew point: 14.15 *C 57.46 *F
DHT12=> Humidity: 54.90 % Temperature: 23.90 *C 75.02 *F Heat index: 23.78 *C 74.80 *F Dew point: 14.37 *C 57.86 *F

 

Link

These sensors are only about $1 each

1pcs DHT-12 DHT12 sensor Digital output Temperature and Humidity Sensor high quality