Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Jamaluddin
Published

Automatic Fan with PIR Sensor and LM35 Sensor

This project uses a PIR sensor to turn a fan ON/oFF and an LM35 sensor to control the speed of the fan with PWM.

BeginnerProtip31,638
Automatic Fan with PIR Sensor and LM35 Sensor

Things used in this project

Hardware components

PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Gravity: Analog LM35 Temperature Sensor For Arduino
DFRobot Gravity: Analog LM35 Temperature Sensor For Arduino
×1
IRF 530 N
×1
Linear Regulator (7805)
Linear Regulator (7805)
×1
Resistor 100 ohm
Resistor 100 ohm
×1
Axial Fan, 12 VDC
Axial Fan, 12 VDC
×1
Custom PCB
Custom PCB
×1
Arduino UNO
Arduino UNO
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Scissor, Electrician
Scissor, Electrician
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Custom parts and enclosures

Design with software Sketch Up

Schematics

Automatic Fan with PIR Sensor and Arduino Sensor

Code

Code

Arduino
#include <avr/io.h>

//the time when the sensor outputs a low impulse
long unsigned int lowIn;        

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 500; 

boolean lockLow = true;
boolean takeLowTime; 

const int pSuhu = A0; //inisialisasi pin to control LM35 pin A0
float suhu, outputlm, adc; //tipe data yang memungkinkan memuat angka desimal
int pirPin = 12;            //digital pin connected to the PIR's output
int pirPos = 13;           //connects to the PIR's 5V pin
int kipas = 3;

void setup(){
  Serial.begin(9600);    //begins serial communication
  pinMode(pirPin, INPUT);
  pinMode(pirPos, OUTPUT);
  pinMode(kipas,OUTPUT);
  digitalWrite(pirPos, HIGH);
  pinMode(pSuhu, INPUT); // set pSuhu(LM35) to be INPUT

  while (digitalRead(pirPin) == HIGH) {
    delay(500);
    Serial.print(".");     
  }
  Serial.println("SENSOR ACTIVE");
}
void lm35(){
  adc = analogRead(pSuhu); //save nilai from LM35 to variabel nilaiLM35
  suhu = adc / 2.0479;
  outputlm=adc*4.883;
  if (suhu >= 25)
  {
    analogWrite(kipas,50);
    Serial.println("Kecepatan=50");
  }
  if (suhu >= 30)
   {
    analogWrite(kipas,100); 
    Serial.println("Kecepatan=100");
  }
  if (suhu >= 35)
   {
    analogWrite(kipas,150); 
    Serial.println("Kecepatan=150");
  }
  if (suhu >= 40)
   {
    analogWrite(kipas,200); 
    Serial.println("Kecepatan=200");
  }
  if (suhu >= 45)
   {
    analogWrite(kipas,255); 
    Serial.println("Kecepatan=255");
  }  
 }
void loop(){
  if(digitalRead(pirPin) == HIGH){  //if the PIR output is HIGH, turn servo
    lm35();
    if(lockLow){ 
      //makes sure we wait for a transition to LOW before further output is made
      lockLow = false;           
      Serial.println("---");
      Serial.print("motion detected at ");
      Serial.print(millis()/1000);
      Serial.println(" sec");
      delay(50);
    }        
    takeLowTime = true;
  }

  if(digitalRead(pirPin) == LOW){      
    digitalWrite(kipas,LOW);
    if(takeLowTime){
      lowIn = millis();             //save the time of the transition from HIGH to LOW
      takeLowTime = false;    //make sure this is only done at the start of a LOW phase
    }
   
    //if the sensor is low for more than the given pause,
    //we can assume the motion has stopped
    if(!lockLow && millis() - lowIn > pause){
      //makes sure this block of code is only executed again after
      //a new motion sequence has been detected
      lockLow = true;                       
      Serial.print("motion ended at "); //output
      Serial.print((millis() - pause)/1000);
      Serial.println(" sec");
      delay(50);
    }
  }
  delay(1000);
  Serial.print(", Suhu: ");
  Serial.print(suhu);
  Serial.println(); //send data ASCII + CR,LF (kode enter)
  delay(1000); 
  Serial.print(", ADC: ");
  Serial.print(adc);
  Serial.println(); //send data ASCII + CR,LF (kode enter)
  delay(1000); 
}

Credits

Jamaluddin
0 projects • 12 followers

Comments