Skip to content

RobTillaart/DHT12

Repository files navigation

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

DHT12

Arduino library for I2C DHT12 temperature and humidity sensor.

Description

The DHT12 is an I2C temperature and humidity sensor.

The constructor is called only with a TwoWire object (Wire, Wire1 etc) as the device has a fixed address (0x5C). The user must call Wire.begin() in setup() before calling the begin() function.

Thereafter one has to call the read() function to do the actual reading, and with getTemperature() and getHumidity() to get the read values. Calling these latter again will return the same values until a new read() is called.

For conversions and more functionality one could check:

Related

Various temperature and humidity sensors.

I2C

The DHT12 has a fixed I2C address of 0x5C.

The DHT12 should work up to 400 KHz however this is not tested (yet).

I2C multiplexing

Sometimes you need to control more devices than possible with the default address range the device provides. This is possible with an I2C multiplexer e.g. TCA9548 which creates up to eight channels (think of it as I2C subnets) which can use the complete address range of the device.

Drawback of using a multiplexer is that it takes more administration in your code e.g. which device is on which channel. This will slow down the access, which must be taken into account when deciding which devices are on which channel. Also note that switching between channels will slow down other devices too if they are behind the multiplexer.

Interface

#include "DHT12.h"

Constructor

  • DHT12(TwoWire *wire = &Wire) constructor, using a specific Wire (I2C bus). Default is set to Wire.
  • bool begin() initializer for non ESP32. Returns true if connected.
  • bool isConnected() returns true if the address of the DHT12 can be seen on the I2C bus.

Core

  • int8_t read() read the sensor and store the values internally. It returns the status of the read which should be 0.
  • float getHumidity() returns last Humidity read, or -999 in case of error.
  • float getTemperature() returns last Temperature read, or -999 in case of error.
  • uint32_t lastRead() returns the timestamp of the last successful read in milliseconds since startup. If zero there has been no read() called yet.

Offset

  • void setHumOffset(float offset = 0) set an offset to calibrate (1st order) the sensor. Default offset is 0.
  • float getHumOffset() return current humidity offset, default 0.
  • void setTempOffset(float offset = 0) set an offset to calibrate (1st order) the sensor. Default offset is 0.
  • float getTempOffset() return current temperature offset, default 0.

Future

Must

  • improve documentation

Should

  • test at different I2C speeds
    • 400 KHz should be possible.
  • add examples.

Could

  • check for optimizations. although I2C overhead is much more.
  • investigate if it is possible to extract temperature and humidity separately
    • if so, is it faster?

Wont

  • add void setIgnoreChecksum(bool = false) ignore checksum flag speeds up communication a bit. (only a few micros, not worth it).
  • add bool getIgnoreChecksum() get status. For completeness.

Support

If you appreciate my libraries, you can support the development and maintenance. Improve the quality of the libraries by providing issues and Pull Requests, or donate through PayPal or GitHub sponsors.

Thank you,

About

Arduino library for I2C DHT12 sensor

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

 
 
 

Contributors

Languages