How to Build a Smart Fan with PIR Sensor & Temperature Control (Arduino)

Have you ever walked out of a room and realized you left the fan running for hours? Or maybe you’ve woken up freezing in the middle of the night because the AC was on full blast? We’ve all been there.

As an engineer, I hate wasted energy. So, I decided to build a smart fan that thinks for itself. It turns on when someone enters the room, turns off when the room is empty, and even adjusts its speed based on the room temperature.

In this comprehensive guide, I will show you exactly how to build a smart fan using an ATmega8535 microcontroller, a PIR motion sensor, and an LM35 temperature sensor. This is the perfect weekend project for anyone looking to dive into home automation and IoT.

How to build a smart fan with PIR sensor and temperature control
How to build a smart fan with PIR sensor and temperature control.

Why Build a Smart Fan?

This isn’t just a fun project; it solves real-world problems:

  • Energy Efficiency: It only runs when someone is in the room, saving electricity.
  • Convenience: No more getting up to adjust the fan speed. It adjusts automatically based on the heat.
  • Learning Opportunity: It’s a fantastic way to learn about microcontrollers, analog sensors, and motor drivers.

Hardware Requirements

Here is the exact bill of materials (BOM) I used for this project:

  • Microcontroller: ATmega8535 (You can also use an Arduino Uno).
  • Motion Sensor: PIR (Passive Infrared) Sensor.
  • Temperature Sensor: LM35.
  • Motor Driver: L293D (H-Bridge).
  • Display: 16×2 LCD (HD44780 compatible).
  • Actuator: 12V DC Fan.
  • Miscellaneous: Resistors, capacitors, crystal oscillator (4MHz), jumper wires, and a breadboard.

Step 1: Wiring the Circuit

Getting the wiring right is the most critical part. If you wire the motor driver incorrectly, you can burn out your microcontroller. Here is how I connected everything:

1. The Microcontroller (ATmega8535)

I used the internal clock with a 4MHz crystal oscillator connected to XTAL1 and XTAL2. The RESET pin is pulled high via a 10k resistor.

2. The Sensors

  • PIR Sensor: Connect VCC to 5V, GND to Ground, and the Output pin to Port B0 (PB0).
  • LM35 Sensor: Connect VCC to 5V, GND to Ground, and the Vout pin to Port A0 (PA0). Port A on the ATmega8535 has a built-in 10-bit ADC (Analog to Digital Converter), which is perfect for reading the analog voltage from the LM35.

3. The Motor Driver (L293D)

The L293D allows us to control the high-power DC fan using low-power signals from the microcontroller.

  • Input 1 (IN1) and Input 2 (IN2): Connect to Port C0 (PC0) and Port C1 (PC1).
  • Enable 1 (EN1): Connect to Port C2 (PC2). We will use this pin for PWM (Pulse Width Modulation) to control the fan speed.
  • Output 1 (OUT1) and Output 2 (OUT2): Connect to the DC fan.
  • VCC2: Connect to a 12V power supply (for the fan).
  • VCC1: Connect to 5V (for the logic).

4. The LCD Display

I connected the LCD to Port D (PD0 to PD7). This will display the current temperature and the fan’s speed status.

Step 2: The Logic & Flowchart

The magic of this project is in the code. The logic is simple:

  1. Check for Motion: If the PIR sensor detects no movement, the fan stays OFF and the LCD turns off to save power.
  2. Check Temperature: If movement is detected, the system reads the LM35 sensor.
  3. Adjust Speed:
    • If temp is below 28°C: Fan stays OFF (or runs very slowly).
    • If temp is between 28°C and 30°C: Fan runs at SLOW speed.
    • If temp is between 30°C and 32°C: Fan runs at MEDIUM speed.
    • If temp is above 32°C: Fan runs at MAX speed.

Step 3: Writing the Code (C++ / Arduino)

I wrote this code in C using CodeVisionAVR, but I’ve adapted it here so you can easily paste it into the Arduino IDE.

#include <LiquidCrystal.h>

// Pin Definitions
#define PIR_PIN 8
#define LM35_PIN A0
#define MOTOR_PIN1 4
#define MOTOR_PIN2 5
#define PWM_PIN 6 // Must be a PWM-capable pin

LiquidCrystal lcd(12, 11, 7, 3, 2, 1); // Adjust pins based on your wiring

void setup() {
  Serial.begin(9600);
  pinMode(PIR_PIN, INPUT);
  pinMode(MOTOR_PIN1, OUTPUT);
  pinMode(MOTOR_PIN2, OUTPUT);
  pinMode(PWM_PIN, OUTPUT);
  
  lcd.begin(16, 2);
  lcd.print("Smart Fan Init");
  delay(2000);
  lcd.clear();
}

void loop() {
  int motion = digitalRead(PIR_PIN);
  
  if (motion == HIGH) {
    // Read temperature
    int sensorValue = analogRead(LM35_PIN);
    float voltage = sensorValue * (5.0 / 1023.0);
    float temperatureC = voltage * 100.0; // LM35 outputs 10mV per degree Celsius
    
    lcd.setCursor(0, 0);
    lcd.print("Temp: ");
    lcd.print(temperatureC);
    lcd.print(" C   ");
    
    // Control Fan Speed based on Temperature
    digitalWrite(MOTOR_PIN1, HIGH);
    digitalWrite(MOTOR_PIN2, LOW);
    
    if (temperatureC < 28) {
      analogWrite(PWM_PIN, 0); // OFF
      lcd.setCursor(0, 1);
      lcd.print("Fan: OFF      ");
    } 
    else if (temperatureC >= 28 && temperatureC < 30) {
      analogWrite(PWM_PIN, 75); // Slow Speed
      lcd.setCursor(0, 1);
      lcd.print("Fan: SLOW     ");
    } 
    else if (temperatureC >= 30 && temperatureC < 32) {
      analogWrite(PWM_PIN, 150); // Medium Speed
      lcd.setCursor(0, 1);
      lcd.print("Fan: MEDIUM   ");
    } 
    else if (temperatureC >= 32) {
      analogWrite(PWM_PIN, 255); // Max Speed
      lcd.setCursor(0, 1);
      lcd.print("Fan: FAST     ");
    }
  } else {
    // No motion detected
    digitalWrite(MOTOR_PIN1, LOW);
    digitalWrite(MOTOR_PIN2, LOW);
    analogWrite(PWM_PIN, 0);
    
    lcd.clear(); // Turn off LCD to save power
  }
  
  delay(500);
}

Step 4: Testing and Results

After building the circuit and uploading the code, it’s time for the fun part: testing. Here is what I found during my tests:

1. PIR Sensor Range

The PIR sensor is incredibly sensitive. I found that it could detect slow hand movements up to 240 cm (about 7.8 feet) away. If you move fast, it can detect up to 600 cm. However, the most reliable detection range is between 10 cm and 240 cm.

2. Temperature Accuracy

The LM35 sensor is incredibly accurate. When I compared its readings with a digital thermometer, the margin of error was less than 0.1°C.

3. Fan Speed (PWM)

The 12V fan responded perfectly to the PWM signals. By changing the duty cycle from 0 to 255, I could smoothly transition the fan from a gentle breeze to a powerful gust. The LCD accurately displayed the speed (Slow, Medium, Fast) matching the temperature.

What I Would Improve (Lessons Learned)

While the project worked beautifully, I noticed one flaw. The PIR sensor only detects changes in heat. If a person sits perfectly still (like reading a book), the sensor might think the room is empty and turn off the fan.

To fix this in future versions, I would:

  • Add an override switch to keep the fan on manually.
  • Use a mmWave radar sensor instead of PIR, which can detect micro-movements like breathing or typing.

If you’re looking to manage a bigger engineering team or a smart home project, check out our guide on the best project management software for engineering teams.

Final Thoughts on How to Build a Smart Fan

Building a smart fan with a PIR sensor and temperature control is a brilliant way to learn about microcontrollers and automation. It costs less than $30 in components and saves electricity in the long run. Whether you are a student, a hobbyist, or a homeowner looking to automate your space, this project is highly rewarding.

Have you built a smart home project? Let me know in the comments below!

Tinggalkan Komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Scroll to Top