Temperature and Humidity Sensor
In this project, you'll learn how to connect and control the DHT11 temperature and humidity sensor with the Arduino.
| Parts needed:
- Arduino
- Temperature and humidity sensor DHT11
- Wires
- Breadboard
- You also need to download the DHT library which contains all of the functions for interfacing with the DHT11 temperature sensor.
|
|
1
| Making the connections
The DHT11 has either three connections: +, Out and –
or four connections: + (pin 1), Out (pin 2), not used (pin 3), and – (pin 4)
- Connect the + (pin 1) to 5V on the Arduino.
- Connect the Out (pin 2) to pin 2 on the Arduino.
- Connect the – (pin 4) to GND on the Arduino.
|
2
| Download the DHT library.
|
3
| Install the DHT library by selecting Sketch from the Arduino IDE menu
-
then select Import Library or Include Library
-
then select Add Library or Add .ZIP Library
Locate and select the DHT.zip file that you just downloaded in step 2. It should be in the Download folder.
Click the Open button
(Refer to this document for more information on how to install a library if you run into problems.)
|
4
| Create a new program and copy the program on the right.
The
#include <DHT.h>
line tells the computer to include the DHT.h temperature library code.
This library contains the low-level code for communicating with the temperature sensor.
The DHT command from the DHT.h library declares a variable called dht
for working with the temperature device.
|
#include <DHT.h> // include the temperature library
int DHTpin = 2; // input pin
DHT dht(DHTpin, DHT11); // declare a variable for the temperature device
void setup() {
Serial.begin(9600);
Serial.println("Current Temperature");
dht.begin(); // initialize the temperature device
}
void loop() {
delay(2000);
float celsius = dht.readTemperature(false); // get temperature in Celsius
float fahrenheit = dht.readTemperature(true); // get temperature in Fahrenheit
float humidity = dht.readHumidity(); // get humidity
// Check for read error
if (isnan(humidity) || isnan(celsius) || isnan(fahrenheit)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.print("°C ");
Serial.print(fahrenheit);
Serial.print("°F\t");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("%\t");
Serial.println();
}
|
3
| Upload the program, then open up the serial monitor. You should see the current temperature and humidity displayed in the monitor.
|
|