How to measure temperature and humidity with Arduino

Photo of author

By Jackson Taylor

Measuring temperature and humidity is essential in a variety of applications, from home automation systems to environmental monitoring. With the power of Arduino, you can easily build a project to measure these parameters and display the results on a screen or store them for later use. In this guide, we’ll explore how to measure temperature and humidity with Arduino, using sensors like the DHT11 and DHT22.

What You’ll Need to Measure Temperature and Humidity with Arduino

Before we dive into the coding and wiring, let’s look at the essential components required to set up your Arduino project.

Required Components for the Project

To build this project, you will need the following components:
  • Arduino board (Uno, Nano, etc.): This is the microcontroller that will control your project.
  • DHT11 or DHT22 sensor: These are temperature and humidity sensors that will collect data.
  • Jumper wires: For connecting the sensor to the Arduino board.
  • Breadboard: For prototyping your project.
  • LCD display (optional): To display the temperature and humidity readings.
  • Resistor (optional): A 10k ohm pull-up resistor for the DHT sensors.
  • Arduino IDE: Software to write and upload your code to the Arduino board.

Wiring the DHT Sensor to Arduino

Now that you have all the required components, let’s wire the DHT sensor to the Arduino board.
  1. Connect the DHT sensor’s VCC pin to the 5V pin on the Arduino.
  2. Connect the sensor’s GND pin to the GND pin on the Arduino.
  3. Connect the sensor’s data pin to a digital pin on the Arduino (e.g., pin 7).
  4. If using the DHT11 or DHT22 sensor, you’ll need a 10k pull-up resistor between the VCC pin and the data pin.
The wiring setup is fairly simple, so now you’re ready to upload your code and get started with measuring the temperature and humidity!

Setting Up the Arduino IDE

Before you can upload your code to the Arduino board, you need to set up your Arduino IDE.
  1. Install the DHT sensor library: Open the Arduino IDE, go to Sketch > Include Library > Manage Libraries. Search for “DHT sensor” and install the DHT sensor library by Adafruit.
  2. Set up the board: Go to Tools > Board and select your Arduino model (e.g., Arduino Uno).
  3. Select the correct port: Go to Tools > Port and choose the correct COM port for your Arduino.
See also
How to read a temperature sensor with Arduino
Once the setup is complete, you’re ready to start writing the code for your temperature and humidity project.

Writing the Code for Temperature and Humidity Measurement

Here’s a simple sketch that will read data from the DHT sensor and print the temperature and humidity values to the Serial Monitor.
#include <DHT.h>

#define DHTPIN 7     // Pin connected to the DHT sensor
#define DHTTYPE DHT11  // Specify the DHT sensor type (DHT11 or DHT22)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
 Serial.begin(9600); // Start the Serial Monitor at 9600 baud
 dht.begin();     // Initialize the DHT sensor
}

void loop() {
 // Wait a few seconds between readings
 delay(2000);

 // Read the humidity and temperature
 float h = dht.readHumidity();
 float t = dht.readTemperature();

 // Check if any reading failed and exit early
 if (isnan(h) || isnan(t)) {
  Serial.println("Failed to read from DHT sensor!");
  return;
 }

 // Print the results to the Serial Monitor
 Serial.print("Humidity: ");
 Serial.print(h);
 Serial.print("% Temperature: ");
 Serial.print(t);
 Serial.println("°C");
}

Explanation of the Code:

  • Include the DHT library: This is required to communicate with the DHT sensor.
  • Define the sensor’s pin: In this case, we’ve connected the sensor to pin 7.
  • Initialize the sensor: dht.begin() initializes the sensor for use.
  • Read the sensor values: We use dht.readHumidity() to get the humidity and dht.readTemperature() to get the temperature.
  • Check for errors: The code checks if the readings are valid. If not, it outputs an error message.
Once you’ve written the code, click Upload to send it to your Arduino board.

Displaying Temperature and Humidity on an LCD

If you want to display the readings on an LCD screen, you can connect a 16×2 LCD to your Arduino. Here’s how you would modify the code to include an LCD display:

Required Components for LCD:

  • 16×2 LCD: This will display the readings.
  • I2C module (optional): If your LCD has an I2C interface, this makes the wiring much simpler.

Wiring the LCD:

  1. VCC: Connect to 5V.
  2. GND: Connect to GND.
  3. SDA: Connect to the SDA pin on the Arduino.
  4. SCL: Connect to the SCL pin on the Arduino.
See also
Arduino temperature and humidity control with Wi-Fi

Code for LCD Display:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

#define DHTPIN 7
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address

void setup() {
 lcd.begin(16, 2); // Initialize the LCD
 lcd.print("Temp & Humidity");
 delay(2000); // Wait for the display to initialize
 dht.begin(); // Initialize the DHT sensor
}

void loop() {
 delay(2000);

 float h = dht.readHumidity();
 float t = dht.readTemperature();

 if (isnan(h) || isnan(t)) {
  lcd.clear();
  lcd.print("Error reading!");
  return;
 }

 lcd.clear();
 lcd.setCursor(0, 0);
 lcd.print("Temp: ");
 lcd.print(t);
 lcd.print(" C");

 lcd.setCursor(0, 1);
 lcd.print("Hum: ");
 lcd.print(h);
 lcd.print(" %");
}

Explanation:

  • LCD initialization: The LCD is initialized with the address 0x27 (common for I2C LCDs).
  • Display the data: The temperature and humidity readings are displayed on the LCD. The lcd.setCursor(x, y) function controls where the text appears on the screen.

Troubleshooting Common Issues

When working with Arduino projects, some common issues may arise. Here are a few solutions:

1. Incorrect Sensor Readings:

If the readings from your sensor appear inaccurate or are constantly reading “NaN” (Not a Number), double-check your wiring and make sure the sensor is connected correctly.

2. LCD Not Displaying Data:

Ensure that the LCD is properly connected to the SDA and SCL pins on the Arduino. If you’re using an I2C module, verify that it’s compatible with your LCD.

3. Slow Response Time:

The DHT sensors like the DHT11 and DHT22 can take a few seconds to give an accurate reading. Ensure that you have a delay of at least 2 seconds between readings in your code.

Applications of Temperature and Humidity Measurement with Arduino

There are many practical applications for measuring temperature and humidity with Arduino, including:
  • Home Automation: Monitor and control the climate inside your house for comfort or energy efficiency.
  • Weather Stations: Build your own weather station to gather local weather data.
  • Greenhouses: Maintain optimal conditions for plant growth by monitoring temperature and humidity.
  • Industrial Applications: Track environmental conditions in factories or warehouses.

Conclusion

Using Arduino to measure temperature and humidity is both an educational and practical project. With just a few components and simple code, you can build your own system to monitor environmental conditions. Whether for personal projects or larger applications, this setup provides a cost-effective and customizable solution for real-time data tracking.
See also
Arduino temperature and humidity data logging
Now that you know how to measure temperature and humidity with Arduino, it’s time to explore and expand your projects further. Happy tinkering!