Build Arduino intelligent temperature and humidity monitoring system from scratch technical blog

  • Share this:
post-title
Welcome to this technical blog about Arduino's intelligent temperature and humidity monitoring system. In this series, we will delve into how to use Arduino programming and IoT technology to build a home automation system to monitor and regulate the indoor environment. Through this blog, you will learn how to choose the right hardware components, write the necessary software code, and how to integrate the system with smart home devices to achieve precise control of the home environment. Whether you want to improve the comfort of your home or create a safer environment for your family, this knowledge will provide you with valuable help.
In today's smart home era, using Arduino to build an intelligent temperature and humidity monitoring system has become an effective way to improve home comfort and safety.

This technical blog will take you from scratch to learn more about how to leverage Arduino programming and Internet of Things (IoT) applications to build your home automation solution.

Required hardware components.

To build an intelligent temperature and humidity monitoring system, you need the following hardware components: 1. # Arduino Development Board #: such as Arduino Uno or Arduino Mega.

2. # DHT11/DHT22 Temperature and Humidity Sensor #: used to measure the temperature and humidity of the environment.

3. # LCD display #: used to display temperature and humidity data.

4. # Relay Module #: A switch used to control other devices.

5. # Breadboard and Jumper #: for connecting various components.

6. # Power Adapter #: Powers the Arduino development board.

7. # Wi-Fi module (optional) #: If you need remote monitoring, you can use the ESP8266 Wi-Fi module.

Software development process.

\n#
Install the necessary libraries.

First, make sure you have the Arduino IDE installed and the DHT sensor library downloaded.

You can search and install DHT libraries in Arduino IDE by "Tools" - > "Manage Libraries".


#include 
#include 

\n#
Define pins and initializations.

Next, define the pins of the DHT sensor and LCD display, and setup()Function to initialize.


#define DHTPIN 2          // DHT传感器连接到数字引脚2
#define DHTTYPE DHT11     // 使用DHT11传感器
DHT dht(DHTPIN, DHTTYPE);

#define LCD_ADDRESS 0x27  // I2C地址
#define LCD_COLS 16       // 列数
#define LCD_ROWS 2        // 行数
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLS, LCD_ROWS);

\n#
Read temperature and humidity data.

Inloop()In the function, read the data of the DHT sensor and display it on the LCD screen.


void loop() {
  float h = dht.readHumidity();    // 读取湿度
  float t = dht.readTemperature(); // 读取温度

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    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(" %");

  delay(2000); // 每2秒更新一次数据
}

Integrated into smart home system.

In order to integrate your smart temperature and humidity monitoring system with other smart home devices, you can use relay modules to control lights, fans and other devices.

The following is a simple example code to turn on the fan when the temperature exceeds the set value.


#define FAN_PIN 3         // 风扇连接到数字引脚3
#define TEMP_THRESHOLD 25 // 温度阈值

void setup() {
  pinMode(FAN_PIN, OUTPUT);
  digitalWrite(FAN_PIN, LOW); // 初始状态关闭风扇
}

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

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

  if (t > TEMP_THRESHOLD) {
    digitalWrite(FAN_PIN, HIGH); // 打开风扇
  } else {
    digitalWrite(FAN_PIN, LOW); // 关闭风扇
  }

  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(" %");

  delay(2000); // 每2秒更新一次数据
}

Remote monitoring (optional).

If you want to implement remote monitoring, you can use the ESP8266 Wi-Fi module to send data to the cloud server.

The following is a simple example code showing how to send temperature and humidity data to the ThingSpeak platform.


#include 
#include 

const char* ssid = "your_SSID";       // 替换为你的Wi-Fi名称
const char* password = "your_PASSWORD"; // 替换为你的Wi-Fi密码
unsigned long myChannelNumber = your_channel_number; // 替换为你的ThingSpeak频道号
const char* myWriteAPIKey = "your_write_api_key";    // 替换为你的ThingSpeak写API密钥

WiFiClient client;

void setup() {
  Serial.begin(9600);
  ThingSpeak.begin(client);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
}

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

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

  ThingSpeak.setField(1, t); // 设置字段1为温度
  ThingSpeak.setField(2, h); // 设置字段2为湿度
  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); // 写入数据到ThingSpeak频道

  delay(20000); // 每20秒发送一次数据
}

Summarize.

Through the above steps, you have successfully built an Arduino-based intelligent temperature and humidity monitoring system.

This system can not only monitor the temperature and humidity of the environment in real time, but also integrate with other smart home devices to realize automatic control.

In addition, you can also check the environmental conditions of your home at any time through the remote monitoring function.

I hope this technical blog will help you, so that you can better use Arduino for IoT programming and create a comfortable and safe smart home environment.