dht11 connected to arduio

How to use DHT11 Temperature and Humidity Sensor with an arduino

What is DHT11 temperature and humidity sensor

The DHT11 temperature and humidity sensor is a compact temperature and humidity sensor that conveniently packages a small device with 4 pins and uses a 1 wire serial interface for interfacing to devices such as microcontrollers.

About the DHT11 sensor

The DHT11 contains a temperature and humidity sensor in a single module that supplies a digital output when prompted by an external device like a microcontroller. The communications interface uses a proprietary 1-wired approach that outputs 40 bits of data when prompted by the controlling device.


There are 4 pins on the device but only 3 are used – one for the supply voltage, one for the ground and one for the data. It’s temperature range is 0-50°C (+/- 2°C), humidity range is 20-80% (+/- 5%) and has a supply voltage of  3-5V DC. The DHT22 is the next model up with greater temperature and humidity range and accuracy specifications.



How to Setup DHT11 Module with Arduino board

Before we get started, we require a few components:

  • Computer
  • Arduino (or Arduino compatible board)
  • USB cable
  • DHT11 temperature and humidity sensor module
  • Arduino IDE
  • DHT11 library

First of we need to identify the pins on the DHT11 module.

dht11 sensor pinout

Next we connect the supply pin of the module to +5V, the ground pin to GND socket of the microcontroller board and data Pin to pin (socket) 2 on the microcontroller board also.

Now that the DHT11 is connected to the microcontroller board we need to connect the board to the computer with the USB cable.

Next we open up the IDE and install the “DHT sensor library”.

Then we open the code example that came with the library and change the following bits of code:

1 – Set devicetype to DHT11

2 – set the data pin to pin 2


Now upload the code then open the “serial monitor” in the IDE. If all is working correctly we should see the temperature and humidity readings logging every 2 seconds on the “serial monitor”. The program runs as an endless loop so the temperature readouts will continue every second until power is removed from the microcontroller board.

The following code is our code we wrote up to simplify testing the the DHT11 on the arduio platform:


#include “DHT.h”
#define DHTPIN 2     // do not connect to pin 0 or pin 1
#define DHTTYPE DHT11   // Define DHT11 module

void setup() {
Serial.println(“DHT11 Temperature and Humidity Sensor is connected \n\n” );

void loop() {
delay(2000);  // 2 second delay between readings
float h = dht.readHumidity();
float t = dht.readTemperature();  // Read temperature as degrees Celsius)
float f = dht.readTemperature(true); // Read temperature as degrees Fahrenheit (set to “true”)
Serial.print(“Humidity: “);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(” C, “);
Serial.print(” F \n”);


We hope this post was useful to you, if you liked it please share it with your friends !


Before you move on, I would love for you to suggest some topics you would like to see covered on the Smartmicrocontroller.com website this year by leaving a comment in the comments section in the youtube channel, or send us a message through one of the social media links on the about page.