Reading Temperature and Humidity with DHT Sensor
Last updated
Last updated
DHT sensors are a type of digital temperature and humidity sensor that combine both sensing elements into a single device. They provide accurate and reliable measurements of temperature and humidity, making them popular choices for environmental monitoring applications.
Look for the number 7 connections on the Sensor Shield as marked on the following picture.
Sensor: | Sensor Shield: |
---|---|
VCC (+) | V |
DATA (S) (D) | S |
GND (-) | G |
Connect the positive (+) pin of the DHT sensor to the 5V pin on the Arduino.
Connect the negative (-) pin of the DHT sensor to the GND pin on the Arduino.
Connect the signal pin of the DHT sensor to digital pin D7 on the Arduino.
Open the Arduino IDE (Integrated Development Environment).
Click on "Sketch" in the menu bar and navigate to "Include Library" > "Manage Libraries...".
The Library Manager window will open. In the search bar, type "DHT" and press Enter.
Look for the "DHT sensor library" by Adafruit. Once found, click on the "Install" button to install the library.
After the installation is complete, you can close the Library Manager window.
To use the library in your sketch, you need to include it at the beginning of your code. Add the following line at the top of your sketch:
Open a new file on the Arduino IDE, copy this code and paste it in the new file.
The code starts by including the DHT.h
library, which provides functions to interact with the DHT sensor.
The DHT sensor pin and object are initialized. In this example, we are using DHT11, so we create a DHT
object with the specified pin and sensor type.
In the setup()
function, serial communication is initialized using Serial.begin()
, and the DHT sensor is started with dht.begin()
.
Within the loop()
function, temperature and humidity values are read from the DHT sensor using dht.readTemperature()
and dht.readHumidity()
respectively.
The temperature and humidity values are printed to the serial monitor using Serial.print()
and Serial.println()
.
A delay of 2 seconds (2000 milliseconds) is added before the next reading.
The DHT sensors, such as DHT11 and DHT22, utilize a digital communication protocol to provide accurate measurements of temperature and humidity. The DHT library simplifies the interaction with these sensors by providing functions to read temperature and humidity values.
In this project, we are using the DHT11 sensor, which is capable of measuring temperature with a resolution of 1°C and humidity with a resolution of 1%. The sensor communicates with the Arduino using a single wire protocol.
By reading temperature and humidity values from the DHT sensor, we can gather important environmental data that is crucial for monitoring and controlling the smart garden system.