User Tools

Site Tools


Oak: PIR Motion Sensor


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. Let's take this sensor one step further and incorporate it into the Internet of Things (IoT),and expand the reach of our Particle with

Components used

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


Connecting to other products and apps

In other tutorials, we've been able to publish items to your Particle account. This is an easy 1st step in branching your Oak into the wiles of the internet. From here, you can expand your Oak's reach and productivity far and wide across the globe. For this exercise you'll create a free acount by clicking the link. This is going to enable you to connect your Oak / Particle account to many other apps and websites. Go ahead and follow the steps below to get everything set up. In addition to your existing particle account, and your ifttt account, we will be using Gmail. If you don't have one (they are free as well) I recommend getting one, if for nothing else then using it as you experiment with your Oak.

Process for ifttt:

1.) Create your free ifttt account. Once you have logged in, from the dropdown menu at the top, select “Create” You will be presented with “IfTHISthenTHAT”. Click on the word “this”

2.) Select your Channel. You will be prompted to select your trigger channel. In the search box enter “Particle” and select it, then log in to your Particle account.


Next choose the option “New Event Published”.


The last step in our Trigger process is to tell ifttt exactly what to listen for. In our program we'll be using the Event Name “MOTION SENSOR”, and the Events Contents of “Motion detected”. These names must match exactly. Finally choose which device of your you will be listening on. This is a clue that we can publish, and then listen for a number of items at once, as well as multiple Oaks.


3.) We finish our ifttt setup by specifying what action to perform by clicking on the word “that” that appears. For your action channel, this time choose Gmail, again follow the prompts to enable your Gmail account with ifttt.



Then tell ifttt specifically what to do in Gmail when it hears the event from particle. In this case we will simply have it send an email to ourselves letting us know that motion was detected.


Press Create. Doing so automatically starts ifttt listening for this specific event on Particle. Nothing will happen of course until we have our program running on the Oak. I recommend that you keep this window open (or bookmarked if you aren't going to finish this tutorial straight away), so that you can easily stop this “recipe” once we have everything working.

Research Your Sensors

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.


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.


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.




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.

The take away from this will be you creating a free account, and connecting it to your Particle account.

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++){


void loop(){

  if(digitalRead(pirPin) == HIGH){
    digitalWrite(ledPin, HIGH);
    Particle.publish("MOTION SENSOR", "Motion detected");
      lockLow = false;            
    takeLowTime = true;

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

      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;                        



Remember the page with our recipe? Before you flash the program to your Oak, I highly recommend that you have this recipe page open in your browser, your Particle log page, as well as your Gmail page, so that you can see everything working together.

Flash your Oak, and you will shortly see your Particle logs showing the information that you've published, and if your recipe is active, you will see an email (or a steady stream) arrive in your Gmail inbox. To halt the emails, simply turn off your recipe on ifttt.


You will most likely need to make many adjustments to tweak this in. You can most likely adjust the sensitivity of your PIR sensor, for activity, for depth of field, as well as the timeout of the sensor. You can also adjust the timing of the reporting within the program. The current set up should fire off a steady stream of emails to show you that it's working in short order, unless of course you are in a room by yourself, with the sensor pointing away from you, and you aren't moving at all.


Connecting your Oak to Particle, (and thereby OakTerm) gives you a number of options, the ability to view what your Oak is doing, program your Oak through the Particle IDE, and event put it into Config Mode via OakTerm. Using a system like quickly, and easily extends the capability of your Oak.

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.txt · Last modified: 2017/01/16 22:53 by weaslyd