Introduction: How to Display Temperature and Humidity Using an Arduino and a DHT11 Humidity Sensor
I made few instructables showing how to read temperature using a temperature sensor.
Now I have discovered the DHT11 which does the same job plus displays humidity level as well. Also reading temperature from a DHT11 is much easier, all the hard work is done by the library.
Step 1: Here Is What You Need for This Project:
- An Arduino, I used an Arduino Nano compatible.
- A DHT11 Humidity sensor
- Three Male to Male jumper cables
- A solderless breadboard
Step 2: How to Connect the Arduino and the DHT11
Connect the - end of the DHT11 to the ground on the Arduino
Connect the + end of the DHT11 to the 5v on the Arduino
Connect the S end of the DHT11 to PIN D5 on the Arduino
The schematic diagram is done using Fritzing
Step 3: The Code
The code and the library are attached in this step.
Start by copying the library to the "libraries" folder on your computer then restart the Arduino IDE if it was open.
I loaded the example code from Arduino IDE then simplified it.
THE CODE
Start by loading the library:
#include <dht.h>
Then define the variables:
#define dht_apin 5 Assigns the DHT11 data pin to pin D5 on the Arduino
dht DHT; This is needed by the DHT11
In the void setup start the serial monitor and print few lines, including the library version for no good reason other than to show you that this command exists.
void setup() {
Serial.begin(9600); //Strat the serial monitor
delay (500); //a small delay
Serial.println("Using a DHT11 humidity and temperature sensor");
Serial.print("THE LIBRARY VERSION IS: ");
Serial.println(DHT_LIB_VERSION); //Prints the library version on the serial monitor
Serial.println(); //Empty line
delay (1000); //a small delay }
In the Void loop we print the temperature and the humidity values every 1 second. When using an LM35 temperature sensor, you read the data from the sensor and convert it to degrees C. However, with the DHT11, the library does the hard work. All you have to do is display the temperature in degrees C using the command DHT.read11(variable name);
void loop() {
DHT.read11(dht_apin); //read data from pin 5 (DHT11)
Serial.print("Current Humidity = "); //Prints information within qoutation
Serial.print(DHT.humidity); //Prints the Humidity read from the DHT11 on PIN 5
Serial.print("% ");
Serial.print("Temperature = ");
Serial.print(DHT.temperature); //Prints the temperature read from the DHT11 on PIN 5
Serial.println("C ");
delay (1000); //a small delay
}
That's it, verify the code and upload it.
See the picture for the serial monitor example.
Hope you enjoyed it.