Community project

Automatic Fan And Light

Photo of Automatic Fan And Light
Generated with AI

Đình Quý

Last updated October 17, 2026

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

Wiring diagram for Automatic Fan And Light
Loading diagram…

Gather all the parts

QtyComponent
1

Mô-đun DHT11 3 chân

Mô-đun nhỏ đo nhiệt độ và độ ẩm rồi gửi số đo cho Arduino.

1

Mô-đun cảm biến ánh sáng LDR 3 chân

Mô-đun dùng quang trở để báo sáng hoặc tối bằng một chân OUT.

1

Đèn 3–6 V hai dây

Đèn hai dây dùng nguồn một chiều trong khoảng 3 đến 6 V.

1

NPN Transistor (2N2222 / BC547)

General-purpose NPN bipolar junction transistor (BJT) in TO-92 package. Used as a low-side switch to drive loads (e.g. buzzers) whose current exceeds ESP32 GPIO source capability (~12 mA). A ~1 kΩ base resistor is placed between the ESP32 GPIO and the transistor base. The collector connects to the load negative terminal (buzzer –), and the emitter connects to GND. When the GPIO goes HIGH (3.3 V), the transistor saturates and switches the load on. Compatible with both 2N2222/2N2222A and BC547/BC547B variants. Add a flyback diode (e.g. 1N4148) across inductive load terminals.

1

Điện trở 1 kΩ

1 kΩ

Điện trở này giới hạn dòng từ chân Arduino vào transistor để bảo vệ chân điều khiển.

1

Quạt DC 5 V hai dây

Quạt nhỏ 5 V làm mát khi nhiệt độ đo được vượt ngưỡng.

1

NPN Transistor (2N2222 / BC547)

General-purpose NPN bipolar junction transistor (BJT) in TO-92 package. Used as a low-side switch to drive loads (e.g. buzzers) whose current exceeds ESP32 GPIO source capability (~12 mA). A ~1 kΩ base resistor is placed between the ESP32 GPIO and the transistor base. The collector connects to the load negative terminal (buzzer –), and the emitter connects to GND. When the GPIO goes HIGH (3.3 V), the transistor saturates and switches the load on. Compatible with both 2N2222/2N2222A and BC547/BC547B variants. Add a flyback diode (e.g. 1N4148) across inductive load terminals.

1

Điện trở 1 kΩ

1 kΩ

Điện trở giới hạn dòng từ chân Arduino đến chân điều khiển transistor quạt.

1

1N4007 Diode

General-purpose silicon rectifier diode in DO-204AL (DO-41) axial through-hole package. Rated 1A average forward current and 1000V peak reverse voltage (VRRM). Forward voltage drop ~0.7V at typical load (up to 1.1V at full 1A). Used here in series with LM7805 Vout for reverse polarity protection: anode to LM7805 output, cathode to 5V rail, resulting in ~4.3V on the output rail. Not suitable for high-frequency switching (reverse recovery ~2µs–30µs); intended for DC or 50/60Hz rectification only.

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"

Functiondht11_1Arduino
groundGNDGND
dataDATAGPIO 2
powerVCC5V

2. Connections between "ldr_module_1" and "Arduino"

Functionldr_module_1Arduino
powerVCC5V
groundGNDGND
digitalOUTGPIO 4

3. Connections between "lamp_3to6v_1" and "Arduino"

Functionlamp_3to6v_1Arduino
powerV+5V
groundV- → NPN Transistor (2N2222 / BC547) COLLECTOREXT

4. Connections between "transistor_lamp_1" and "Arduino"

Functiontransistor_lamp_1Arduino
groundEMITTERGND

5. Connections between "resistor_lamp_base_1" and "Arduino"

Functionresistor_lamp_base_1Arduino
digitalEnd AGPIO 9
digitalEnd B → NPN Transistor (2N2222 / BC547) BASEEXT

6. Connections between "fan_5v_1" and "Arduino"

Functionfan_5v_1Arduino
powerV+5V
groundV- → NPN Transistor (2N2222 / BC547) COLLECTOREXT

7. Connections between "transistor_fan_1" and "Arduino"

Functiontransistor_fan_1Arduino
groundEMITTERGND

8. Connections between "resistor_fan_base_1" and "Arduino"

Functionresistor_fan_base_1Arduino
digitalEnd AGPIO 8
digitalEnd B → NPN Transistor (2N2222 / BC547) BASEEXT

9. Connections between "diode_fan_1" and "Arduino"

Functiondiode_fan_1Arduino
powerAnode → Quạt DC 5 V hai dây V-EXT
powerCathode5V

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.

Open in Schematik