This project builds an automatic environmental control system that monitors temperature and light levels, then activates a fan and lamp accordingly. When the DHT11 sensor detects the temperature rising above 30°C, the fan turns on automatically; when it drops below 28°C, the fan shuts off. Meanwhile, the LDR light sensor triggers the lamp to illuminate when darkness is detected.
The guide provides a complete wiring diagram showing how to connect both sensors to the Arduino Uno, wire the NPN transistors as switches for the fan and lamp loads, and include the protective diode for the DC fan motor. Assembly steps walk through the connections in order, a full parts list with component specifications, and the Arduino firmware that reads the sensors and controls the outputs with hysteresis logic to prevent rapid switching.
Wiring diagram

Gather all the parts
Assemble it in 7 steps
1. Rút USB trước khi nối dây
Rút cáp USB khỏi Arduino trước khi cắm hoặc đổi dây. Dùng breadboard hoặc một điểm nối chung để chia chân 5V và GND cho DHT11, LDR, đèn và quạt.
- Chỉ cắm USB lại sau khi đã kiểm tra toàn bộ dây.
- Nếu dây 5V và GND chạm nhau, Arduino hoặc cáp USB có thể nóng lên.
2. Nối hai cảm biến
Trên DHT11 ghi +, OUT, −: nối + → 5V (nguồn), − → GND (ground), OUT → D2 (dữ liệu). Trên mô-đun LDR: nối VCC → 5V (nguồn), GND → GND (ground), OUT → D4 (tín hiệu).
- Nối theo chữ in trên mô-đun, không dựa vào màu dây.
- Vặn vít nhỏ trên LDR để chọn mức tối mà bạn muốn đèn bật.
- Đừng đổi dây nguồn dương và GND; nối ngược có thể làm hỏng mô-đun.
3. Nối transistor điều khiển đèn
Xem đúng sơ đồ chân E, B, C của transistor 2N2222 bạn có. Nối D9 → một đầu điện trở 1 kΩ (lệnh bật/tắt); đầu kia điện trở → Base/B. Nối Emitter/E → GND (đường về nguồn).
- Điện trở 1 kΩ không có chiều; đầu nào nối D9 cũng được.
- Không nối D9 thẳng vào Base/B vì dòng điện quá lớn có thể làm hỏng chân Arduino hoặc transistor.
4. Nối đèn qua transistor
Nối dây đỏ hoặc dây có dấu + của đèn → 5V (nguồn). Nối dây đen hoặc dây có dấu − của đèn → Collector/C transistor đèn (đường bật/tắt).
- Đèn ghi 3–6 V đã có hai dây, nên không cần điện trở 220 Ω nối tiếp trong mạch này.
- Không nối dây âm của đèn thẳng vào GND, vì đèn sẽ luôn sáng và Arduino không thể tắt nó.
5. Nối transistor điều khiển quạt
Với transistor thứ hai, xem đúng sơ đồ chân E, B, C của chính con bạn dùng. Nối D8 → một đầu điện trở 1 kΩ (lệnh bật/tắt); đầu còn lại điện trở → Base/B. Nối Emitter/E → GND (đường về nguồn).
- Quạt và đèn phải dùng hai transistor riêng, mỗi transistor có điện trở 1 kΩ riêng ở chân Base.
- Nhầm E, B, C có thể làm transistor nóng hoặc quạt không quay.
6. Nối quạt và diode bảo vệ
Nối dây đỏ quạt V+ → 5V (nguồn). Nối dây đen quạt V− → Collector/C transistor quạt (đường bật/tắt). Mắc diode 1N4007 song song với quạt: đầu không có vạch → dây đen quạt V− (bảo vệ), đầu có vạch bạc → 5V (bảo vệ).
- Diode không có chiều tùy ý: luôn để đầu có vạch bạc về 5V.
- Lắp ngược diode có thể chập nguồn khi quạt bật. Nếu quạt lớn hơn khoảng 150 mA, không cấp nó từ USB Arduino; dùng nguồn 5V ngoài đủ dòng và nối GND nguồn ngoài chung với GND Arduino.
7. Cấp nguồn và thử hoạt động
Kiểm tra một lần nữa rồi cắm Arduino qua USB. Che LDR để đèn bật, chiếu sáng để đèn tắt. Làm ấm DHT11 đến 30 °C hoặc hơn để quạt bật; quạt sẽ tắt khi nhiệt độ xuống 28 °C hoặc thấp hơn.
- Trong phần Serial của Schematik, bạn sẽ thấy nhiệt độ, độ ẩm, trạng thái sáng/tối, đèn và quạt.
- Nếu transistor, dây, Arduino hoặc quạt nóng bất thường, rút USB ngay và kiểm tra lại chân E–B–C cùng chiều diode.
Review all connections
1. Connections between "dht11_1" and "Arduino"
2. Connections between "ldr_module_1" and "Arduino"
3. Connections between "lamp_3to6v_1" and "Arduino"
4. Connections between "transistor_lamp_1" and "Arduino"
5. Connections between "resistor_lamp_base_1" and "Arduino"
6. Connections between "fan_5v_1" and "Arduino"
7. Connections between "transistor_fan_1" and "Arduino"
8. Connections between "resistor_fan_base_1" and "Arduino"
9. Connections between "diode_fan_1" and "Arduino"
Deploy the firmware
#include <DHT.h>
#define DHT_PIN 2
#define DHT_TYPE DHT11
#define LDR_OUT_PIN 4
#define FAN_PIN 8
#define LAMP_PIN 9
DHT dht(DHT_PIN, DHT_TYPE);
unsigned long lastSensorReadMs = 0;
const unsigned long SENSOR_INTERVAL_MS = 2000;
const float FAN_ON_TEMPERATURE_C = 30.0;
const float FAN_OFF_TEMPERATURE_C = 28.0;
bool fanOn = false;
void setup() {
Serial.begin(9600);
pinMode(LDR_OUT_PIN, INPUT);
pinMode(FAN_PIN, OUTPUT);
pinMode(LAMP_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW); // Start with the fan off.
digitalWrite(LAMP_PIN, LOW); // Start with the lamp off.
dht.begin();
Serial.println(F("Cam bien DHT11 va LDR da san sang."));
}
void loop() {
// Most common LDR comparator modules pull OUT LOW when the light level is below
// the threshold set by their small adjustment screw.
bool dark = (digitalRead(LDR_OUT_PIN) == LOW);
// The LDR module normally sends LOW when its adjustment threshold sees darkness.
digitalWrite(LAMP_PIN, dark ? HIGH : LOW);
unsigned long now = millis();
if (now - lastSensorReadMs < SENSOR_INTERVAL_MS) {
delay(SENSOR_INTERVAL_MS - (now - lastSensorReadMs));
return;
}
lastSensorReadMs = now;
float temperatureC = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperatureC) || isnan(humidity)) {
digitalWrite(FAN_PIN, LOW);
Serial.println(F("Khong doc duoc DHT11. Kiem tra VCC, GND va DATA."));
return;
}
if (!fanOn && temperatureC >= FAN_ON_TEMPERATURE_C) {
fanOn = true;
} else if (fanOn && temperatureC <= FAN_OFF_TEMPERATURE_C) {
fanOn = false;
}
digitalWrite(FAN_PIN, fanOn ? HIGH : LOW);
Serial.print(F("Nhiet do: "));
Serial.print(temperatureC, 1);
Serial.print(F(" C | Do am: "));
Serial.print(humidity, 0);
Serial.print(F(" % | LDR: "));
Serial.print(dark ? F("TOI") : F("SANG"));
Serial.print(dark ? F(" | Den: BAT") : F(" | Den: TAT"));
Serial.println(fanOn ? F(" | Quat: BAT") : F(" | Quat: TAT"));
}Remix this project
Make it yours in one click
Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.




