User Tools

Site Tools


oak:tutorials:pir

This is an old revision of the document!


Oak: PIR Motion Sensor


{{http://digistump.com/wiki/_media/oak/tutorials/pir_sketch.png?600|

If you've been working your way though the starter tutorials to get here, you've found that there are many different kinds of sensors readily available for your Oak, each with their own benefits. The PIR sensor (Passive Infrared Sensor) works by detecting infrared (invisible radiant) energy, aka heat. These are most commonly knows as the security sensor tucked up inside the corners inside homes to detect intruders.

Components used

Part Quantity Identification
Oak with soldered headers 1
PIR HC-SR501 1
Jumper Wires 3

Concepts

The take away from this tutorial is to thoroughly research the sensors you are interested in for their compatibility.

My particular HC-SR501 states that it operates with 4.5 - 20v. However taking a deeper look, the chipset's datasheet states it runs on 3.3v. The 5v input goes to the board's power regulator, which shifts it to 3.3v. There is a bypass pin that allows one to power the sensor directly with 3.3v. This is why the power jumper does not go to the bottom sensor input. See the pictures below for more detail.

Circuit

3 connections total from the Sensor to the Oak, the VCC, GND, and Output. The power from the Oak is going to the 3.3v pin NOT the 5v pin that is beside the Output pin.

  • =Connect the 3.3 vcc pin to the Oak VCC=
  • Connect GND pin to the Oak GND
  • Connect the Output pin to Oak pin #2

NOTE The schematic reflects the location of the pins with the sensor FACING you due to the model showing the sensor face.


{{http://digistump.com/wiki/_media/oak/tutorials/pir_sketch.png?600|

Here are picture of the actual setup. The 1st is a closeup of the rear of the sensor showing the 3.3v connection. The 2nd is of course the entire setup.


{{http://digistump.com/wiki/_media/oak/tutorials/pir_sensor_back2.jpg?600|


{{http://digistump.com/wiki/_media/oak/tutorials/pir_sensor_hook-up.jpg?600|

Code

Again, if you've been doing the starter tutorials, this code should be 2nd nature to you. The lines that aren't typical from the starter tutorials have been commented. The onboard LED is used to signal at the device that motion was detected.

int calibrationTime = 30;        
long unsigned int lowIn;         
long unsigned int pause = 5000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 2; 
int ledPin = 1;


void setup(){
  
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
    for(int i = 0; i < calibrationTime; i++){
      delay(1000);
      }
    delay(50);
  }

void loop(){

     if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH); 
       if(lockLow){  
         lockLow = false;            
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  

       if(takeLowTime){
        lowIn = millis();      
        takeLowTime = false;      
        }

       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;                        
           delay(50);
           }
       }
  }

Conclusion

With the various devices available, with varying voltages, it's not uncommon for manufacturers to market and build to a specific voltage only. This doesn't mean that it won't work, sometimes with minor modifications, with something else. If your up for it, do further research to see what you can do with some tweeking, at your own risk, and the risk of your equipment of course.

The PIR sensors are used of course as part of security systems, as well as the automatic room lights. What project will you use a PIR sensor for? Write one up and post it on the digistump Oak forums.

oak/tutorials/pir.1471011664.txt.gz · Last modified: 2016/08/12 07:21 by brohan