
Temperature and Humidity Sensor

Using a temperature and humidity sensor with an Arduino Uno is a straightforward and versatile project suitable for beginners and hobbyists. These sensors can monitor environmental conditions and are widely used in weather stations, greenhouses, and smart home applications. The Arduino Uno reads data from the sensor and can display it on a serial monitor, an LCD, or even send it to the cloud for further analysis.
​
This guide focuses on using a popular temperature and humidity sensor using a library with the Arduino Uno. These sensors are inexpensive, easy to interface, and provide reasonably accurate measurements of temperature (°C/°F) and relative humidity (%). The data will be shown on a 16x2 LCD.
Materials
Temperature and Humidity Sensor

1) Arduino Uno (or any compatible Arduino board)
-
The microcontroller that will read data from the sensor and process it.
​​​
2) KY-015 Temperature and Humidity Sensor
-
A sensor module that measures temperature and relative humidity.​
-
DHT11: Lower accuracy and smaller range but cheaper. (same as store component)
-
DHT22: Higher accuracy and range but slightly more expensive.​​​
​​​
3) Jumper Wires
-
Used to connect the sensor to the arduino.
​​​
4) USB Cable (Type-A to Type-B)
-
To connect the Arduino to your computer for programming and to power the circuit.
​​
Basic Setup


1) Connect the Temperature and Humidity sensor:
-
Place the DHT sensor on the breadboard.
-
For KY-015 DHT11/DHT22 Pin Configuration:
-
Pin 1 (S) → Connect to a digital pin on the Arduino (e.g., Pin 2)..
-
Pin 2 (VCC) → Connect to the 5V pin on the Arduino
-
Pin 3 (GND) → Connect to the GND pin on the Arduino.​
-
​
2) Software Setup:
-
Open the Arduino IDE and install the Adafruit DHT library:
-
Go to Sketch → Include Library → Manage Libraries.
-
Search for "DHT" and install the Adafruit DHT sensor library.
-

CODE BREAK-DOWN
1. #include "DHT.h"
-
Includes the DHT library, which provides functions to interface with the DHT sensor.
​
2. #define DHTPIN 2
-
Defines the digital pin (Pin 2) on the Arduino to which the sensor's data pin is connected.
​
3. #define DHTTYPE DHT11
-
Specifies the type of DHT sensor being used, in this case, the DHT11. This could also be set to DHT22 if that sensor is used.
​
4. DHT dht(DHTPIN, DHTTYPE);
-
Creates an instance of the DHT object using the defined pin and sensor type.
​
5. void setup() {
-
Marks the beginning of the setup function, which runs once when the Arduino starts.
​
6. Serial.begin(9600);
-
Initializes serial communication at 9600 baud rate, allowing the Arduino to send data to a connected computer.
​
7. dht.begin();
-
Initializes the DHT sensor, preparing it to read data.
​
8. Serial.println("DHT sensor setup complete.");
-
Prints a confirmation message to the serial monitor to indicate that the setup is complete.
​
9. void loop() {
-
Marks the beginning of the loop function, which runs repeatedly after setup.
​
10. float humidity = dht.readHumidity();
-
Reads the humidity value from the DHT sensor and stores it as a floating-point number in the humidity variable.
​
11. float temperature = dht.readTemperature();
-
Reads the temperature value from the DHT sensor and stores it as a floating-point number in the temperature variable.
​
12. if (isnan(humidity) || isnan(temperature)) {
-
Checks if the humidity or temperature readings are invalid (NaN - "Not a Number").
​
13. Serial.println("Failed to read from DHT sensor!");
-
Prints an error message to the serial monitor if the sensor reading failed.
​
14. return;
-
Exits the current iteration of the loop if the readings are invalid.
​
15. Serial.print("Humidity: ");
-
Prints the label "Humidity:" to the serial monitor.
​
16. Serial.print(humidity);
-
Prints the value of the humidity variable to the serial monitor.
​
17. Serial.print(" %\t");
-
Prints a percentage symbol and a tab space after the humidity value for formatting.
​
18. Serial.print("Temperature: ");
-
Prints the label "Temperature:" to the serial monitor.
​
19. Serial.print(temperature);
-
Prints the value of the temperature variable to the serial monitor.
​
20. Serial.println(" *C");
-
Prints a degree Celsius symbol (*C) and moves to the next line in the serial monitor.
​
21. delay(2000);
-
Pauses the program for 2000 milliseconds (2 seconds) before the next iteration of the loop, to avoid excessive readings.