Code Monkey home page Code Monkey logo

dht12_sensor_library's Introduction

Support forum DHT12 English
Forum supporto DHT12 italiano

Additional information and document update here on my site: DHT12 Article.

Here a comparison of the major competitor of the sensor: Temperature humidity sensors comparison (Specifications) Part 1

Temperature humidity sensors comparison (Code configuration) Part 2.

Temperature humidity sensors comparison (Data) Part 3.

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.

AI read that sometime seems that need calibration, but I have tree of this and get value very similar to DHT22. If you have calibration this problem, open issue on github and I add implementation.

06/04/2022: v1.0.2 Fix package size

Tutorial:

To download. click the DOWNLOADS button in the top right corner, 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.

Reef complete DHT12 Humidity & Temperature

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.

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

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

Thanks to Bobadas, dplasa and adafruit, to share the code in github (where I take some code and ideas).

DHT12 PIN

DHT12 Pin

DHT12 connection schema

ArduinoUNO i2c

ArduinoUNO i2c

ArduinoUNO oneWire

ArduinoUNO oneWire

esp8266 (D1Mini) i2c

esp8266 (D1Mini) i2c

esp8266 (D1Mini) oneWire

esp8266 (D1Mini) oneWire

dht12_sensor_library's People

Contributors

mwreef avatar palmerabollo avatar per1234 avatar xreef avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

dht12_sensor_library's Issues

floating point measurment

Hi xreef

In my case, I just got full numbers out of my sensor, no floating points. Maybe I miss understand something but is it true, that the library is right now not able to suport floating point?

The data Array ist defined as a int, with no floating points.
uint8_t data[5];

But one step up, we use floats for calculating with celcius and farenheid.
float DHT12::readTemperature(bool scale, bool force) { float temperature = NAN; ...

Of corse calculating with floating points uses a big amount of CPU power, at least with no FPU in your CPU. Maybe it would make sense to take in the value, multiplie it by 10'000 and then calculate everything. Then we could take two read methods. One that returnes the value as a float again devided by 10'000 and one for more CPU optimiced usage with the int value aso the original value * 10'000.

Thanks for your work, support and maybe clearithing of my possible missunderstanding.

bests Night

SDA and SCL pins

Thanks for library.
But i have an issue.
I got an DHT12 sensor and i tried to connect it to and ESP8266.
Althought the example says to connect SDA to D1 and SCL to D2 it never worked.
So i connect SDL to D1 and SDA to D2
Then the sensor worked fine.

No negative value reading?

Hi there,

I really like your project and library, but my issue is that I can't get negative measurement value when reading the sensor output.

In a room environment everything seems to be good as I see, but if I put the sensor outdoor I got really weird readings:

- DHT 12 (i2c)
Status: Ok
Temperature: 13.50 c
Humidity: 95.00 %

and here is the readings from our local weather station (about 300-400m from my house):

Temperature: -0.70 c
Humidity: 96.00 %

Tested 12pcs DHT12 sensors (brand new ones), I tought that was defective, but I got the same results with all of them.

Using WeMos D1 mini, Arduino IDE v1.8.2, ESP8266 Core v2.3.0.

Thank you for your answer! ๐Ÿ˜‰

Kind regards,
Viktor

Incorrect moisture readings

Temperature readings coincide with other devices in the house: split system, xiaomi sensors.

But humidity indicators are greatly underestimated. At an actual 39-40 percent, the sensor outputs 21-24 percent.
Test 2 different HDT12 sensors, the deviation is the same.

Wemos D1 Mini + DHT12 I2C Shield + DHT12_sensor_library

Hello

I'm newbie and trying to use the library with my wemos d1 mini and its DHT12 shield soldered.

Actually with the default configuration, I 've got an error and I can't read the DHT12 sensor.

According to wemos :

  • The WeMos D1 Mini I2C bus uses pins:
  • D1 = SCL
  • D2 = SDA

How can I check that there are used by the library or that force the library to use them ?

Thanks

working with second I2C bus for example with ESP32

The ESP32 has two I2C buses. The first bus is mostly designed for an OLED and can also be used with the DHT12 or other sensor. The gpio pis are mostly SDA / SCL: 5/4 or 4/15.
The second I2C bus has the gpio pins SDA / SCL 21/22.
For DHT12 to work with this bus, some changes are needed in DHT12.cpp and DHT12.h.
The changes follow:

Example code compile fail with NANO 33 IoT

Hello,
When I compile this example named "ArduinoOneWireDHT12", an error occurs and cannot be compiled. The error message is as follows. Any suggestion ?

Arduino:1.8.12 (Windows 7), ้–‹็™ผๆฟ:"Arduino NANO 33 IoT"
In file included from C:\Users\eric\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.6\cores\arduino/delay.h:23:0,
from C:\Users\eric\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.6\cores\arduino/Arduino.h:81,
from D:\arduino-1.8.12\libraries\DHT12_sensor_library-master\DHT12.h:32,
from D:\arduino-1.8.12\libraries\DHT12_sensor_library-master\DHT12.cpp:28:
D:\arduino-1.8.12\libraries\DHT12_sensor_library-master\DHT12.cpp: In constructor 'DHT12::DHT12(uint8_t, bool)':

C:\Users\eric\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.6\variants\nano_33_iot/variant.h:50:35: error: invalid conversion from 'PortGroup*' to 'uint8_t {aka unsigned char}' [-fpermissive]

#define digitalPinToPort(P) (&(PORT->Group[g_APinDescription[P].ulPort]))

                              ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

D:\arduino-1.8.12\libraries\DHT12_sensor_library-master\DHT12.cpp:43:12: note: in expansion of macro 'digitalPinToPort'

_port = digitalPinToPort(_pin);

        ^~~~~~~~~~~~~~~~

D:\arduino-1.8.12\libraries\DHT12_sensor_library-master\DHT12.cpp: In member function 'uint32_t DHT12::expectPulse(bool)':

C:\Users\eric\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.8.6\variants\nano_33_iot/variant.h:54:41: error: base operand of '->' is not a pointer

#define portInputRegister(port) (&(port->IN.reg))

                                     ^

D:\arduino-1.8.12\libraries\DHT12_sensor_library-master\DHT12.cpp:495:11: note: in expansion of macro 'portInputRegister'

while ((*portInputRegister(_port) & _bit) == portState) {

       ^~~~~~~~~~~~~~~~~

exit status 1
้–‹็™ผๆฟ Arduino NANO 33 IoT ็ทจ่ญฏ้Œฏ่ชคใ€‚
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.