Arduino temperature and humidity control with relays

Photo of author

By Jackson Taylor

When it comes to building a reliable temperature and humidity control system, Arduino offers an accessible yet powerful solution. By integrating sensors, relays, and an Arduino board, you can effectively manage environments like greenhouses, server rooms, or terrariums. This guide walks you through how to create your own system to monitor and control temperature and humidity using Arduino, relays, and the appropriate sensors.

What is Arduino Temperature and Humidity Control?

Arduino temperature and humidity control refers to the process of using an Arduino microcontroller to monitor and adjust the levels of temperature and humidity in a given space. By connecting sensors that measure environmental conditions and relays that control equipment like fans, heaters, or humidifiers, you can automate the process of maintaining a stable environment.

Key Components for Building the Control System

To start building your own Arduino-based temperature and humidity control system, you will need the following components:

Arduino Board

The heart of the project, the Arduino board, processes the data from sensors and activates relays based on specific conditions. Common models used in these systems include the Arduino Uno and Arduino Mega.

Temperature and Humidity Sensors

The most popular sensor for temperature and humidity is the DHT22 (or DHT11 for a lower-cost alternative). These sensors can accurately measure both temperature and humidity, providing data to the Arduino to make decisions on whether to activate or deactivate a relay.

Relays

Relays act as switches, enabling you to control high-voltage devices like fans, heaters, and humidifiers using the low-voltage output from your Arduino. A common relay module for Arduino projects is the 5V relay module.

Fans, Heaters, or Humidifiers

These devices will be controlled by the relays to either increase or decrease the temperature or humidity in the environment. For example, a fan can cool down a room, while a heater can raise the temperature.

How to Set Up Your Arduino Temperature and Humidity Control System

Step 1: Wiring the Temperature and Humidity Sensor

The first step is to wire your DHT22 sensor to the Arduino board. Connect the following pins:
  • VCC to the 5V pin on the Arduino.
  • GND to the GND pin on the Arduino.
  • Data to a digital pin (usually pin 2 or 4).
See also
Arduino cloud integration with Particle IoT
You’ll also need to add a 10kΩ pull-up resistor between the data pin and the 5V pin to ensure stable readings from the sensor.

Step 2: Wiring the Relay Module

The relay module connects to the Arduino to control devices like fans or heaters. Here’s how to wire it:
  • Connect the VCC pin of the relay module to the 5V pin on the Arduino.
  • Connect the GND pin to the GND pin on the Arduino.
  • Connect the IN pin to a digital pin on the Arduino (e.g., pin 7).

Step 3: Writing the Arduino Code

Once your hardware is set up, you need to write the code for your Arduino. The code will read the temperature and humidity from the sensor and compare it to the set thresholds. If the readings fall outside the desired range, the Arduino will activate or deactivate the relay to control the connected devices. Here’s a basic code snippet to get you started:
cpp
#include <DHT.h> #define DHTPIN 2 // Pin connected to DHT sensor #define DHTTYPE DHT22 // Define sensor type #define RELAYPIN 7 // Pin connected to relay DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); pinMode(RELAYPIN, OUTPUT); } void loop() { float temp = dht.readTemperature(); float humidity = dht.readHumidity(); if (isnan(temp) || isnan(humidity)) { Serial.println("Failed to read from DHT sensor!"); return; } Serial.print("Temperature: "); Serial.print(temp); Serial.print(" °C Humidity: "); Serial.print(humidity); Serial.println(" %"); if (temp > 30) { // If temperature exceeds 30°C, activate relay digitalWrite(RELAYPIN, HIGH); } else if (temp < 20) { // If temperature drops below 20°C, deactivate relay digitalWrite(RELAYPIN, LOW); } delay(2000); // Wait 2 seconds before reading again }

Step 4: Testing the System

After uploading the code to your Arduino board, test the system by adjusting the temperature or humidity manually (using a heat source or humidifier) and checking if the relay activates as expected. If the temperature exceeds the preset threshold, the relay should turn on the fan or other cooling device. Conversely, if the temperature falls below the threshold, the system should turn off the device.
See also
How to measure temperature and humidity with Arduino

Fine-Tuning the System for Optimal Control

Once you have your basic setup working, you can fine-tune the system. Consider adding these features to improve functionality:

1. Adjustable Thresholds

Allow the user to change the temperature and humidity thresholds via the serial monitor or external buttons. This flexibility enables better control over various environments.

2. Multiple Relays for Different Devices

If you need more control, add more relays to control multiple devices. For example, one relay could control a fan, while another controls a heater or humidifier. You can use the same logic in the code to activate different relays based on different conditions.

3. Data Logging

For a more advanced setup, add an SD card module to store temperature and humidity data over time. This data can then be used for analysis, allowing you to better understand how temperature and humidity fluctuate in your environment.

Troubleshooting Common Issues

1. Inaccurate Readings

If the sensor isn’t providing accurate readings, check the wiring and ensure the 10kΩ pull-up resistor is properly placed. You might also want to recalibrate the sensor by adjusting the code.

2. Relay Not Triggering

If the relay isn’t activating, make sure that the relay module is correctly wired and that the Arduino code is correctly written. Double-check that the digital pin used for the relay is properly defined.

3. Devices Not Responding

When connecting high-voltage devices, make sure you’re following safety guidelines. Verify the relay’s current rating and ensure the devices you’re controlling don’t exceed the relay’s limits.

Applications of Arduino Temperature and Humidity Control

Greenhouses

Greenhouses often require precise control of both temperature and humidity for optimal plant growth. Using Arduino with relays allows growers to automate the environment, reducing the need for manual adjustments.

Server Rooms

Maintaining stable temperature and humidity levels in server rooms is critical to prevent equipment damage. Arduino-based control systems can automate cooling and humidifying devices to ensure these conditions are met consistently.
See also
Arduino temperature and humidity control with Wi-Fi

Terrariums

For reptile or plant terrariums, precise temperature and humidity control is crucial. Arduino systems can regulate heaters, fans, and misting systems to maintain a comfortable environment for animals and plants.

Conclusion

Arduino offers a simple yet effective way to automate temperature and humidity control using relays. By integrating sensors and relays, you can create a system that adjusts the environment automatically based on real-time data. Whether for a greenhouse, server room, or terrarium, this project offers endless possibilities for efficient environmental management.