Skip to content

Commit 40d79ff

Browse files
authored
Fixes for Dhtxx (#50)
1 parent a701287 commit 40d79ff

File tree

9 files changed

+150
-5
lines changed

9 files changed

+150
-5
lines changed

devices/Dhtxx/Dhtxx.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<RootNamespace>Iot.Device.Dhtxx</RootNamespace>
1616
<AssemblyName>Iot.Device.Dhtxx</AssemblyName>
1717
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
<DocumentationFile>bin\$(Configuration)\Iot.Device.Dhtxx.xml</DocumentationFile>
1819
</PropertyGroup>
1920
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2021
<ItemGroup>

devices/Dhtxx/Dhtxx.nuspec

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
</dependencies>
3131
</metadata>
3232
<files>
33-
<file src="bin\Release\Iot.Device.Dhtxxx.dll" target="lib\Iot.Device.Dhtxxx.dll" />
34-
<file src="bin\Release\Iot.Device.Dhtxxx.pdb" target="lib\Iot.Device.Dhtxxx.pdb" />
35-
<file src="bin\Release\Iot.Device.Dhtxxx.pdbx" target="lib\Iot.Device.Dhtxxx.pdbx" />
36-
<file src="bin\Release\Iot.Device.Dhtxxx.pe" target="lib\Iot.Device.Dhtxxx.pe" />
37-
<file src="bin\Release\Iot.Device.Dhtxxx.xml" target="lib\Iot.Device.Dhtxxx.xml" />
33+
<file src="bin\Release\Iot.Device.Dhtxx.dll" target="lib\Iot.Device.Dhtxx.dll" />
34+
<file src="bin\Release\Iot.Device.Dhtxx.pdb" target="lib\Iot.Device.Dhtxx.pdb" />
35+
<file src="bin\Release\Iot.Device.Dhtxx.pdbx" target="lib\Iot.Device.Dhtxx.pdbx" />
36+
<file src="bin\Release\Iot.Device.Dhtxx.pe" target="lib\Iot.Device.Dhtxx.pe" />
37+
<file src="bin\Release\Iot.Device.Dhtxx.xml" target="lib\Iot.Device.Dhtxx.xml" />
3838
<file src="readme.md" target="" />
3939
<file src="..\..\assets\nf-logo.png" target="images" />
4040
<file src="..\..\LICENSE.md" target="" />

devices/Dhtxx/README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# DHTxx - Digital-Output Relative Humidity & Temperature Sensor Module
2+
3+
The DHT temperature and humidity sensors are very popular. This projects support DHT10, DHT11, DHT12, DHT21(AM2301), DHT22(AM2302).
4+
5+
## Comparison
6+
7+
| | DHT10 | DHT11 | DHT12 | DHT21 | DHT22 |
8+
| :------: | :------: | :------: | :------: | :------: | :------: |
9+
| Image | <img src="imgs/dht10.jpg" height="60"/> | <img src="imgs/dht11.jpg" height="60"/> | <img src="imgs/dht12.jpg" height="60"/> | <img src="imgs/dht21.jpg" height="60"/> | <img src="imgs/dht22.jpg" height="60"/> |
10+
| Temperature Range | -40 ~ 80 ℃ | 0 ~ 60 ℃ | -20 ~ 60 ℃ | -40 ~ 80 ℃ | -40 ~ 80 ℃ |
11+
| Humidity Range | 0 ~ 99.9 % | 2 ~ 95 % | 20 ~ 95 % | 0 ~ 99.9 % | 0 ~ 99.9 % |
12+
| Temperature Accuracy | ±0.5 ℃ | ±2 ℃ | ±0.5 ℃ | ±0.5 ℃ | ±0.5 ℃ |
13+
| Humidity Accuracy | ±3 % | ±5 % | ±4 % | ±3 % | ±2 % |
14+
| Protocol | I2C | 1-Wire | I2C, 1-Wire | 1-Wire | 1-Wire |
15+
16+
## Usage
17+
18+
### 1-Wire Protocol
19+
20+
```csharp
21+
// GPIO Pin
22+
using (Dht11 dht = new Dht11(26))
23+
{
24+
var temperature = dht.Temperature;
25+
var humidity = dht.Humidity;
26+
// You can only display temperature and humidity if the read is successful otherwise, this will raise an exception as
27+
// both temperature and humidity are NAN
28+
if (dht.IsLastReadSuccessful)
29+
{
30+
Console.WriteLine($"Temperature: {temperature.DegreesCelsius} \u00B0C, Humidity: {humidity.Percent} %");
31+
32+
// WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity.
33+
Console.WriteLine(
34+
$"Heat index: {WeatherHelper.CalculateHeatIndex(temperature, humidity).Celsius:0.#}\u00B0C");
35+
Console.WriteLine(
36+
$"Dew point: {WeatherHelper.CalculateDewPoint(temperature, humidity).Celsius:0.#}\u00B0C");
37+
}
38+
else
39+
{
40+
Console.WriteLine("Error reading DHT sensor");
41+
}
42+
}
43+
```
44+
**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:_
45+
- https://github.com/ms-iot/samples/tree/develop/GpioOneWire
46+
- And on Hackster.io: https://www.hackster.io/porrey/go-native-c-with-the-dht22-a8e8eb
47+
48+
### I2C Protocol
49+
50+
Only DHT12 can use I2C protocol.
51+
52+
```csharp
53+
I2cConnectionSettings settings = new I2cConnectionSettings(1, DhtSensor.DefaultI2cAddressDht12);
54+
I2cDevice device = I2cDevice.Create(settings);
55+
56+
using (Dht12 dht = new Dht12(device))
57+
{
58+
var tempValue = dht.Temperature;
59+
var humValue = dht.Humidity;
60+
if (dht.IsLastReadSuccessful)
61+
{
62+
Console.WriteLine($"Temperature: {tempValue.Celsius:0.#}\u00B0C");
63+
Console.WriteLine($"Relative humidity: {humValue:0.#}%");
64+
65+
// WeatherHelper supports more calculations, such as saturated vapor pressure, actual vapor pressure and absolute humidity.
66+
Console.WriteLine(
67+
$"Heat index: {WeatherHelper.CalculateHeatIndex(tempValue, humValue).Celsius:0.#}\u00B0C");
68+
Console.WriteLine(
69+
$"Dew point: {WeatherHelper.CalculateDewPoint(tempValue, humValue).Celsius:0.#}\u00B0C");
70+
}
71+
else
72+
{
73+
Console.WriteLine("Error reading DHT sensor");
74+
}
75+
}
76+
```
77+
78+
## Reading frequency and quality measurement
79+
80+
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.
81+
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.
82+
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.
83+
This check has not been done in the binding itself, so you may consider adding a check on your application side.
84+
85+
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.
86+
87+
## FAQ
88+
89+
**I always get wrong measurements, what's happening?**
90+
91+
Please check that the sensor is plugged correctly, make sure you are using the correct pin.
92+
93+
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.
94+
95+
**The data I measure are not correct, humidity seems ok but temperature is always weird, what's the problem?**
96+
97+
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.
98+
99+
**I am trying to get a temperature and humidity 5 times per seconds but I mainly get wrong measurements, why?**
100+
101+
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.
102+
103+
**When reading the temperature and humidity and trying to write the data in the console, I get an exception, why?**
104+
105+
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:
106+
107+
```csharp
108+
var tempValue = dht.Temperature;
109+
var humValue = dht.Humidity;
110+
if (dht.IsLastReadSuccessful)
111+
{
112+
Console.WriteLine($"Temperature: {tempValue.Celsius:0.#}\u00B0C");
113+
Console.WriteLine($"Relative humidity: {humValue:0.#}%");
114+
}
115+
```
116+
117+
**I have a Raspberry Pi 4 and I get an exception when creating the DHT sensor**
118+
119+
See this [issue 1145](https://github.com/dotnet/iot/issues/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:
120+
121+
```csharp
122+
GpioDriver driver = new RaspberryPi3Driver();
123+
var controller = new GpioController(PinNumberingScheme.Logical, driver);
124+
// This uses pin 4 in the logical schema so pin 7 in the physical schema
125+
var dht = new Dht11(4, gpioController: controller);
126+
```
127+
128+
**My DHT sensor using 1 wire protocol is not working on my Raspberry Pi with Windows 10 IoT Core, what can I do?**
129+
130+
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:_
131+
- https://github.com/ms-iot/samples/tree/develop/GpioOneWire
132+
- And on Hackster.io: https://www.hackster.io/porrey/go-native-c-with-the-dht22-a8e8eb
133+
134+
Now if your sensor is an I2C sensor, it should just work perfectly on Windows 10 IoT Core.
135+
136+
## References
137+
138+
* **DHT10** [datasheet (Currently only Chinese)](http://www.aosong.com/userfiles/files/media/DHT10%E8%A7%84%E6%A0%BC%E4%B9%A6.pdf)
139+
* **DHT11** [datasheet](https://cdn.datasheetspdf.com/pdf-down/D/H/T/DHT11-Aosong.pdf)
140+
* **DHT12** [datasheet](https://cdn.datasheetspdf.com/pdf-down/D/H/T/DHT12-Aosong.pdf)
141+
* **DHT21** [datasheet](https://cdn.datasheetspdf.com/pdf-down/A/M/2/AM2301-Aosong.pdf)
142+
* **DHT22** [datasheet](https://cdn-shop.adafruit.com/datasheets/DHT22.pdf)

devices/Dhtxx/category.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
thermometer
2+
hygrometer
11.9 KB
Binary file not shown.
201 KB
Loading

devices/Dhtxx/samples/dht22.fzz

12.8 KB
Binary file not shown.

devices/Dhtxx/samples/dht22.png

316 KB
Loading

devices/Dhtxx/samples/dht22ex.jpg

54.5 KB
Loading

0 commit comments

Comments
 (0)