Experiment:4
Name:
Reg no:
Measuring Temperature and Humidity using DHT11 Sensor
with Arduino Uno
Aim: To interface a DHT11 temperature and humidity sensor with an Arduino Uno board and
display the readings on the Serial Monitor.
Materials Required:
Arduino Uno board
DHT11 Temperature and Humidity Sensor
Jumper wires
USB cable
Procedure:
1. Wiring:
o Connect the VCC pin of the DHT11 sensor to the 5V pin on the Arduino Uno.
o Connect the GND pin of the DHT11 sensor to the GND pin on the Arduino Uno.
o Connect the data pin (usually the middle pin) of the DHT11 sensor to digital pin 8
on the Arduino Uno. (You can change this in the code if needed).
2. Software Setup:
o Open the Arduino IDE on your computer.
o If you haven't already, install the Adafruit DHT Sensor Library and the Adafruit
Unified Sensor Library. Go to Sketch -> Include Library -> Manage Libraries...
and search for them. Install both.
o Copy and paste the following code into the Arduino IDE:
C++
#include <Adafruit_Sensor.h> // Required for Adafruit DHT sensors
#include <DHT.h> // The actual DHT library
#define DHTPIN 8 // Digital pin connected to DHT sensor
#define DHTTYPE DHT11 // DHT 11 <- IMPORTANT: Define DHT11
DHT dht(DHTPIN, DHTTYPE); // Create DHT object
void setup() {
Serial.begin(9600);
Serial.println("DHT11 test!");
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true); // Read temperature as
Fahrenheit (optional)
if (isnan(h) || isnan(t) || isnan(f)) { // Check for errors!
Serial.println(F("Failed to read from DHT sensor!"));
return; // Exit the loop if there's an error
}
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% "));
Serial.print(F("Temperature: "));
Serial.print(t);
Serial.print(F(" *C "));
Serial.print(f);
Serial.println(F(" *F"));
delay(2000); // Wait 2 seconds (DHT11 needs time between
readings)
}
3. Upload Code:
o Connect your Arduino Uno to your computer using the USB cable.
o Select the correct board and port in the Arduino IDE (Tools -> Board and Tools -
> Port).
o Click the "Upload" button (the arrow icon) to upload the code to your Arduino.
4. View Results:
o Open the Serial Monitor (Tools -> Serial Monitor or Ctrl+Shift+M).
o Make sure the baud rate in the Serial Monitor is set to 9600.
o You should now see the temperature and humidity readings displayed in the Serial
Monitor.
Circuit:
Hardware component executed component
Result:
Inference:
The DHT11 sensor successfully measures the ambient temperature and relative humidity. The
readings are displayed on the Serial Monitor. Observe the temperature values in Celsius (°C) and
Fahrenheit (°F), as well as the humidity percentage. Note that the DHT11 has a specified
accuracy range, so the readings might not be perfectly precise. Also, the DHT11 has a slow
refresh rate, which is why we have a 2-second delay in the loop() function.