Project Overview
This project creates a smart fan controller that turns on a fan when motion is detected and the room temperature exceeds a set threshold. It is perfect for bathrooms, home offices, or workshops where you want ventilation only when needed.
Difficulty: Intermediate
Estimated time: 2 hours
Estimated cost: $20-30
How It Works
A PIR sensor detects motion, and a DHT22 temperature sensor measures the room temperature. If motion is detected AND the temperature exceeds the threshold (e.g., 26°C), the system activates a relay to turn on the fan. The fan runs for a set time after motion stops.
Materials Needed
- Arduino Uno or Nano (1)
- HC-SR501 PIR sensor (1)
- DHT22 temperature/humidity sensor (1)
- Relay module (5V, rated for fan load)
- LED for status indication
- 220Ω resistor
- Jumper wires
- Power supply (5V 2A)
Circuit Diagram
Connection Table
| Component |
Pin |
Arduino Pin |
PIR Sensor |
OUT
Digital Pin 2
DHT22 |
DATA
Digital Pin 3
Relay Module |
IN
Digital Pin 4
Status LED |
Anode
Digital Pin 13
表
Arduino Code
// Smart Fan Controller with PIR and Temperature Sensor
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 3
DHT dht(DHTPIN, DHTTYPE);
const int pirPin = 2;
const int relayPin = 4;
const int ledPin = 13;
const float tempThreshold = 26.0; // Celsius
unsigned long lastMotionTime = 0;
const unsigned long fanOffDelay = 600000; // 10 minutes
bool fanOn = false;
void setup() {
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(relayPin, LOW);
digitalWrite(ledPin, LOW);
dht.begin();
delay(60000);
}
void turnFanOn() {
digitalWrite(relayPin, HIGH);
digitalWrite(ledPin, HIGH);
fanOn = true;
lastMotionTime = millis();
}
void turnFanOff() {
digitalWrite(relayPin, LOW);
digitalWrite(ledPin, LOW);
fanOn = false;
}
void loop() {
bool motion = digitalRead(pirPin) == HIGH;
float temp = dht.readTemperature();
if (isnan(temp)) {
Serial.println("DHT read error");
delay(1000);
return;
}
if (motion && !fanOn && temp > tempThreshold) {
turnFanOn();
}
if (fanOn && motion) {
lastMotionTime = millis();
}
if (fanOn && (millis() - lastMotionTime > fanOffDelay)) {
turnFanOff();
}
delay(100);
}
Installation Steps
- Assemble the circuit and test.
- Mount the PIR sensor to cover the room.
- Place the DHT22 away from direct airflow.
- Wire the fan through the relay (AC wiring requires caution).
- Adjust temperature threshold as desired.
- Test by entering the room when warm.
Project Extensions
- Add humidity sensing to trigger the fan (bathroom use)
- Add manual override button
- Add Wi-Fi to report temperature and fan status
- Add LCD to display temperature and fan state
Conclusion
This smart fan controller saves energy by running the fan only when needed.