The DHT11 Sensor and Arduino

Today we are going to measure humidity and temperature using the DHT11 sensor attached to an Arduino board.  After uploading a sketch to the Arduino, it will be able to communicate with the sensor, request the data through the D3 pin and display it through the Serial Console.

Let's work on the hardware first. Grab your Arduino board, your DHT11 sensor, some colorful wires and follow along. For your first lesson, let's not worry about the color of the wires.  Any color will work and connect the components together. Our first step is to connect the sensor to the Arduino board using three wires:

  • Connect the S pin of the sensor (Signal) to the Arduino Pin Digital 3 (the 4th from the bottom on the lower right header).  This line is used as the communications between the Arduino and the sensor using the 1-wire protocol.
  • Connect the middle pin of the sensor (Power) to the Arduino 5V on the top left header (4th position).  This will power the sensor with a 5V source.
  • Connect the - pin of the sensor (the ground) to the Arduino GND pin on the top left header (3rd position).

The Software

Great!  Now, if this is your first time using the DHT11 sensor, you will need to download some libraries onto your computer.  Libraries are collections of small programs or drivers written by someone else and shared with the Arduino and Open-Source community.  In this case, the libraries you need will provide the code necessary for the Arduino to communicate with the sensor using the 1-wire protocol.

Open your Arduino IDE (the Arduino application - Install it if it is not already installed on your computer).  Open the Library Manager (click on Sketch / Include Library / Manage Libraries.  The following screen should appear.  Use this method every time you have a new sensor and device to search for existing code that will help you get started quickly.  It will not only install the libraries and drivers, but usually includes some examples that you can use to get going without having to write custom code.


Use the search bar to quickly find what you are looking for.  in this case, use the sensor name "DHT" to filter all the libraries that support the DHT11 sensor.  Install the Adafruit library for our lesson.  Click on Install. 

Note: Make sure to be connected to the Internet, so that the Arduino application can download the library and install it.


If this is your first time installing an Adafruit sensor library, you will need one extra file called Adafruit_Sensor.h. You can find this file on GitHub following this link:


Click on the Download button.  Then install the library on your computer by clicking on Sketch / Include Library / Add .ZIP library - then browse to the downloaded file in your Downloads directory and confirm.  This will install the file in the proper place in Arduino.


Upload the code to the Arduino

OK, we are now ready to open the example code, compile it and upload it to your Arduino board.  Open the example as shown below:


Once you click on the DHTtester menu option, it will load the file in your Arduino IDE.  You now need to make sure you have the correct sensor selected and the correct pin.  Otherwise, the communications between the Arduino and the sensor won't be working properly.


In the code, make the following changes:

  • Uncomment the line that has the DHT11 sensor (remove the //).
  • Comment out lines with the other DHTxx sensors (by adding //)
  • Make sure to specify Pin 3 in the code (#define DHTPIN 3) if set to another value.

Make sure your Arduino board is selected and ready to receive the code with the following two steps:

  • Click on Tools / Boards and make sure the Arduino Genuino/Uno is selected (or your board if you are using a different type).
  • Click on Tools / Port and select the port that shows the Arduino connected.

Compile and upload the code by clicking on the Upload arrow.  Make sure there are no errors and the sketch is uploaded successfully.


Now to view the values generated by the Arduino and sensor. To open the Serial Monitor, click on Tools / Serial Monitor.  You should now see the values captured by the sensor and sent to the Arduino board.

Get the Daybreak Protocol story in printed form