Arduino temperature and humidity control with Wi-Fi

Photo of author

By Jackson Taylor

Home » Arduino » Arduino temperature and humidity control with Wi-Fi

As an Amazon Associate we earn from qualifying purchases.

When it comes to monitoring environmental conditions, particularly temperature and humidity, an efficient system is essential. With the power of Arduino and Wi-Fi connectivity, you can easily create a smart temperature and humidity control system that allows for remote monitoring and management. In this guide, we will walk you through the process of building your very own Arduino-based system with Wi-Fi capabilities for real-time control and monitoring.

What is Arduino Temperature and Humidity Control?

Arduino temperature and humidity control refers to the use of an Arduino microcontroller to monitor and adjust environmental parameters, specifically temperature and humidity. By integrating sensors and Wi-Fi modules, you can create an automated system that can send data to a mobile device or computer, allowing you to control and view conditions remotely.

Components Required for the Project

Before diving into the setup process, it’s important to gather the necessary components. Below is a list of essential items for building your own Arduino temperature and humidity control system with Wi-Fi:

1. Arduino Board (e.g., Arduino Uno or Nano)

The Arduino board serves as the central processing unit for the system. It reads data from the sensors and controls connected devices.

2. DHT11 or DHT22 Temperature and Humidity Sensor

The DHT11 and DHT22 are commonly used sensors for measuring temperature and humidity. While the DHT11 is more affordable, the DHT22 offers greater accuracy and a wider range of measurement.

3. ESP8266 Wi-Fi Module

The ESP8266 is a low-cost Wi-Fi module that allows the Arduino to connect to a network, enabling remote monitoring via a web server or mobile app.

4. Jumper Wires

These wires will connect the components, including the Arduino, sensor, and Wi-Fi module.

5. Breadboard

A breadboard is helpful for prototyping the circuit before soldering.

6. Power Supply

A 5V power supply is required to power the Arduino and the Wi-Fi module.
See also
Arduino temperature and humidity data logging

Wiring and Connecting the Components

Now that you have all the necessary components, let’s move on to the wiring process. This is a crucial step to ensure your Arduino-based temperature and humidity control system works properly.

Step-by-Step Wiring

  1. Connect the DHT Sensor to Arduino:
    • Connect the VCC pin of the DHT sensor to the 5V pin on the Arduino.
    • Connect the GND pin of the sensor to the GND pin on the Arduino.
    • Connect the DATA pin of the DHT sensor to a digital input pin on the Arduino (for example, pin 2).
  2. Integrating the ESP8266 Wi-Fi Module:
    • Connect the VCC pin of the ESP8266 to the 3.3V pin of the Arduino.
    • Connect the GND pin of the ESP8266 to the GND pin of the Arduino.
    • Connect the TX pin of the ESP8266 to the RX pin of the Arduino (pin 0).
    • Connect the RX pin of the ESP8266 to the TX pin of the Arduino (pin 1).
Tip: Ensure you have sufficient power for the ESP8266 module, as it can draw more current than the Arduino’s 3.3V output can supply. It’s recommended to use an external power supply for the ESP8266 if necessary.

Writing the Code

With the hardware connected, the next step is to write the code that will enable the Arduino to read data from the sensor and send it over Wi-Fi. Below is an example of code that integrates both the DHT22 sensor and the ESP8266 Wi-Fi module.
cpp
#include <DHT.h>
#include <ESP8266WiFi.h>

#define DHTPIN 2 // DHT sensor connected to pin 2
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);

const char* ssid = "your_SSID"; // Wi-Fi SSID
const char* password = "your_PASS"; // Wi-Fi Password

void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}

void loop() {
delay(2000); // Wait for 2 seconds before reading the sensor

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

if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor");
return;
}

Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");

// Additional code to send data over Wi-Fi can be added here.
}

What Does the Code Do?

  • DHT Sensor Setup: The code initializes the DHT22 sensor to read temperature and humidity data.
  • Wi-Fi Connection: It connects to your Wi-Fi network using the ESP8266 Wi-Fi module.
  • Data Reading: Every 2 seconds, the temperature and humidity are read from the sensor and displayed in the serial monitor.
See also
Using the Arduino map() Function to Remap Values

Sending Data to the Cloud

Once the Arduino reads the data, you may want to send it to the cloud or a web server for remote access. This step is where the ESP8266 Wi-Fi module comes in.

Option 1: Using Blynk for Mobile Access

One easy way to monitor the data is by using the Blynk app, which allows you to create a custom interface to display your temperature and humidity readings on your mobile device.
  1. Install the Blynk App: Download the Blynk app from the App Store or Google Play Store.
  2. Create a New Project: In the Blynk app, create a new project, select your board (Arduino Uno), and Wi-Fi as the connection type.
  3. Generate an Authentication Token: This token will be used in the code to connect the Arduino to the Blynk server.

Option 2: Use ThingSpeak for Cloud Monitoring

If you prefer cloud-based monitoring, ThingSpeak is a great option. With ThingSpeak, you can send data from your Arduino to the cloud and access it from anywhere.
  1. Create a ThingSpeak Account: Sign up at ThingSpeak.
  2. Generate an API Key: This key will be used to send data to ThingSpeak.
  3. Modify Your Code: Update your code to include the ThingSpeak API key and send data to the channel.
cpp
#include <ThingSpeak.h>
#include <WiFi.h>

char ssid[] = "your_SSID"; 
char pass[] = "your_PASS"; 
WiFiClient client;

unsigned long myChannelNumber = YOUR_CHANNEL_ID;
const char * myWriteAPIKey = "YOUR_API_KEY"; 

void setup() {
 WiFi.begin(ssid, pass);
 ThingSpeak.begin(client); 
}

void loop() {
 float h = dht.readHumidity();
 float t = dht.readTemperature();

 if (!isnan(h) && !isnan(t)) {
  ThingSpeak.setField(1, h);
  ThingSpeak.setField(2, t);
  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
 }
 delay(2000); 
}

Real-Time Monitoring

With the data sent to the cloud, you can view your temperature and humidity levels in real-time via a dashboard. Both Blynk and ThingSpeak offer intuitive user interfaces that allow for easy data visualization.

Benefits of Arduino Temperature and Humidity Control with Wi-Fi

Building a Wi-Fi-enabled temperature and humidity control system using Arduino offers numerous benefits, including:
  1. Remote Monitoring: With Wi-Fi connectivity, you can monitor temperature and humidity from anywhere using your smartphone or computer.
  2. Automation: Set thresholds for temperature and humidity, and automate actions like turning on a fan or heater when conditions exceed a certain level.
  3. Data Logging: By sending data to the cloud, you can keep track of environmental changes over time.
  4. Cost-Effective: Arduino and Wi-Fi modules are affordable, making this project budget-friendly.
See also
How to build an autonomous car with Arduino

Turning Your Wi-Fi Thermometer into a Humidity Controller

The code above only reads and prints values. To make it a controller, you need a rule that switches something on or off, such as a fan, heater, humidifier or dehumidifier. The simplest rule is a threshold: if humidity goes above a set value, switch the output on. On its own this causes a problem, because the reading hovers around the threshold and the output clicks on and off rapidly.

The usual fix is hysteresis, which means using two values instead of one. For example, switch a dehumidifier on when humidity rises above your upper limit, and switch it off only after it falls below a lower limit. The gap between the two numbers stops constant switching and is easier on relays and connected devices. The same idea works for temperature.

A few practical points will save you trouble:

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video
  • Match the board to the library. The ESP8266WiFi library in the sample code is meant for programs that run on an ESP8266 board itself, such as a NodeMCU or Wemos D1 mini. If you want to keep a classic Arduino Uno and use the ESP8266 only as a Wi-Fi add-on, you will need to talk to it with AT commands or a library built for that setup. Using an ESP8266-based board alone is often simpler, since it has Wi-Fi built in and can read the DHT sensor directly.
  • Watch the logic levels. The ESP8266 works at 3.3V, while an Uno sends 5V signals. Use a voltage divider or level shifter on the line going into the ESP8266 RX pin.
  • Keep mains power isolated. Drive heaters, fans or humidifiers through a relay module rated for the load, and do not wire mains voltage on a breadboard. If you are not comfortable with mains wiring, use low-voltage devices or a ready-made smart plug.
  • Allow for sensor limits. DHT sensors respond slowly and should be kept away from heat sources and direct airflow from the device you are controlling. Otherwise the controller reacts to local conditions rather than the room.
  • Handle Wi-Fi drops. Make sure the control logic keeps running if the connection is lost, so the system does not depend on the cloud to switch a device off.
See also
How to use Wi-Fi with Arduino

Once the basic loop works, you can add a way to change the target values remotely, for example through a Blynk slider or a simple web page served by the board.

Conclusion

Creating an Arduino-based temperature and humidity control system with Wi-Fi is a rewarding project that combines hardware and software to provide remote monitoring and control. By using simple components like the DHT22 sensor and ESP8266 Wi-Fi module, you can easily set up your system and monitor environmental conditions in real-time. Whether you’re looking to manage your greenhouse, server room, or just ensure a comfortable living environment, this project provides an excellent introduction to the world of IoT and smart systems.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works