Devices & Components
Arduino Nano
PIR Motion Sensor (generic)
5v DC Relay
Jumper wires (generic)
Box Enclosure
Prototype PCB
Hardware & Tools
Soldering iron (generic)
Cutting and Drilling Tools
Software & Tools
Arduino IDE
Project description
Code
PIR + Relay
arduino
1/* 2* PIR sensor tester 3*/ 4 5int relayPin = 3; // choose the 6 pin for the relay 7int pirSensorPin = 2; // choose the input pin (for PIR sensor) 8int 9 pirState = true; // we start, assuming no motion detected 10int val = 0; // variable 11 for reading the pin status 12int minimummSecsLowForInactive = 5000; // If the sensor 13 reports low for 14// more than this time, then assume no activity 15long unsigned 16 int timeLow; 17boolean takeLowTime; 18 19//the time we give the sensor to calibrate 20 (10-60 secs according to the datasheet) 21int calibrationTime = 30; 22 23void 24 setup() { 25pinMode(relayPin, OUTPUT); // declare LED as output 26pinMode(pirSensorPin, 27 INPUT); // declare sensor as input 28 29Serial.begin(9600); 30 31//give the 32 sensor some time to calibrate 33Serial.print("calibrating sensor "); 34for(int 35 i = 0; i < calibrationTime; i++){ 36Serial.print("."); 37delay(1000); 38} 39Serial.println(" 40 done"); 41Serial.println("SENSOR ACTIVE"); 42delay(50); 43} 44 45void loop(){ 46 47 48val = digitalRead(pirSensorPin); // read input value 49if (val == HIGH) { 50 // check if the input is HIGH 51 52digitalWrite(relayPin, HIGH); // turn LED 53 ON 54if (pirState) { 55// we have just turned on 56pirState = false; 57Serial.println("Motion 58 detected!"); 59// We only want to print on the output change, not state 60delay(50); 61} 62takeLowTime 63 = true; 64} 65 66else { 67digitalWrite(relayPin, LOW); // turn LED OFF 68 69 70if (takeLowTime){ 71timeLow = millis(); 72takeLowTime = false; 73} 74 75if(!pirState 76 && millis() - timeLow > minimummSecsLowForInactive){ 77pirState = true; 78Serial.println("Motion 79 ended!"); 80delay(50); 81} 82 83} 84} 85
PIR + Relay
arduino
1/* 2* PIR sensor tester 3*/ 4 5int relayPin = 3; // choose the pin for the relay 6int pirSensorPin = 2; // choose the input pin (for PIR sensor) 7int pirState = true; // we start, assuming no motion detected 8int val = 0; // variable for reading the pin status 9int minimummSecsLowForInactive = 5000; // If the sensor reports low for 10// more than this time, then assume no activity 11long unsigned int timeLow; 12boolean takeLowTime; 13 14//the time we give the sensor to calibrate (10-60 secs according to the datasheet) 15int calibrationTime = 30; 16 17void setup() { 18pinMode(relayPin, OUTPUT); // declare LED as output 19pinMode(pirSensorPin, INPUT); // declare sensor as input 20 21Serial.begin(9600); 22 23//give the sensor some time to calibrate 24Serial.print("calibrating sensor "); 25for(int i = 0; i < calibrationTime; i++){ 26Serial.print("."); 27delay(1000); 28} 29Serial.println(" done"); 30Serial.println("SENSOR ACTIVE"); 31delay(50); 32} 33 34void loop(){ 35 36val = digitalRead(pirSensorPin); // read input value 37if (val == HIGH) { // check if the input is HIGH 38 39digitalWrite(relayPin, HIGH); // turn LED ON 40if (pirState) { 41// we have just turned on 42pirState = false; 43Serial.println("Motion detected!"); 44// We only want to print on the output change, not state 45delay(50); 46} 47takeLowTime = true; 48} 49 50else { 51digitalWrite(relayPin, LOW); // turn LED OFF 52 53if (takeLowTime){ 54timeLow = millis(); 55takeLowTime = false; 56} 57 58if(!pirState && millis() - timeLow > minimummSecsLowForInactive){ 59pirState = true; 60Serial.println("Motion ended!"); 61delay(50); 62} 63 64} 65} 66
Downloadable files
Schematics
Schematics

Schematics
Schematics

5v DC Relay and Extension Cord Diagram
5v DC Relay and Extension Cord Diagram

Comments
Only logged in users can leave comments