Tested on:Raspberry Pi 2; Raspberry Pi B+; Python 2.7; Raspbian pre Jessie
Prerequisites:Raspberry Pi with Raspbian Jessie(see steps 1-5 in the Introduction)
Hardware:Passive infrared sensor(PIR), ideally with a 3.3V output

A passive infrared sensor(PIR) lets a Raspberry Pi detect motion

Since PIR sensors generally have a digital output, the setup and usage information of the smoke detector MQ-x tutorial applies as well.
However, a PIR sensor has two analog potentiometers, one for adjusting the outgoing signal duration(probably labeled d) and one for adjusting the sensors sensitivity(probably labeled s):

PIR sensor potentiometers for signal length and sensitivity

PIR sensor potentiometers for signal length and sensitivity

The Raspberry Pi is happy with the minimal signal duration, so only sensitivity matters. For continous motion, the signal stays on. To change that, sensor model-specific jumpers need to be set or soldered. See the technical specification of the sensor for further information.
This specific sensor is used with 3.3V besides it being made for 5V and runs well, but with a reduced sensor range. Building a voltage divider for reducing the sensors digital output to 3.3V solves that.

Run the sensor
Connect the sensors GND pin to pin 6, power to either pin 1 (3.3V) or pin 2 (5V) and the digital output to pin 7 of the Raspberry Pi as counted per board numbering. Then, download and start the sample script with

The script
Click on Full Script to show all of the code.

Now, it’s possible to trigger the light, send a warning per Email or start a camera with night vision, …

Smoke detector MQ-x tutorial: http://www.knight-of-pi.org/digital-sensors-and-the-raspberry-pi-with-the-smoke-detector-mq-x-as-example/
Home automation with rc switches: http://www.knight-of-pi.org/cheap-home-automation-with-a-raspberry-pi-controlling-rc-switches-with-pi-switch/
Email tutorial: http://www.knight-of-pi.org/sending-emails-with-the-raspberry-pi/
PiCamera Tutorial: http://www.knight-of-pi.org/raspberry-pi-camera-and-night-vision-with-pinoir/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


Time limit is exhausted. Please reload CAPTCHA.