Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
đ Ready to explore and create with us? Click [here] and join today!
6.2 Measuring Temperature and Humidity with DHT11ï
In this lesson, weâll learn how to use a DHT11 temperature and humidity sensor with the Raspberry Pi Pico 2. The DHT11 is a basic, low-cost digital sensor that can measure ambient temperature and humidity, providing a calibrated digital output.
What Youâll Need
In this project, we need the following components.
Itâs definitely convenient to buy a whole kit, hereâs the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Newton Lab Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT |
QUANTITY |
LINK |
---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
1 |
Understanding the DHT11 Sensor
The DHT11 sensor uses a capacitive humidity sensor and a thermistor to measure the surrounding air. It outputs a digital signal on the data pin, and itâs fairly simple to use, but requires precise timing to read data.
Temperature Range: 0â50 °C with ±2 °C accuracy
Humidity Range: 20â80% RH with ±5% accuracy
Sampling Rate: 1 Hz (once every second)
Circuit Diagram
Wiring Diagram
Writing the Code
Weâll write a program that reads temperature and humidity data from the DHT11 sensor and prints the values to the Serial Monitor.
Note
You can open the file
6.2_dht11.ino
fromnewton-lab-kit/arduino/6.2_dht11
.Or copy this code into Arduino IDE.
Select the Raspberry Pi Pico 2 board and the correct port, then click âUploadâ.
The
DHT sensor library
library is used here, you can install it from the Library Manager.
#include <DHT.h>
// Define the connection pins
#define DHTPIN 16 // GPIO 16 -> Data pin of DHT11
#define DHTTYPE DHT11 // Define the sensor type
// Create a DHT object
DHT dht(DHTPIN, DHTTYPE);
unsigned long previousMillis = 0; // Stores the last time the display was updated
const long interval = 2000; // Interval at which to read sensor (milliseconds)
void setup() {
// Initialize serial communication at 115200 baud
Serial.begin(115200);
Serial.println(F("DHT11 Sensor Test!"));
// Initialize the DHT sensor
dht.begin();
}
void loop() {
unsigned long currentMillis = millis();
// Update the sensor reading every 'interval' milliseconds
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Read humidity and temperature
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature();
float temperatureF = dht.readTemperature(true);
// Check if any reads failed
if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Calculate heat index
float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
float heatIndexF = dht.computeHeatIndex(temperatureF, humidity);
// Print the results to the Serial Monitor
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F("% Temperature: "));
Serial.print(temperatureC);
Serial.print(F("°C "));
Serial.print(temperatureF);
Serial.print(F("°F Heat index: "));
Serial.print(heatIndexC);
Serial.print(F("°C "));
Serial.print(heatIndexF);
Serial.println(F("°F"));
}
}
After uploading the code, the Serial Monitor should display the temperature and humidity readings every two seconds.
DHT11 Sensor Test!
Humidity: 45.00% Temperature: 25.00°C 77.00°F Heat index: 25.00°C 77.00°F
Humidity: 46.00% Temperature: 25.50°C 78.00°F Heat index: 25.50°C 78.00°F
Humidity: 47.00% Temperature: 26.00°C 79.00°F Heat index: 26.00°C 79.00°F
Humidity: Expose the sensor to different humidity levels to see changes in readings.
Temperature: Change the temperature around the sensor to observe temperature measurements.
Understanding the Code
Including Libraries and Defining Constants:
DHT.h
: Includes the DHT sensor library to simplify interactions with the sensor.DHTPIN
: Specifies the GPIO pin connected to the DHT11 data pin.DHTTYPE
: Defines the type of DHT sensor being used (DHT11 in this case).
#include <DHT.h> #define DHTPIN 16 // GPIO 16 -> Data pin of DHT11 #define DHTTYPE DHT11 // Define the sensor type
Creating the
DHT
Object:Initializes a
DHT
object with the specified data pin and sensor type.DHT dht(DHTPIN, DHTTYPE);
Setup Function:
Serial Communication: Starts serial communication for debugging and data display.
DHT Sensor Initialization: Prepares the DHT11 sensor for data reading.
void setup() { // Initialize serial communication at 115200 baud Serial.begin(115200); Serial.println(F("DHT11 Sensor Test!")); // Initialize the DHT sensor dht.begin(); }
Loop Function:
Timing with
millis()
:Uses non-blocking timing to read the sensor every 2 seconds (interval = 2000 milliseconds).
if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; ... }
Reading Sensor Data:
dht.readHumidity()
: Reads the current humidity.dht.readTemperature()
: Reads the current temperature in Celsius.dht.readTemperature(true)
: Reads the current temperature in Fahrenheit.
Error Handling:
Checks if any of the readings failed (returned NaN) and prints an error message if so.
if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) { Serial.println(F("Failed to read from DHT sensor!")); return; }
Calculating Heat Index:
dht.computeHeatIndex(temperatureC, humidity, false)
: Calculates the heat index in Celsius.dht.computeHeatIndex(temperatureF, humidity)
: Calculates the heat index in Fahrenheit.
Displaying Data:
Prints humidity, temperature in Celsius and Fahrenheit, and heat index to the Serial Monitor.
Serial.print(F("Humidity: ")); Serial.print(humidity); Serial.print(F("% Temperature: ")); Serial.print(temperatureC); Serial.print(F("°C ")); Serial.print(temperatureF); Serial.print(F("°F Heat index: ")); Serial.print(heatIndexC); Serial.print(F("°C ")); Serial.print(heatIndexF); Serial.println(F("°F"));
Troubleshooting
No Readings Displayed:
Check all wiring connections.
Ensure the DHT11 sensor is receiving power.
Verify that the correct GPIO pins are defined in the code.
Incorrect Readings:
Verify that the DHT11 sensor is not damaged.
Check the sensorâs datasheet for proper timing and signal requirements.
Sensor Interference:
Avoid placing the sensor near other electronic devices that might cause interference.
Ensure there are no obstacles blocking the sensorâs line of sight.
Further Exploration
Integrating with Displays:
Connect an LCD or OLED display to show temperature and humidity readings without using the Serial Monitor.
Creating Alerts:
Implement buzzer or notification systems that trigger when temperature or humidity exceeds certain thresholds.
Combining with Other Sensors:
Pair the DHT11 with motion sensors, light sensors, or other environmental sensors to create comprehensive monitoring systems.
Building a Weather Station:
Expand the project by adding additional sensors like barometric pressure sensors, rain gauges, and wind speed sensors to build a full-fledged weather station.
Conclusion
In this lesson, youâve learned how to use a DHT11 temperature and humidity sensor with the Raspberry Pi Pico to measure and display ambient temperature and humidity levels. By leveraging the DHT library, you can easily integrate environmental sensing into your projects. The optional LED indicator provides a simple way to add visual feedback based on sensor readings, enhancing the interactivity of your system.