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’s 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 (e.g., 5 minutes), 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)
  • Resistor (220Ω for LED)
  • Jumper wires
  • Power supply (5V 1A)
  • Project enclosure
  • Optional: DHT22 temperature sensor

Circuit Diagram

Connection Table

Component Pin Arduino Pin
PIR Sensor VCC 5V PIR Sensor GND GND PIR Sensor OUT Digital Pin 2 Relay Module VCC 5V Relay Module GND GND Relay Module IN Digital Pin 3 Status LED Anode Digital Pin 13 (through 220Ω) Status LED Cathode GND DHT22 (optional) DATA Digital Pin 4

Arduino Code

// Automatic Fan Control with PIR
#include <DHT.h>  // Include if using temperature sensor

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

unsigned long lastMotionTime = 0;
const unsigned long fanOffDelay = 300000; // 5 minutes

bool fanOn = false;

#ifdef USE_TEMP_SENSOR
#define DHTTYPE DHT22
const int dhtPin = 4;
DHT dht(dhtPin, DHTTYPE);
const float tempThreshold = 28.0; // Turn on if above 28°C
#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
  
  Serial.println("Auto Fan Control Ready");
  delay(60000); // PIR warm-up
}

void turnFanOn() {
  digitalWrite(relayPin, HIGH);
  digitalWrite(ledPin, HIGH);
  fanOn = true;
  lastMotionTime = millis();
  Serial.println("Fan ON");
}

void turnFanOff() {
  digitalWrite(relayPin, LOW);
  digitalWrite(ledPin, LOW);
  fanOn = false;
  Serial.println("Fan OFF");
}

void loop() {
  bool motion = digitalRead(pirPin) == HIGH;
  
  #ifdef USE_TEMP_SENSOR
  float temp = dht.readTemperature();
  if (!isnan(temp) && temp > tempThreshold) {
    if (!fanOn) {
      turnFanOn();
    }
  }
  #endif
  
  if (motion) {
    lastMotionTime = millis();
    if (!fanOn) {
      turnFanOn();
    }
  }
  
  if (fanOn && (millis() - lastMotionTime > fanOffDelay)) {
    #ifdef USE_TEMP_SENSOR
    // Only turn off if temperature is below threshold
    if (temp <= tempThreshold || isnan(temp)) {
      turnFanOff();
    } else {
      lastMotionTime = millis(); // Reset timer if still hot
    }
    #else
    turnFanOff();
    #endif
  }
  
  delay(100);
}

Ceiling Fan Wiring Safety

Important: Ceiling fans typically operate on 120V/240V AC. Use a relay rated for the fan’s current (typically 0.5-1.5A). If you are not experienced with AC wiring, consult an electrician.

For DC fans (e.g., computer fans), the 5V relay can directly control them.

Installation Steps

  1. Assemble circuit: Build on breadboard and test.
  2. Connect fan: Wire the fan to the relay (AC wiring requires caution).
  3. Mount PIR sensor: Place sensor at 2m height covering the room.
  4. Enclose electronics: Place Arduino and relay in a safe enclosure.
  5. Power up: Connect power supply.
  6. Test: Walk into room, verify fan turns on. Leave room, wait 5 minutes, verify fan turns off.

Project Extensions

  • Speed control: Add PWM control for variable fan speed (requires triac circuit for AC fans).
  • Window sensor: Add magnetic reed switch to disable fan when window is open.
  • Humidity sensor: Use DHT22 to turn on fan in bathrooms when humidity rises.
  • Smart home integration: Add ESP32 to report fan status and allow remote control.
  • Manual override: Add push button for manual control.

Conclusion

This automatic fan control system ensures ventilation only when needed, saving energy and reducing noise when rooms are empty. It’s perfect for bathrooms, workshops, and home offices.

Leave a Reply

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