nanoFramework.Iot.Device.Dhtxx
1.2.573
Prefix Reserved
See the version list below for details.
dotnet add package nanoFramework.Iot.Device.Dhtxx --version 1.2.573
NuGet\Install-Package nanoFramework.Iot.Device.Dhtxx -Version 1.2.573
<PackageReference Include="nanoFramework.Iot.Device.Dhtxx" Version="1.2.573" />
paket add nanoFramework.Iot.Device.Dhtxx --version 1.2.573
#r "nuget: nanoFramework.Iot.Device.Dhtxx, 1.2.573"
// Install nanoFramework.Iot.Device.Dhtxx as a Cake Addin #addin nuget:?package=nanoFramework.Iot.Device.Dhtxx&version=1.2.573 // Install nanoFramework.Iot.Device.Dhtxx as a Cake Tool #tool nuget:?package=nanoFramework.Iot.Device.Dhtxx&version=1.2.573
DHT10/DHT11/DHT12/DHT21/DHT22 - Digital-Output Relative Humidity & Temperature Sensor Module
IMPORTANT This sensor is very time sensitive. This implementation will only work on few boards. Do not use with ESP32. If you are working with an ESP32, to use any of the DHT with 1 wire protocol, please use the Dhtxx.Esp32 version.
The DHT temperature and humidity sensors are very popular. This projects support DHT10, DHT11, DHT12, DHT21(AM2301), DHT22(AM2302).
Documentation
DHT10 | DHT11 | DHT12 | DHT21 | DHT22 | |
---|---|---|---|---|---|
Image | |||||
Temperature Range | -40 ~ 80 ℃ | 0 ~ 60 ℃ | -20 ~ 60 ℃ | -40 ~ 80 ℃ | -40 ~ 80 ℃ |
Humidity Range | 0 ~ 99.9 % | 2 ~ 95 % | 20 ~ 95 % | 0 ~ 99.9 % | 0 ~ 99.9 % |
Temperature Accuracy | ±0.5 ℃ | ±2 ℃ | ±0.5 ℃ | ±0.5 ℃ | ±0.5 ℃ |
Humidity Accuracy | ±3 % | ±5 % | ±4 % | ±3 % | ±2 % |
Protocol | I2C | 1-Wire | I2C, 1-Wire | 1-Wire | 1-Wire |
- DHT10 datasheet (Currently only Chinese)
- DHT11 datasheet
- DHT12 datasheet
- DHT21 datasheet
- DHT22 datasheet
Usage
1-Wire Protocol
// GPIO Pin
using (Dht11 dht = new Dht11(26))
{
var temperature = dht.Temperature;
var humidity = dht.Humidity;
// You can only display temperature and humidity if the read is successful otherwise, this will raise an exception as
// both temperature and humidity are NAN
if (dht.IsLastReadSuccessful)
{
Debug.WriteLine($"Temperature: {temperature.DegreesCelsius} \u00B0C, Humidity: {humidity.Percent} %");
// WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity.
Debug.WriteLine(
$"Heat index: {WeatherHelper.CalculateHeatIndex(temperature, humidity).Celsius:0.#}\u00B0C");
Debug.WriteLine(
$"Dew point: {WeatherHelper.CalculateDewPoint(temperature, humidity).Celsius:0.#}\u00B0C");
}
else
{
Debug.WriteLine("Error reading DHT sensor");
}
}
Note: On the RPi with any of the DHT sensor, 1-Wire works using Raspian but not with Windows 10 IoT Core. The device has to switch the 1-wire pin between input and output and vice versa. It seems that Windows IoT Core OS can't switch the pin direction quick enough. There have been suggestions for using two pins; one for input and one for output. This solution has not been implemented here, but these are some handy links that may help setting that up:
- https://github.com/ms-iot/samples/tree/develop/GpioOneWire
- And on Hackster.io: https://www.hackster.io/porrey/go-native-c-with-the-dht22-a8e8eb
I2C Protocol
Important: make sure you properly setup the I2C pins especially for ESP32 before creating the I2cDevice
, make sure you install the nanoFramework.Hardware.ESP32 nuget
:
//////////////////////////////////////////////////////////////////////
// when connecting to an ESP32 device, need to configure the I2C GPIOs
// used for the bus
Configuration.SetPinFunction(21, DeviceFunction.I2C1_DATA);
Configuration.SetPinFunction(22, DeviceFunction.I2C1_CLOCK);
For other devices like STM32, please make sure you're using the preset pins for the I2C bus you want to use.
Only DHT12 can use I2C protocol.
I2cConnectionSettings settings = new I2cConnectionSettings(1, DhtSensor.DefaultI2cAddressDht12);
I2cDevice device = I2cDevice.Create(settings);
using (Dht12 dht = new Dht12(device))
{
var tempValue = dht.Temperature;
var humValue = dht.Humidity;
if (dht.IsLastReadSuccessful)
{
Debug.WriteLine($"Temperature: {tempValue.Celsius:0.#}\u00B0C");
Debug.WriteLine($"Relative humidity: {humValue:0.#}%");
// WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity.
Debug.WriteLine(
$"Heat index: {WeatherHelper.CalculateHeatIndex(tempValue, humValue).Celsius:0.#}\u00B0C");
Debug.WriteLine(
$"Dew point: {WeatherHelper.CalculateDewPoint(tempValue, humValue).Celsius:0.#}\u00B0C");
}
else
{
Debug.WriteLine("Error reading DHT sensor");
}
}
Reading frequency and quality measurement
In the case of I2C or GPIO, any type of DHT needs a bit of time between 2 readings. DHT22 documentation refer to a sensing period of 2 seconds and a collecting period higher than 1.7 seconds. Measuring with higher frequency won't give you more accurate numbers. As you can see from the specifications, the accuracy depends on the sensor type, it goes from ±2 ℃ for the DHT11 to ±0.5 ℃ for the others. Even if the parity check can come clear, we do recommend to check that the data are in a normal range. For example of humidity is higher than 100%, then it means that measurement is wrong. This check has not been done in the binding itself, so you may consider adding a check on your application side.
The DHT sensors are very sensitive, avoid too long cables, electromagnetic perturbations and compile the code as release not debug to increase the quality of measurement.
FAQ
I always get wrong measurements, what's happening?
Please check that the sensor is plugged correctly, make sure you are using the correct pin.
Please check you are using the correct sensor, only DHT10 and DHT12 supports I2C. All others support only GPIO with 1 wire protocol. DHT12 supports both.
The data I measure are not correct, humidity seems ok but temperature is always weird, what's the problem?
Please check you are using the correct sensor. Refer to the top part of this page to check which sensor you have. Using a DHT11 instead of a DHT22 will give you a wrong temperature.
I am trying to get a temperature and humidity 5 times per seconds but I mainly get wrong measurements, why?
This is absolutely normal, you should check the measurements once every 2 seconds approximately. Don't try to get more measures than once every 2 seconds.
When reading the temperature and humidity and trying to write the data in the console, I get an exception, why?
You need to check first if the measurement has been successful. If the measurement hasn't been successful, the default values will be NaN and so you won't be able to convert the temperature or humidity and you'll get an exception. This is the correct way of first reading the sensor and then checking the reading was correct and finally using the temperature and humidity data:
var tempValue = dht.Temperature;
var humValue = dht.Humidity;
if (dht.IsLastReadSuccessful)
{
Debug.WriteLine($"Temperature: {tempValue.Celsius:0.#}\u00B0C");
Debug.WriteLine($"Relative humidity: {humValue:0.#}%");
}
I have a Raspberry Pi 4 and I get an exception when creating the DHT sensor
See this issue 1145. We're actively trying to fix it automatically. You will have to force using either the Raspberry Pi 3 driver, either the LibGpiodDriver. This is how you can force using a specific drive, in this case the Raspberry Pi 3 one which will work:
GpioDriver driver = new RaspberryPi3Driver();
var controller = new GpioController(PinNumberingScheme.Logical, driver);
// This uses pin 4 in the logical schema so pin 7 in the physical schema
var dht = new Dht11(4, gpioController: controller);
My DHT sensor using 1 wire protocol is not working on my Raspberry Pi with Windows 10 IoT Core, what can I do?
On the RPi with any of the DHT sensor, 1-Wire works using Raspian but not with Windows 10 IoT Core. The device has to switch the 1-wire pin between input and output and vice versa. It seems that Windows IoT Core OS can't switch the pin direction quick enough. There have been suggestions for using two pins; one for input and one for output. This solution has not been implemented here, but these are some handy links that may help setting that up:_
- https://github.com/ms-iot/samples/tree/develop/GpioOneWire
- And on Hackster.io: https://www.hackster.io/porrey/go-native-c-with-the-dht22-a8e8eb
Now if your sensor is an I2C sensor, it should just work perfectly on Windows 10 IoT Core.
Example of DHTxx
Hardware Required
- DHT10/DHT11/DHT12/DHT21/DHT22
- Male/Female Jumper Wires
Circuit
1-Wire Protocol Circuit
Simply connect your DHTxx data pin to GPIO26 (physical pin 37), the ground to the ground (physical pin 6) and the VCC to +5V (physical pin 2).
Some sensors are already sold with the 10K resistor. Connect the GPIO26 to the data pin, its position can vary depending on the integrator.
I2C Protocol Circuit
- SCL - SCL
- SDA - SDA
- VCC - 5V
- GND - GND
Code
// GPIO Pin
using (Dht11 dht = new Dht11(26))
{
var temperature = dht.Temperature;
var humidity = dht.Humidity;
// You can only display temperature and humidity if the read is successful otherwise, this will raise an exception as
// both temperature and humidity are NAN
if (dht.IsLastReadSuccessful)
{
Debug.WriteLine($"Temperature: {temperature.DegreesCelsius} \u00B0C, Humidity: {humidity.Percent} %");
// WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity.
Debug.WriteLine(
$"Heat index: {WeatherHelper.CalculateHeatIndex(temperature, humidity).Celsius:0.#}\u00B0C");
Debug.WriteLine(
$"Dew point: {WeatherHelper.CalculateDewPoint(temperature, humidity).Celsius:0.#}\u00B0C");
}
else
{
Debug.WriteLine("Error reading DHT sensor");
}
}
Sample application navigation
This sample application allows you to select either a DHT10 through I2C either any other supported DHT through GPIO:
Select the DHT sensor you want to use:
1. DHT10 on I2C
2. DHT11 on GPIO
3. DHT12 on GPIO
4. DHT21 on GPIO
5. DHT22 on GPIO
Just select the sensor you want to test and use by typing the number. For example, if you want to test a DHT22, type 5.
Then, you are prompted to type the pin number in the logical schema:
Which pin do you want to use in the logical pin schema?
If you want to use the pin 26, then type 26 and enter. This will then create a DHT22 sensor attached to pin 26 and start the measurement.
Please note that the few first measurements won't be correct, that's totally normal and related to the fact the sensor needs a bit of time to warm up and give data. Those sensors are very sensitive and too long wires, many perturbations, code compile as debug will increase the numbers of bad readings.
Result
Note: reading this sensor is sensitive, if you can't read anything, make sure you have it correctly cabled. Also note you'll get better results when running in Release
mode.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net is compatible. |
-
- nanoFramework.CoreLibrary (>= 1.15.5)
- nanoFramework.Runtime.Events (>= 1.11.18)
- nanoFramework.System.Device.Gpio (>= 1.1.41)
- nanoFramework.System.Device.I2c (>= 1.1.16)
- nanoFramework.System.Device.Model (>= 1.2.418)
- nanoFramework.System.Diagnostics.Stopwatch (>= 1.2.418)
- nanoFramework.System.Math (>= 1.5.43)
- UnitsNet.nanoFramework.RelativeHumidity (>= 5.52.0)
- UnitsNet.nanoFramework.Temperature (>= 5.52.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.2.673 | 77 | 10/23/2024 |
1.2.662 | 77 | 10/11/2024 |
1.2.656 | 74 | 10/3/2024 |
1.2.639 | 102 | 9/6/2024 |
1.2.631 | 98 | 8/28/2024 |
1.2.613 | 106 | 8/9/2024 |
1.2.601 | 81 | 7/26/2024 |
1.2.590 | 95 | 7/17/2024 |
1.2.573 | 108 | 6/19/2024 |
1.2.570 | 101 | 6/14/2024 |
1.2.560 | 114 | 5/29/2024 |
1.2.548 | 128 | 5/15/2024 |
1.2.536 | 115 | 4/15/2024 |
1.2.514 | 122 | 3/22/2024 |
1.2.494 | 112 | 2/28/2024 |
1.2.474 | 128 | 1/24/2024 |
1.2.462 | 141 | 1/5/2024 |
1.2.458 | 130 | 12/20/2023 |
1.2.436 | 182 | 11/10/2023 |
1.2.416 | 117 | 11/8/2023 |
1.2.403 | 147 | 10/6/2023 |
1.2.396 | 128 | 9/27/2023 |
1.2.384 | 147 | 9/6/2023 |
1.2.378 | 132 | 8/16/2023 |
1.2.369 | 168 | 8/2/2023 |
1.2.363 | 149 | 7/28/2023 |
1.2.357 | 157 | 7/19/2023 |
1.2.354 | 153 | 7/14/2023 |
1.2.345 | 154 | 6/21/2023 |
1.2.341 | 127 | 6/14/2023 |
1.2.337 | 150 | 6/7/2023 |
1.2.335 | 157 | 6/2/2023 |
1.2.329 | 145 | 5/26/2023 |
1.2.316 | 152 | 5/16/2023 |
1.2.313 | 148 | 5/12/2023 |
1.2.302 | 130 | 5/10/2023 |
1.2.297 | 150 | 5/3/2023 |
1.2.273 | 284 | 3/17/2023 |
1.2.267 | 223 | 3/10/2023 |
1.2.263 | 246 | 3/8/2023 |
1.2.259 | 259 | 2/27/2023 |
1.2.256 | 238 | 2/24/2023 |
1.2.253 | 252 | 2/22/2023 |
1.2.222 | 340 | 1/9/2023 |
1.2.217 | 331 | 1/6/2023 |
1.2.208 | 307 | 1/3/2023 |
1.2.203 | 314 | 12/28/2022 |
1.2.159 | 385 | 11/14/2022 |
1.2.153 | 375 | 11/5/2022 |
1.2.141 | 403 | 10/25/2022 |
1.2.128 | 390 | 10/22/2022 |
1.2.122 | 447 | 10/12/2022 |
1.2.114 | 434 | 10/8/2022 |
1.2.95 | 451 | 9/22/2022 |
1.2.87 | 536 | 9/15/2022 |
1.2.73 | 438 | 9/8/2022 |
1.2.63 | 433 | 9/3/2022 |
1.2.47 | 470 | 8/15/2022 |
1.2.40 | 463 | 8/6/2022 |
1.2.38 | 445 | 8/5/2022 |
1.2.28 | 442 | 8/1/2022 |
1.2.13 | 464 | 7/24/2022 |
1.2.10 | 465 | 7/23/2022 |
1.1.142.3202 | 481 | 7/7/2022 |
1.1.133.52556 | 457 | 6/30/2022 |
1.1.121.35854 | 451 | 6/26/2022 |
1.1.116.8772 | 451 | 6/24/2022 |
1.1.113.2032 | 451 | 6/23/2022 |
1.1.102.51394 | 462 | 6/15/2022 |
1.1.99.36719 | 434 | 6/14/2022 |
1.1.72.29765 | 432 | 5/31/2022 |
1.1.64.21380 | 450 | 5/26/2022 |
1.1.58.10097 | 471 | 5/23/2022 |
1.1.54.28879 | 466 | 5/23/2022 |
1.1.40 | 505 | 5/5/2022 |
1.1.3 | 505 | 4/15/2022 |
1.1.1 | 462 | 4/14/2022 |
1.0.300 | 451 | 3/31/2022 |
1.0.288-preview.114 | 129 | 3/25/2022 |
1.0.288-preview.113 | 116 | 3/25/2022 |
1.0.288-preview.103 | 106 | 3/21/2022 |
1.0.288-preview.100 | 119 | 3/19/2022 |
1.0.288-preview.99 | 123 | 3/18/2022 |
1.0.288-preview.98 | 115 | 3/18/2022 |
1.0.288-preview.94 | 123 | 3/15/2022 |
1.0.288-preview.93 | 118 | 3/15/2022 |
1.0.288-preview.87 | 121 | 3/10/2022 |
1.0.288-preview.86 | 119 | 3/8/2022 |
1.0.288-preview.77 | 121 | 2/27/2022 |
1.0.288-preview.75 | 114 | 2/26/2022 |
1.0.288-preview.65 | 121 | 2/18/2022 |
1.0.288-preview.63 | 118 | 2/16/2022 |
1.0.288-preview.61 | 121 | 2/12/2022 |
1.0.288-preview.58 | 115 | 2/10/2022 |
1.0.288-preview.53 | 109 | 2/9/2022 |
1.0.288-preview.48 | 138 | 2/4/2022 |
1.0.288-preview.41 | 131 | 1/31/2022 |
1.0.288-preview.29 | 127 | 1/28/2022 |
1.0.288-preview.20 | 141 | 1/27/2022 |
1.0.288-preview.19 | 141 | 1/27/2022 |
1.0.288-preview.18 | 129 | 1/27/2022 |
1.0.288-preview.5 | 134 | 1/24/2022 |
1.0.288-preview.3 | 122 | 1/21/2022 |
1.0.288-preview.1 | 130 | 1/21/2022 |
1.0.272 | 175 | 1/10/2022 |
1.0.261 | 327 | 12/22/2021 |
1.0.259 | 325 | 12/9/2021 |
1.0.258 | 325 | 12/7/2021 |
1.0.155 | 380 | 8/31/2021 |
1.0.143 | 244 | 7/21/2021 |
1.0.91 | 189 | 5/27/2021 |