DHT11 /DHT22 Temperature Sensor


Hardware components:
386 00
DHT11 Temperature & Humidity Sensor
R8326274 01
Raspberry Pi 2 Model B
A000066 iso both
Arduino UNO & Genuino UNO
Male to Male Jumper Wire
12002 04
Breadboard (generic)
4.7K Ω Resistor
Cobbler Breakout Kit
Software apps and online services:
Microsoft Windows 10 IoT Core
Ide web
Arduino IDE
Raspberry Pi Raspbian OS
Microsoft Visual Studio 2015

DHT11 DHT22 Temperature Sensor



If you would like a different approach to connect your DHT11/22 to a Raspberry Pi using Windows 10 IoT Core, see my article “DHT Tiny Breakout for the Raspberry Pi“.


The DHT11 is a 4-pin (one pin is unused) temperature and humidity sensor capable of measuring 20% – 90% relative humidity and 0 to 50 °C. The sensor can operate between 3 and 5.5V DC and communicates using its own proprietary OneWire protocol. This protocol requires very precise timing in order to get the data from the sensor. The LOW and HIGH bits are coded on the wire by the length of time the signal is HIGH. The total time to take a reading is at most 23.4 ms. This includes an 18 ms delay required to start the data transfer and a window of up to 5.4 ms for the data. Individual signals can be as short as 20 μs and as long as 80 μs.

When Windows 10 IoT Core first became available I grabbed my Raspberry Pi 2 and my DHT11 sensor and tried it out in C#. I quickly found that it was not going to work. The issue with C# in the Windows 10 IoT Core is that it is just not going to be fast enough (at least not right now).

I posted to the Microsoft forum and had a few exchanges with other developers having the same struggle. I eventually came across a Microsoft response on the OneWire protocol in Windows 10 IoT Core that read

“Please keep in mind that the Windows 10 IoT Core OS is not a real-time OS in the same way that Windows CE was, so very low level timings and measurements might not always be possible. Also at the moment we don’t have a native implementation of Bitbanging / OneWire”.

After a while I received a response to my post that read

I’m given to understand that a more accurate timing facility will be available in future releases”.

Well, that will be a great and I am truly excited about the upcoming capability but who can wait? I recently saw that Microsoft posted a sample on how to support the a OneWire protocol using the DHT11 as the sample sensor and using C++ on Windows 10 IoT Core. I saw this as an opportunity to bring it to C#.

This project is about turning that sample into a library that can be used in C#. I am grateful for the person that wrote the library to help make this popular sensor useful in Windows 10 IoT Core, and hopefully, with this library it will be even more useful.


The library I created is a simple refactoring of the code originally posted by Microsoft so I take no credit for the work done to get the sensor reading.

The library presents a simple class called Dht11 in the namesapace Sensors.Dht. Creating a new object in C# is simple.

First open the GPIO pin you have the DHT11 sensor pin connected.

using Sensors.Dht;GpioPin pin = GpioController.GetDefault().OpenPin(4, GpioSharingMode.Exclusive);

Then pass this pin to the constructor of the Dht11 class and specify the GPIO Pin Drive Mode. This allows you to decide whether you will add your own pull-up resistor.

Dht11 dht11 = new Dht11(_pin, GpioPinDriveMode.Input);

To get a reading from the device use the GetReadingAsync method.

DhtReading reading = await dht11.GetReadingAsync().AsTask();

There is an overload that allows the maximum retry value to be specified. The default value is 20. This specifies how many attempts to make to read the sensor before giving up and returning a failed reading.

The DhtReading structure is defined as

public value struct DhtReading{  bool TimedOut;  bool IsValid;  double Temperature;  double Humidity;  int RetryCount;};

TimedOut (true if the attempting to take a reading timed out; false otherwise)IsValid (true if the reading checksum was correct; false otherwise)Temperature (the temperature reading in Celsius. The DHT11 only supports integer values) Humidity (the humidity reading in percent. The DHT11 only supports integer values)RetryCount (the number of attempts made to read the sensor)


Even though the sensor works in C++ it still does not get a reading every time. Thus, the need for a retry option in the class (this is actually part of the Microsoft sample). I thought it would be best to compare this to other platforms to see how it performs. I tried code on the Raspberry Pi 2 running Raspbian as well as on an Arduino Uno. The code I used for both has been included in the GitHub repository.

The video included in this project will demonstrate and compare the output from each of these platforms.

Getting Started

Assemble the Circuit for the Raspberry Pi 2

Use this guide to assemble the circuit while using the diagram located near the bottom of the page as a guide (note the color of the wires are optional and have been selected to help make the circuit easy to follow when it is constructed).

  • Place the T-shaped cobbler at the left end of the half size+ board (where the numbers start at 1). The two left most pins will be in E1 and F1 on the board. The two right most pins will be at E20 and F20
  • Place the 4.7K Ω resistor between A4 and 3V3
  • Connect an orange male to male jumper wire between B4 and F28
  • Connect a red male to male jumper wire between F29 and 3V3
  • Connect a black male to male jumper wire between F30 and GND
  • Place the DHT11 sensors into J30 (-), J29 (+) and J28 (s)
  • Plug the ribbon cable into the cobbler and the Raspberry Pi

DHT11 DHT22 Temperature Sensor

Assemble the Circuit for the Arduino

Use this guide to assemble the circuit while using the diagram located near the bottom of the page as a guide (note the color of the wires are optional and have been selected to help make the circuit easy to follow when it is constructed).

  • Place the 4.7K Ω resistor between E12 and E13
  • Connect a white male to male jumper wire between D13 and Pin 5 in the Arduino
  • Connect a red male to male jumper wire between D12 and the 5V pin in the Arduino
  • Connect a black male to male jumper wire between D11 and GND on the Arduino
  • Place the DHT11 sensors into A11 (-), A12 (+) and A13 (s)
  • Plug the USB cable from your computer to your Arduino

Below are photos of the circuits I built.

Read More: DHT11 /DHT22 Temperature Sensor

© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top