PIR Sensor for Automatic Fan Control

Project Overview

This project creates an automatic fan control system that turns on a ceiling or desk fan when someone enters a room and turns it off after they leave. It is perfect for bathrooms, workshops, or any room where you want ventilation only when occupied.

Difficulty: Beginner
Estimated time: 1-2 hours
Estimated cost: $15-25

How It Works

A PIR sensor detects when someone enters the room. When motion is detected, the system activates a relay that turns on the fan. After a set period of no motion, the fan turns off automatically. An optional temperature sensor can also trigger the fan if the room gets too hot.

Materials Needed

  • Arduino Uno or ESP32 (1)
  • HC-SR501 PIR sensor (1)
  • Relay module (5V, single channel, rated for fan load)
  • LED for status indication
  • 220Ω resistor for LED
  • Jumper wires
  • Power supply (5V 1A)
  • Project enclosure
  • DHT22 temperature sensor (optional)

Circuit Diagram

Connection Table

VCC

5V

GND

GND

OUT

Digital Pin 2

IN

Digital Pin 3

Anode

Digital Pin 13 (via 220Ω)

DATA

Digital Pin 4

Arduino Code

// Automatic Fan Control with PIR
#include <DHT.h>

const int pirPin = 2;
const int relayPin = 3;
const int ledPin = 13;

unsigned long lastMotionTime = 0;
const unsigned long fanOffDelay = 300000;

bool fanOn = false;

#ifdef USE_TEMP_SENSOR
#define DHTTYPE DHT22
const int dhtPin = 4;
DHT dht(dhtPin, DHTTYPE);
const float tempThreshold = 28.0;
#endif

void setup() {
  Serial.begin(9600);
  
  pinMode(pirPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
  digitalWrite(relayPin, LOW);
  digitalWrite(ledPin, LOW);
  
  #ifdef USE_TEMP_SENSOR
  dht.begin();
  #endif
  
  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;
  
  #ifdef USE_TEMP_SENSOR
  float temp = dht.readTemperature();
  if (!isnan(temp) && temp > tempThreshold && !fanOn) {
    turnFanOn();
  }
  #endif
  
  if (motion) {
    lastMotionTime = millis();
    if (!fanOn) turnFanOn();
  }
  
  if (fanOn && (millis() - lastMotionTime > fanOffDelay)) {
    #ifdef USE_TEMP_SENSOR
    if (temp <= tempThreshold || isnan(temp)) turnFanOff();
    else lastMotionTime = millis();
    #else
    turnFanOff();
    #endif
  }
  
  delay(100);
}

Installation Steps

  1. Assemble circuit on breadboard and test
  2. Wire the fan to the relay (AC wiring requires caution)
  3. Mount PIR sensor at 2m height covering the room
  4. Place Arduino and relay in a safe enclosure
  5. Connect power supply
  6. Walk into room to test fan activation

Project Extensions

  • Add PWM control for variable fan speed
  • Add magnetic reed switch to disable fan when window is open
  • Use DHT22 to turn on fan when humidity rises (bathrooms)
  • Add ESP32 to report fan status and allow remote control
  • Add push button for manual override

Conclusion

This automatic fan control system ensures ventilation only when needed, saving energy and reducing noise when rooms are empty.

Leave a Reply

Your email address will not be published. Required fields are marked *

Component Pin Arduino Pin
PIR Sensor PIR Sensor PIR Sensor Relay Module Status LED DHT22 (optional)