DIY Smart Environmental Monitoring! Build a High-Precision Temperature and Humidity Sensor with ESP32
Say goodbye to stuffiness and dampness, easily build your own environmental monitoring system
In modern smart home systems, temperature and humidity monitoring is an essential basic function. Today, I will teach you how to use the powerful ESP32 development board, paired with the common DHT series sensors, to create a professional-grade environmental monitoring device. This project is simple and easy to get started with, yet it can achieve unexpectedly practical functions!
🛠 Hardware Preparation List
- Core BrainESP32 development board (with WiFi/Bluetooth capabilities)
- Environmental SensorDHT11 or DHT22 sensor (it is recommended to choose the more precise DHT22)
- Connection Accessoriesbreadboard, Dupont wires, 4.7KΩ resistor (usually built into the sensor module)
Wiring Guide (Easily Completed in 3 Steps)
- Connect the sensor’s VCC to the ESP32’s 3.3V pin
- GND pin connects to the ESP32’s GND
- DATA pin connects to GPIO4 (other pins can be used, just modify the code accordingly)
Wiring Tip: If using a bare sensor, remember to add a 4.7KΩ pull-up resistor between the VCC and DATA pins
💻 Core Code Implementation
#include <DHT.h>
// Configuration parameters
#define DHTPIN 4 // Data pin
#define DHTTYPE DHT22 // Sensor type (DHT22 has higher precision)
DHT dht(DHTPIN, DHTTYPE); // Initialize sensor
void setup() {
Serial.begin(115200);
dht.begin(); // Start sensor
Serial.println("Environmental monitoring system has started");
}
void loop() {
delay(2000); // Sampling interval of 2 seconds
// Read environmental data
float humidity = dht.readHumidity(); // Humidity (%)
float temp = dht.readTemperature(); // Temperature (℃)
// Check for abnormal data
if (isnan(humidity) || isnan(temp)) {
Serial.println("Failed to read data, please check the sensor connection!");
return;
}
// Calculate the heat index (especially important in summer)
float heatIndex = dht.computeHeatIndex(temp, humidity, false);
// Print monitoring results to serial
Serial.print("Temperature: "); Serial.print(temp); Serial.print("°C");
Serial.print("\tHumidity: "); Serial.print(humidity); Serial.print("%\t");
Serial.print("Feels Like: "); Serial.print(heatIndex); Serial.println("°C");
}
🔧 Required Library Installation
- Arduino IDE → Tools → Manage Libraries
- Search and install:
- DHT sensor library (by Adafruit)
- Adafruit Unified Sensor (support library)
✨ Extended Features: Build a Professional Monitoring System
1️⃣ Add OLED Screen (Real-time Data Display)
#include <Wire.h>
#include <Adafruit_SSD1306.h>
// Initialize 0.96 inch OLED
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void showData(float t, float h) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Temperature: "); display.print(t); display.println(" C");
display.print("Humidity: "); display.print(h); display.println(" %");
display.display();
}
2️⃣ Cloud Data Upload (Build IoT Platform)
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "your_wifi";
const char* password = "your_password";
const char* mqtt_server = "broker.example.com";
WiFiClient espClient;
PubSubClient client(espClient);
void connectWifi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void sendData(float t, float h) {
char msg[50];
snprintf(msg, 50, "temp=%.2f,hum=%.2f", t, h);
client.publish("home/sensor/env", msg);
}
3️⃣ Ultra-Low Power Mode (Battery-Powered Solution)
#include "driver/rtc_io.h"
// Operations before entering sleep
void deepSleepSetup() {
esp_sleep_enable_timer_wakeup(5 * 60 * 1000000); // Sleep for 5 minutes
rtc_gpio_isolate(GPIO_NUM_4); // Isolate sensor pin
esp_deep_sleep_start();
}
// Call at the end of loop()
deepSleepSetup();
🧐 Frequently Asked Questions
Q1: Sensor readings are unstable or fail?
- It is recommended to add a 10KΩ pull-up resistor to the data pin
- Shorten the wire length to avoid power interference
- Try using another GPIO pin
Q2: How to choose between different sensors?
| Parameter | DHT11 | DHT22 |
|---|---|---|
| Temperature Range | 0-50℃ | -40-80℃ |
| Temperature Accuracy | ±2℃ | ±0.5℃ |
| Humidity Range | 20-80% | 0-100% |
| Humidity Accuracy | ±5% | ±2% |
Q3: How to reduce system power consumption?
- Enable deep sleep mode, which can reduce to microamp level
- Disconnect the power LED on the development board
- Reduce the ESP32 main frequency to 80MHz
🚀 Project Upgrade Directions
- Greenhouse Monitoring System Add soil moisture sensors for automatic irrigation
- Smart Ventilation System Automatically turn on the exhaust fan when humidity exceeds limits
- Cloud Data Visualization Build historical data charts through Alibaba Cloud/AWS
- Anomaly Alerts Push WeChat notifications when environmental parameters exceed limits
Get started on building your own environmental monitoring system! Whether it’s monitoring humidity in a nursery or providing moisture alerts in a basement, the ESP32 can provide you with real-time accurate data support. Feel free to share your results in the comments!
ESP32 IoT CompassMaster the microcontroller in three days