PIR Motion Sensor (SEN-08630)
Simple PIR motion sensor with digital input interface.
Hello World
Import programPIR_motion_HelloWorld
Simple HelloWorld example program for PIR motion sensor.
Library
Import programPIR_motion_HelloWorld
Simple HelloWorld example program for PIR motion sensor.
Datasheet
https://www.sparkfun.com/datasheets/Sensors/Proximity/SE-10.pdfNotes
The PIR Motion sensor (SEN-08630) is a small, low cost proximity sensor user to detect motion. The sensor is available on Sparkfun for about $10. To setup, simply hook it up to your bed on a breadboard, power up, and wait 1-2 seconds for the sensor to get a "snapshot" of the room. After set up, the output signal will be pulled low if any motion is detected.
Wiring
mbed | PIR Motion |
---|---|
Vu (5V) | + |
GND | GND |
p29 | AL |
Connect Vu (5V) from the mbed to power, GND to GND, and the output to any GPIO pin (p29 is chosen in the table because that's the pin used in the photo below). According to the data sheet, the specified operation voltage is 12V, but the sensor works just fine off of the 5V from the bed. A pull up resistor is needed since the output pin is open collector. This can be done with a external resistor or in software with internal pull ups. The connector as a 0.1" pitch female connector that can easily be connected with some standard header pins or by just using some jumper wires.
Note that the two red jumper wires in the four holes of the board are used only to keep the sensor flat on the bread board.
Note
The color of the wires may vary from device to device and some may not be labeled on the board. Looking at the sensor from the top with the wires on the bottom, as seen in the picture below, the middle wire is GND, the left most wire is power, and the right most wire is the output. This is the general configuration of the wires.
Sample Code
HelloWorld
Here is short example program to get started. The output pin from the device is initialized as a DigitalIn using an internal pull up as opposed to an external resistor. The program starts and gives the sensor 2 seconds to take a snapshot of its surroundings. After, if any motion is detected, LED1 on the mbed is turned on for 2 seconds then turned off unless motion is still detected. The link to import the program to the online compiler is below.
main.cpp
#include "mbed.h" DigitalOut led1(LED1); DigitalIn alarm(p29, PullUp); //internal pull up int main() { wait(2); //Wait for sensor to take snap shot of still room while(1) { if (!alarm){ led1=1; wait(2); } else led1=0; } }
Import programPIR_motion_HelloWorld
Simple HelloWorld example program for PIR motion sensor.
Here is another sample program that uses the InterruptIn library instead of DigitalIn. The output of the sensor is initialized as an interrupt. When motion is detected, the signal from the sensor is pulled low which corresponds to a falling edge. On this falling edge, the flip function is called and LED1 changes state. LED4 flashes continuously to show the code is still running.
main.cpp
#include "mbed.h" //example using InterruptIn with the input from the PIR motion sensors InterruptIn alarm(p11); DigitalOut led(LED1); DigitalOut flash(LED4); void flip() { led = !led; } int main() { alarm.mode(PullUp); wait(2); alarm.fall(&flip); // attach the address of the flip function to the falling edge while(1) { // wait around, interrupts will interrupt flash = !flash; wait(0.25); } }
Import programPIR_Motion_Example
Example program using interrupts with PIR motion sensor.
Security System Demo
A more complicated demo with several peripheral devices to simulate an security system is shown in the video. When motion is detected by the sensor, a message is displayed on the LCD and an alarm sound is played with the speaker.