SparkFun PIR Breakout Hookup Guide

Pages
Contributors: El Duderino, Englandsaurus
Favorited Favorite 0

Arduino Examples

Now that our SparkFun PIR Breakout circuit is assembled it's time to upload some code to our microcontroller to interact with the sensor. We'll cover three examples in this section to demonstrate how to read the signal, stabilize it and use it as an external interrupt to trigger events on a microcontroller.

Example 1 - Simple Read with Debounce

This quick and dirty example monitors the PIR output signal on D2 and uses the built-in LED on the SparkFun RedBoard as a visual indicator whenever the PIR detects an object in it's field of view.

Copy the code below into a blank Arudino sketch, open the Tools menu to select your board (in our case, Arduino Uno) and correct Port and click the "Upload" button:

language:c
#define PIR_PIN 2   // PIR output on D2
#define LED_PIN  13  // LED to illuminate on motion
#define DEBOUNCE_TIME 750


void setup() 
{
  Serial.begin(115200); 
  // Set the PIR Output signal as an input for the microcontroller:
  pinMode(PIR_PIN, INPUT);

  // Configure the LED pin as an output
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW); // Turn the LED off

  // Wait for 30 seconds for the PIR to stabilize after power on:
  Serial.println("Waiting 30 Seconds while PIR warms up");
  for (uint8_t seconds = 0; seconds < 30; seconds++)
  {
    Serial.println(seconds);
    delay(1000);
  }
  Serial.println("PIR Warmed up.");
}

void loop() 
{
  // The PIR's output is active high
  int motionStatus = digitalRead(PIR_PIN);

  // If motion is detected, turn the onboard LED on and print an object was detected:
  if (motionStatus == HIGH) 
  {
    Serial.println("Object Detected!");
    digitalWrite(LED_PIN, HIGH);
    delay(DEBOUNCE_TIME);
  }
  else // Otherwise turn the LED off and print All Clear:
  {
    Serial.println("All clear!");
    digitalWrite(LED_PIN, LOW);
    delay(DEBOUNCE_TIME);
  }
}

After uploading, open the serial monitor and set your baud to 115200 to see the serial data. The code will print out over serial any time the PIR detects motion in it' field of view and the D13 LED should illuminate.

Example 2 - Interrupt

The Interrupt Example shows how to set up the output signal from the PIR as an external interrupt to trigger an event on your microcontroller. This is particularly helpful for applications where you do not want to constantly poll the digital pin the PIR's output is connected to so you can run other loops in the background.

As we mentioned in the Hardware Assembly, this example assumes a SparkFun RedBoard/Arduino Uno is used and uses D2 as the interrupt pin. If you are using a different microcontroller, adjust the PIR_PIN definition to an interrupt-capable pin.

Copy the code below into a blank Arudino sketch, open the Tools menu to select your board (in our case, Arduino Uno) and correct Port and click the "Upload" button:

language:c
#define PIR_PIN 2 //Connect the output of the PIR to this pin
#define DEBOUNCE_TIME 750

bool pirStatus = false;
bool lastPirStatus = pirStatus;

void interruptRoutine() {
  pirStatus = digitalRead(PIR_PIN);
}

void setup() {
  Serial.begin(115200);
  pinMode(PIR_PIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(PIR_PIN), interruptRoutine, CHANGE);

    // Wait for 30 seconds for the PIR to st abilize after power on:
  Serial.println("Waiting 30 Seconds while PIR warms up");
  for (uint8_t seconds = 0; seconds < 30; seconds++)
  {
    Serial.println(seconds);
    delay(1000);
  }
  Serial.println("PIR Warmed up. Starting readings");
}

void loop() {
  if (pirStatus != lastPirStatus) {
    if (pirStatus)
    {
      Serial.println("Object Detected");
    }
    else
    {
      Serial.println("Object Removed");
    }
    lastPirStatus = pirStatus;
  }
  delay(DEBOUNCE_TIME);
}

From here, you can modify the code so the interrupt event triggers whatever behavior you would like. If you find the interrupt is firing too often, modify the code to trigger only on detected events or by modifying the interrupt type to be either RISING or FALLING.