Introduction
The DHT11 sensor is used to measure temperature and humidity. This experiment demonstrates how to interface the DHT11 with an Arduino to display readings on the Serial Monitor.
Components Needed
- Arduino (e.g., Uno, Nano)
- DHT11 Temperature and Humidity Sensor
- 10kΩ resistor
- Breadboard and jumper wires
- USB cable for programming
Circuit Setup
- Connect the VCC pin of the DHT11 to the 5V pin on the Arduino.
- Connect the GND pin of the DHT11 to the GND of the Arduino.
- Connect the DATA pin of the DHT11 to digital pin 2 on the Arduino.
- Place a 10kΩ pull-up resistor between the DATA pin and VCC.
Ensure all connections are secure and the DHT11 is oriented correctly on the breadboard.
Code for DHT11
Upload the following code to your Arduino to read data from the DHT11 sensor:
// Include the necessary library
#include
// Define the DHT11 sensor type and pin
#define DHTTYPE DHT11
#define DHTPIN 2
// Initialize the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHT11 Temperature and Humidity Sensor Experiment");
dht.begin();
}
void loop() {
// Read temperature and humidity values
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if readings are valid
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the results 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 2 seconds before taking the next reading
}
Explanation
The code initializes the DHT11 sensor and reads temperature and humidity data every 2 seconds. The readings are then displayed on the Serial Monitor:
dht.readHumidity()
returns the current humidity as a percentage.dht.readTemperature()
returns the temperature in degrees Celsius.- The
isnan
check ensures valid readings before displaying the data.
Troubleshooting
- If you see "Failed to read from DHT sensor!" in the Serial Monitor, check your connections and ensure the sensor is correctly powered.
- Ensure you have the DHT library installed in your Arduino IDE. You can install it via Tools > Manage Libraries.
- Check that the pull-up resistor is correctly placed between the DATA pin and VCC.
- If the readings are inaccurate, ensure the sensor is placed in a stable environment without sudden temperature or humidity changes.