Here’s the dht11 temperature and humidity sensor arduino code, you’ll need to use a library that simplifies communication with the sensor.
Below is a simple Arduino sketch to read data from a dht11 sensor and display the temperature and humidity on the Serial Monitor.
Components Needed for dht11 temperature and humidity sensor arduino code :
- Arduino (e.g., Uno)
- dht11 sensor
- Jumper wires
- Breadboard (optional)
- Resistor 4.7kΩ – 10kΩ (optional if using just the dht11 chip)
Aliexpress
Item | Image | Cost ($USD) |
dht11 sensor | ![]() | $1.25 |
arduino uno | ![]() | $3.27 |
jumper wires | ![]() | $1.87 |
breadboard | ![]() | $1.53 |
resistors | ![]() | $1.11 (50 pieces) |
dht11 arduino Wiring and diagram:
- DHT11 Pin 1 (Data) to Digital Pin 2 on Arduino (or any other digital pin)
- DHT11 Pin 2 (VCC) to 5V on Arduino
- DHT11 Pin 3 (GND) to GND on Arduino
![](https://www.slyautomation.com/wp-content/uploads/2024/08/image-1.png)
![](https://www.slyautomation.com/wp-content/uploads/2024/08/20240813_153717-1024x768.jpg)
- Optional if using just using the dht11 chip; Using a breadboard, place a resistor between VCC and Data pin (4.7kΩ or 10kΩ)
![](https://www.slyautomation.com/wp-content/uploads/2024/08/image-2.png)
dht11 temperature and humidity sensor arduino code
First, you’ll need to install the DHT
library. You can do this by going to Sketch > Include Library > Manage Libraries…, searching for “DHT sensor library” by Adafruit, and installing it.
![](https://www.slyautomation.com/wp-content/uploads/2024/08/image-3.png)
// dht11 temperature and humidity sensor arduino code
#include "DHT.h"
#define DHTPIN 2 // Pin where the data pin of DHT11 is connected
#define DHTTYPE DHT11 // Define DHT type as DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // For Fahrenheit use dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print temperature and humidity values to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
delay(2000); // Wait a few seconds between measurements.
}
1. Reading Humidity and Temperature from dht11 temperature and humidity sensor arduino code
float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // For Fahrenheit use dht.readTemperature(true);
- Purpose: The first thing the loop does is read the humidity and temperature values from the DHT11 sensor.
dht.readHumidity()
: This function reads the humidity from the sensor and returns it as a floating-point number (e.g.,50.5
for 50.5% humidity).dht.readTemperature()
: This function reads the temperature from the sensor and returns it as a floating-point number in Celsius (e.g.,23.7
for 23.7°C). If you want the temperature in Fahrenheit, you would passtrue
to this function like this:dht.readTemperature(true)
.
2. Checking for Errors in the dht11 temperature and humidity sensor arduino code
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
- Purpose: After attempting to read the sensor, the code checks if the readings were successful.
isnan()
Function: This function checks if the value returned from the sensor is “NaN” (Not a Number). If the sensor fails to provide a valid reading (due to a wiring issue or sensor malfunction), it might returnNaN
.- Condition Check: The code checks if either the
humidity
ortemperature
variable containsNaN
. - If there’s an error: If any of the readings failed, a message is printed to the Serial Monitor saying “Failed to read from DHT sensor!”, and the loop ends early using
return;
. This prevents the code from trying to print invalid data.
3. Displaying the dht11 Results
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
- Purpose: This section is responsible for displaying the temperature and humidity readings on the Serial Monitor.
Serial.print()
andSerial.println()
Functions:Serial.print("Humidity: ")
: Prints the string “Humidity: ” to the Serial Monitor.Serial.print(humidity)
: Prints the value of thehumidity
variable to the Serial Monitor.Serial.print(" %\t")
: Prints the percentage symbol%
followed by a tab (\t
) for spacing.Serial.print("Temperature: ")
: Prints the string “Temperature: ” to the Serial Monitor.Serial.print(temperature)
: Prints the value of thetemperature
variable to the Serial Monitor.Serial.println(" *C")
: Prints the string ” *C” (for Celsius) to the Serial Monitor and moves the cursor to a new line.
4. Delay Between Readings
delay(2000); // Wait a few seconds between measurements.
- Purpose: This line pauses the program for 2000 milliseconds (2 seconds) before the loop starts again.
- Why 2 Seconds?: The DHT11 sensor has a rate limit on how often it can provide readings, so adding a delay ensures that the sensor has enough time to stabilize before the next reading.
- and that the end of the dht11 temperature and humidity sensor arduino code!
dht sensor Summary:
- Read Data: The loop reads the humidity and temperature from the DHT11 sensor.
- Check for Errors: It checks if the readings are valid.
- Display Data: If the readings are valid, it prints the humidity and temperature values to the Serial Monitor.
- Wait: The program pauses for 2 seconds before taking the next reading.
- All elements combined this makes the dht11 temperature and humidity sensor arduino code work!
This loop repeats indefinitely, providing continuous temperature and humidity readings.
- The sketch initializes the DHT11 sensor and reads the temperature and humidity values every 2 seconds.
- The data is displayed in the Serial Monitor, showing the humidity in percentage and the temperature in Celsius.
- For more techincal spec and feature information on the dht11
Usage:
- Upload this sketch of the dht11 temperature and humidity sensor arduino code to your Arduino by ensuring the arduino is selected if it isn’t, use the drop down in the top left of the ide app, then select the option ‘Select other board and port’ scroll down to arduino uno and the port number your arduino is connected to.
![](https://www.slyautomation.com/wp-content/uploads/2024/08/image-5.png)
![](https://www.slyautomation.com/wp-content/uploads/2024/08/image-4.png)
Then press upload
![](https://www.slyautomation.com/wp-content/uploads/2024/08/image-6.png)
- Open the Serial Monitor (
Ctrl + Shift + M
) to view the temperature and humidity readings.
Datasheet for dht11 temperature and humidity sensor arduino code
DHT11 Features & Specs | ||
---|---|---|
No. | Parameter | Value |
1 | Measures | Humidity & Temperature |
2 | Sensors Included | Capacitive Humidity Sensor & Thermistor |
3 | Humidity Range | 20% to 80% with ±5% accuracy |
4 | Temperature Range | 0°C to 50°C with ±2°C accuracy |
5 | Package | 4 Pins in a single row |
6 | Operating Voltage | 3.0V to 5.5V |
7 | Operating Current | 0.3mA(measuring), 60uA(idle) |
8 | Resolution | 1°C, 1%RH (8-Bit) |
9 | Response Time | 6s-15s |
10 | Repeatability | ±1°C, ±1%RH |
11 | Sampling Frequency | 1Hz |
12 | Dimensions | 27mm x 59mm x 13.5mm (1.05″ x 2.32″ x 0.53″) |