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.
Components and supplies
Axial Fan, 12 VDC
Custom PCB
Linear Regulator (7805)
IRF 530 N
Gravity: Analog LM35 Temperature Sensor For Arduino
Arduino UNO
Resistor 100 ohm
PIR Motion Sensor (generic)
Tools and machines
Solder Wire, Lead Free
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Scissor, Electrician
Hot glue gun (generic)
Soldering iron (generic)
Project description
Code
Code
arduino
1#include <avr/io.h> 2 3//the time when the sensor outputs a low impulse 4long unsigned int lowIn; 5 6//the amount of milliseconds the sensor has to be low 7//before we assume all motion has stopped 8long unsigned int pause = 500; 9 10boolean lockLow = true; 11boolean takeLowTime; 12 13const int pSuhu = A0; //inisialisasi pin to control LM35 pin A0 14float suhu, outputlm, adc; //tipe data yang memungkinkan memuat angka desimal 15int pirPin = 12; //digital pin connected to the PIR's output 16int pirPos = 13; //connects to the PIR's 5V pin 17int kipas = 3; 18 19void setup(){ 20 Serial.begin(9600); //begins serial communication 21 pinMode(pirPin, INPUT); 22 pinMode(pirPos, OUTPUT); 23 pinMode(kipas,OUTPUT); 24 digitalWrite(pirPos, HIGH); 25 pinMode(pSuhu, INPUT); // set pSuhu(LM35) to be INPUT 26 27 while (digitalRead(pirPin) == HIGH) { 28 delay(500); 29 Serial.print("."); 30 } 31 Serial.println("SENSOR ACTIVE"); 32} 33void lm35(){ 34 adc = analogRead(pSuhu); //save nilai from LM35 to variabel nilaiLM35 35 suhu = adc / 2.0479; 36 outputlm=adc*4.883; 37 if (suhu >= 25) 38 { 39 analogWrite(kipas,50); 40 Serial.println("Kecepatan=50"); 41 } 42 if (suhu >= 30) 43 { 44 analogWrite(kipas,100); 45 Serial.println("Kecepatan=100"); 46 } 47 if (suhu >= 35) 48 { 49 analogWrite(kipas,150); 50 Serial.println("Kecepatan=150"); 51 } 52 if (suhu >= 40) 53 { 54 analogWrite(kipas,200); 55 Serial.println("Kecepatan=200"); 56 } 57 if (suhu >= 45) 58 { 59 analogWrite(kipas,255); 60 Serial.println("Kecepatan=255"); 61 } 62 } 63void loop(){ 64 if(digitalRead(pirPin) == HIGH){ //if the PIR output is HIGH, turn servo 65 lm35(); 66 if(lockLow){ 67 //makes sure we wait for a transition to LOW before further output is made 68 lockLow = false; 69 Serial.println("---"); 70 Serial.print("motion detected at "); 71 Serial.print(millis()/1000); 72 Serial.println(" sec"); 73 delay(50); 74 } 75 takeLowTime = true; 76 } 77 78 if(digitalRead(pirPin) == LOW){ 79 digitalWrite(kipas,LOW); 80 if(takeLowTime){ 81 lowIn = millis(); //save the time of the transition from HIGH to LOW 82 takeLowTime = false; //make sure this is only done at the start of a LOW phase 83 } 84 85 //if the sensor is low for more than the given pause, 86 //we can assume the motion has stopped 87 if(!lockLow && millis() - lowIn > pause){ 88 //makes sure this block of code is only executed again after 89 //a new motion sequence has been detected 90 lockLow = true; 91 Serial.print("motion ended at "); //output 92 Serial.print((millis() - pause)/1000); 93 Serial.println(" sec"); 94 delay(50); 95 } 96 } 97 delay(1000); 98 Serial.print(", Suhu: "); 99 Serial.print(suhu); 100 Serial.println(); //send data ASCII + CR,LF (kode enter) 101 delay(1000); 102 Serial.print(", ADC: "); 103 Serial.print(adc); 104 Serial.println(); //send data ASCII + CR,LF (kode enter) 105 delay(1000); 106} 107
Code
arduino
1#include <avr/io.h> 2 3//the time when the sensor outputs a low impulse 4long 5 unsigned int lowIn; 6 7//the amount of milliseconds the sensor has to 8 be low 9//before we assume all motion has stopped 10long unsigned int pause = 11 500; 12 13boolean lockLow = true; 14boolean takeLowTime; 15 16const int pSuhu 17 = A0; //inisialisasi pin to control LM35 pin A0 18float suhu, outputlm, adc; //tipe 19 data yang memungkinkan memuat angka desimal 20int pirPin = 12; //digital 21 pin connected to the PIR's output 22int pirPos = 13; //connects to the 23 PIR's 5V pin 24int kipas = 3; 25 26void setup(){ 27 Serial.begin(9600); //begins 28 serial communication 29 pinMode(pirPin, INPUT); 30 pinMode(pirPos, OUTPUT); 31 32 pinMode(kipas,OUTPUT); 33 digitalWrite(pirPos, HIGH); 34 pinMode(pSuhu, INPUT); 35 // set pSuhu(LM35) to be INPUT 36 37 while (digitalRead(pirPin) == HIGH) { 38 39 delay(500); 40 Serial.print("."); 41 } 42 Serial.println("SENSOR 43 ACTIVE"); 44} 45void lm35(){ 46 adc = analogRead(pSuhu); //save nilai from 47 LM35 to variabel nilaiLM35 48 suhu = adc / 2.0479; 49 outputlm=adc*4.883; 50 51 if (suhu >= 25) 52 { 53 analogWrite(kipas,50); 54 Serial.println("Kecepatan=50"); 55 56 } 57 if (suhu >= 30) 58 { 59 analogWrite(kipas,100); 60 Serial.println("Kecepatan=100"); 61 62 } 63 if (suhu >= 35) 64 { 65 analogWrite(kipas,150); 66 Serial.println("Kecepatan=150"); 67 68 } 69 if (suhu >= 40) 70 { 71 analogWrite(kipas,200); 72 Serial.println("Kecepatan=200"); 73 74 } 75 if (suhu >= 45) 76 { 77 analogWrite(kipas,255); 78 Serial.println("Kecepatan=255"); 79 80 } 81 } 82void loop(){ 83 if(digitalRead(pirPin) == HIGH){ //if the PIR 84 output is HIGH, turn servo 85 lm35(); 86 if(lockLow){ 87 //makes 88 sure we wait for a transition to LOW before further output is made 89 lockLow 90 = false; 91 Serial.println("---"); 92 Serial.print("motion 93 detected at "); 94 Serial.print(millis()/1000); 95 Serial.println(" 96 sec"); 97 delay(50); 98 } 99 takeLowTime = true; 100 } 101 102 103 if(digitalRead(pirPin) == LOW){ 104 digitalWrite(kipas,LOW); 105 if(takeLowTime){ 106 107 lowIn = millis(); //save the time of the transition from HIGH 108 to LOW 109 takeLowTime = false; //make sure this is only done at the start 110 of a LOW phase 111 } 112 113 //if the sensor is low for more than the given 114 pause, 115 //we can assume the motion has stopped 116 if(!lockLow && millis() 117 - lowIn > pause){ 118 //makes sure this block of code is only executed again 119 after 120 //a new motion sequence has been detected 121 lockLow = true; 122 123 Serial.print("motion ended at "); //output 124 125 Serial.print((millis() - pause)/1000); 126 Serial.println(" sec"); 127 128 delay(50); 129 } 130 } 131 delay(1000); 132 Serial.print(", Suhu: "); 133 134 Serial.print(suhu); 135 Serial.println(); //send data ASCII + CR,LF (kode enter) 136 137 delay(1000); 138 Serial.print(", ADC: "); 139 Serial.print(adc); 140 Serial.println(); 141 //send data ASCII + CR,LF (kode enter) 142 delay(1000); 143} 144
Downloadable files
Automatic Fan with PIR Sensor and Arduino Sensor
Automatic Fan with PIR Sensor and Arduino Sensor
Automatic Fan with PIR Sensor and Arduino Sensor
Automatic Fan with PIR Sensor and Arduino Sensor
Documentation
Design with software Sketch Up
Design with software Sketch Up
Design with software Sketch Up
Design with software Sketch Up
Comments