Note what follows below is a bit out of date- the latest version on Github is a 2 wire operation requiring pin4 as input and pin 5 as output. Read the link above to GITHUB Code and the readme https://github.com/ms-iot/samples/blob/develop/GpioOneWire/README.md and look at the NEW Schematic schematic.png which requires an extra MOSFET
This sample shows how to read from the DHT11 from a Universal Windows Application. The DHT11 is a low cost temperature and humidity sensor that uses a single wire to interface to the host controller. This wire is used by the host to request a sample from the DHT11 and by the DHT11 to transmit data back to the host.
The DHT11 is right on the edge performance-wise of what the GPIO APIs can handle. If there is background activity such as network, USB, filesystem, or graphics activity, it can prevent the sample from successfully sampling from the DHT11.
|Minimum supported build||10.0.10556|
|Supported Hardware||Raspberry Pi 2 or 3
You will need the following hardware to run this demo:
Connect the components as shown in the following diagram:
Universal (Unencrypted Protocol)
F5to build, deploy, and debug the project. You should see temperature and humidity samples updated on the screen every 2 seconds.
The logic that interacts with the DHT11 is contained in the Dht11::Sample() method. Since the 1s and 0s that the DHT11 sends back are encoded as pulse widths, we need a way to precisely measure the time difference between falling edges. We use QueryPerformanceCounter() for this purpose. The units of QueryPerformanceCounter are platform-dependent, so we must call QueryPerformanceFrequency() to determine the resolution of the counter.
A difference of 76 microseconds between falling edges denotes a ‘0’, while a difference of 120 microseconds between falling edges denotes a ‘1’. We choose 110 microseconds as a reasonable threshold above which we will consider bits to be 1s, while we will consider pulses shorter than this threshold to be 0s. We convert 110 microseconds to QueryPerformanceCounter (QPC) units to be used later.
Next, we send the sequence required to activate the sensor. The GPIO signal is normally pulled high while the device is idle, and we must pull it low for 18 milliseconds to request a sample. We latch a low value to the pin and set it as an output, driving the GPIO pin low.
We then revert the pin to an input which causes it to go high, and wait for the DHT11 to pull the pin low, then high again.
After receiving the first rising edge, we catch all of the falling edges and measure the time difference between them to determine whether the bit is a 0 or 1.
After all bits have been received, we validate the checksum to make sure the
received data is valid. The data is returned through the