Needed Components with eBay Links
- Arduino Uno
- DHT11 Sensor
- 10kΩ Resistor (for pull-up)
- Breadboard
- Jumper Cables
- Micro USB Cable
Understanding the DHT11 Sensor
The DHT11 sensor measures both temperature and humidity and outputs these values digitally to the Arduino. It is simple to use but has limited precision, making it an ideal sensor for beginner-level experiments.
Key Functions
DHT.readTemperature()
: Reads temperature in degrees Celsius.DHT.readHumidity()
: Reads humidity as a percentage.
Wiring the Circuit
Connect the DHT11 sensor to your Arduino as follows:
- VCC pin of the DHT11 to 5V on the Arduino.
- GND pin of the DHT11 to GND on the Arduino.
- DATA pin of the DHT11 to digital pin 2 on the Arduino.
- Connect a 10kΩ pull-up resistor between the VCC and DATA pins of the DHT11 sensor.
Arduino Code to Read Data from DHT11
Ensure the DHT sensor library is installed before uploading this code. You can install it from the Library Manager in the Arduino IDE by searching for DHT sensor library.
// Include the DHT library
#include "DHT.h"
// Define pin and type for the sensor
#define DHTPIN 2 // Digital pin connected to the DHT11
#define DHTTYPE DHT11 // DHT 11 sensor type
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600); // Initialize the serial communication
dht.begin(); // Start the DHT sensor
}
void loop() {
delay(2000); // Wait a few seconds between measurements
// Reading temperature and humidity
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reading failed
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display results in Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}
Upload & Test
- Connect your Arduino to your computer using a USB cable.
- Open the Arduino IDE and copy the code above into the editor.
- Install the DHT library if you haven't already.
- Select your board and the correct port in the IDE.
- Upload the code by clicking the upload button.
- Open the Serial Monitor (Ctrl+Shift+M) to see the temperature and humidity readings.
Code Breakdown
The code begins by including the DHT library, which contains pre-written functions to simplify sensor usage. In the setup()
function, we initialize the sensor and Serial Monitor. The loop()
function continuously reads the humidity and temperature, then prints them to the Serial Monitor.
Conclusion
This experiment demonstrates how to interface with a DHT11 sensor using Arduino. It provides basic weather station-like functionality, allowing you to monitor the surrounding environment. You can further expand this project by adding an LCD display or integrating the data into an IoT project.