Bird
0
0
Arduinoprogramming~30 mins

Humidity and temperature (DHT11/DHT22) in Arduino - Mini Project: Build & Apply

Choose your learning style9 modes available
Humidity and temperature (DHT11/DHT22)
📖 Scenario: You want to measure the temperature and humidity in your room using a DHT11 or DHT22 sensor connected to an Arduino board. This sensor gives you two values: how warm the room is (temperature) and how much moisture is in the air (humidity).
🎯 Goal: Build a simple Arduino program that reads temperature and humidity from the DHT sensor and prints these values to the Serial Monitor.
📋 What You'll Learn
Use the DHT sensor library to read data from the sensor
Set up the sensor on a specific Arduino pin
Read temperature and humidity values from the sensor
Print the temperature and humidity values to the Serial Monitor
💡 Why This Matters
🌍 Real World
Measuring temperature and humidity is useful for weather stations, home automation, and plant care systems.
💼 Career
Understanding how to read sensor data and display it is a key skill for embedded systems and IoT device programming.
Progress0 / 4 steps
1
Set up the DHT sensor and include the library
Include the DHT.h library at the top of your sketch. Create a DHT object called dht using pin 2 and sensor type DHT11. Also, start the Serial communication at 9600 baud in the setup() function.
Arduino
Hint

Use #include <DHT.h> to include the library. Define DHTPIN as 2 and DHTTYPE as DHT11. Create the dht object with these. In setup(), start serial with Serial.begin(9600); and call dht.begin();.

2
Create variables to store humidity and temperature
Inside the loop() function, create two float variables called humidity and temperature. Use dht.readHumidity() to set humidity and dht.readTemperature() to set temperature.
Arduino
Hint

Use float humidity = dht.readHumidity(); and float temperature = dht.readTemperature(); inside loop().

3
Check if readings are valid and print them
Add an if statement to check if humidity or temperature is NaN using isnan(). If either is NaN, print "Failed to read from DHT sensor!" and return from loop(). Otherwise, print "Humidity: " followed by humidity and " %", then print "Temperature: " followed by temperature and " *C". Add a delay of 2000 milliseconds at the end.
Arduino
Hint

Use if (isnan(humidity) || isnan(temperature)) to check for errors. Print the error message and return; if true. Otherwise, print humidity and temperature with labels and units. Use delay(2000); to wait 2 seconds before next reading.

4
Print the final output to Serial Monitor
Make sure your program prints the humidity and temperature readings every 2 seconds to the Serial Monitor in this exact format (replace numbers with actual readings):
Humidity: 45.00 %
Temperature: 23.00 *C
If the sensor fails, it should print:
Failed to read from DHT sensor!
Arduino
Hint

Run your program and open the Serial Monitor at 9600 baud. You should see humidity and temperature printed every 2 seconds. If the sensor fails, the error message appears.