Arduino Code and Video for DHT11 Temperature and Humidity Sensor
In this tutorial, we will explore how to use the DHT11 temperature and humidity sensor with an Arduino. This sensor is a low-cost option for measuring both temperature and humidity, making it a perfect choice for various projects. We will cover the wiring, the necessary code, and how the components interact to provide accurate readings.

By the end of this tutorial, you will be able to read temperature in Celsius, Fahrenheit, and Kelvin, as well as humidity levels from the DHT11 sensor. For additional clarity, be sure to check out the video at the indicated timestamps (in video at 00:00).
Hardware Explained
The DHT11 sensor consists of three pins: ground, power, and data. The ground pin connects to the Arduino's ground, while the power pin connects to a 5V supply from the Arduino. The data pin is responsible for the communication between the Arduino and the sensor, allowing the Arduino to read temperature and humidity values.
This sensor employs a digital output, which means it sends data in a serial format to the Arduino. It contains a thermistor for temperature measurement and a capacitive humidity sensor. The DHT11 converts the analog signals it receives into digital signals, making it easier for microcontrollers like Arduino to process the data.
Datasheet Details
| Manufacturer | ASAIR |
|---|---|
| Part number | DHT11 |
| Logic/IO voltage | 3.3–5.5 V |
| Supply voltage | 3.3–5.5 V |
| Output current (per channel) | 0.5 mA |
| Peak current (per channel) | 2.5 mA |
| PWM frequency guidance | N/A |
| Input logic thresholds | 0.3*VDD to 0.7*VDD |
| Voltage drop / RDS(on) / saturation | N/A |
| Thermal limits | 0 to 60 °C |
| Package | 4-pin DIP |
| Notes / variants | Low-cost, easy to use |
- Operating voltage between 3.3V to 5.5V.
- Maximum output current of 2.5 mA.
- Temperature range from 0 to 60 degrees Celsius.
- Humidity range from 20% to 90%.
- Use pull-up resistor on the data pin for stable readings.
- Ensure proper connection to avoid floating inputs.
- Be cautious of reversed polarity when connecting power.
- Delay readings by at least 1 second for accurate results.
Wiring Instructions

To wire the DHT11 sensor to your Arduino, follow these steps:
First, connect the ground pin (usually black or brown) of the DHT11 to the ground (GND) on the Arduino. Next, connect the power pin (usually red) of the DHT11 to the 5V output on the Arduino. Finally, connect the data pin (usually yellow or white) to digital pin 2 on the Arduino. Make sure to use a pull-up resistor (around 10k ohms) between the data pin and the power pin to ensure stable communication.
Code Examples & Walkthrough
In the Arduino code, we start by including the necessary header file for the DHT11 sensor:
#include
This line imports the library that contains the functions needed to interact with the DHT11 sensor. Next, we create an instance of the DHT11 class:
dht11 DHT11; // create object of DHT11
This line initializes the DHT11 object which we will use to read data from the sensor. The pin connected to the DHT11 is defined as:
#define dhtpin 2 // set the pin to connect to DHT11
This sets the data pin for the sensor to pin 2 on the Arduino. In the loop() function, we read the sensor data:
DHT11.read(dhtpin);// initialize the reading
This line starts the reading process, and the humidity can be accessed with:
int humidity = DHT11.humidity;// get humidity
The humidity value is stored in the variable humidity, which is then printed to the serial monitor along with temperature values converted to Celsius, Fahrenheit, and Kelvin.
Demonstration / What to Expect
When you run the code, you should see temperature and humidity readings displayed in the serial monitor. The temperature readings will update every half second (in video at 00:00). If the sensor is heated or cooled, you will notice the values change accordingly. Be cautious, as the DHT11 has a maximum temperature limit of 60 degrees Celsius; exceeding this may lead to inaccurate readings.
Video Timestamps
- 00:00 - Introduction to DHT11 sensor
- 01:30 - Wiring instructions
- 02:15 - Overview of the code
- 03:00 - Running the code and expected output
Resources & references
-
ExternalDHT11 library on GitHubgithub.com
Files📁
Arduino Libraries (zip)
-
DHT22 Arduino Library
robojax-DHT22_library.zip0.01 MB
Fritzing File
-
Temperature Sensor DHT11
application/zip0.01 MB
User’s Manual
-
DHT11 User's Manual
robojax-DHT11_manual.pdf0.82 MB