DHT12 complete and working library

Last Modified: January 19, 2018, at 03:58 AM
By: Renzo Mischianti
Platforms: Arduino, esp8266

Download

Latest version on github

Direct download zip

Intro

This is an Arduino and esp8266 library for the DHT12 series of very low cost temperature/humidity sensors (less than 1$) that work with i2c or one wire connection.

To download. click the "Direct download zip", rename the uncompressed folder DHT12. Check that the DHT folder contains DHT12.cpp and DHT12.h. Place the DHT library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.

Sensor image


DHT12 sensor

Features

  • Super compact size
  • Super low power consumption
  • Super low voltage operation
  • Standard I2C and 1-wire interface.
  • Semi conductor technology
  • 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)

Usage

This libray try to emulate the behaivor of standard DHT library sensors (and copy a lot of code), and I add the code to manage i2c olso in the same manner.

The method is the same of DHT library sensor, with some adding like dew point function.

To use with i2c (default address and default SDA SCL pin) the constructor is:

DHT12 dht12; and take the default value for SDA SCL pin. (It's possible to redefine with specified contructor for esp8266, needed for ESP-01). or

DHT12 dht12(uint8_t addressOrPin)

addressOrPin -> address to change address.

To use one wire:

DHT12 dht12(uint8_t addressOrPin, true)

addressOrPin -> pin boolean value is the selection of oneWire or i2c mode.

You can use It with "implicit", "simple read" or "fullread": Implicit, only the first read doing a true read of the sensor, the other read that become in 2secs. interval are the stored value of first read.

Implicit read

// The read of sensor have 2secs of elapsed time, unless you pass force parameter
                // 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();


                // 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);

Simple read to get a status of read.


// The read of sensor have 2secs of elapsed time, unless you pass force parameter
                bool chk = dht12.read(); // true read is ok, false read problem

                // 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();

                // 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);

Full read to get a specified status.


// The read of sensor have 2secs of elapsed time, unless you pass force parameter
                DHT12::ReadStatus chk = dht12.readStatus();
                Serial.print(F("\nRead sensor: "));
                switch (chk) {
                case DHT12::OK:
                        Serial.println(F("OK"));
                        break;
                case DHT12::ERROR_CHECKSUM:
                        Serial.println(F("Checksum error"));
                        break;
                case DHT12::ERROR_TIMEOUT:
                        Serial.println(F("Timeout error"));
                        break;
                case DHT12::ERROR_TIMEOUT_LOW:
                        Serial.println(F("Timeout error on low signal, try put high pullup resistance"));
                        break;
                case DHT12::ERROR_TIMEOUT_HIGH:
                        Serial.println(F("Timeout error on low signal, try put low pullup resistance"));
                        break;
                case DHT12::ERROR_CONNECT:
                        Serial.println(F("Connect error"));
                        break;
                case DHT12::ERROR_ACK_L:
                        Serial.println(F("AckL error"));
                        break;
                case DHT12::ERROR_ACK_H:
                        Serial.println(F("AckH error"));
                        break;
                case DHT12::ERROR_UNKNOWN:
                        Serial.println(F("Unknown error DETECTED"));
                        break;
                case DHT12::NONE:
                        Serial.println(F("No result"));
                        break;
                default:
                        Serial.println(F("Unknown error"));
                        break;
                }

                // 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();

                // 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);

Connection diagram

With examples in the code, there are the connection diagram, it's important to use correct pullup resistor.


ArduinoUNO i2c


ArduinoUNO oneWire


esp8266 (D1Mini) i2c


esp8266 (D1Mini) oneWire