OK, I dug deep into my parts bin and found another Parallax part. The Passive Infrared Motion Sensor, or PIR sensor. This is another very simple device that you can connect up to your B+ (or any other Raspberry Pi devices).
The theory behind the PIR sensor is that is is a pyroelectric device, this means that it can detect small changes in the radiant heat of objects in the sensors vicinity, when this occurs the output of the sensor will flip from low to high, which is something that you can detect.
The device is fitted with a Fresnel lens that focuses the infrared signals onto the sensing element.
The version of the device that I have is Parallax Part #555-28027, this device, once again, is safe to connect directly to the Raspberry Pi as it only outputs 3.3v. This is very important when selecting sensors for the Raspberry Pi as signals greater than 3.3 volts can damage the Pi.
So the first thing to do is to ensure that you have the python gpio libraries installed, this can be done as follows:
You just need to run the following commands at the command line
sudo apt-get update sudo apt-get install python-dev sudo apt-get install python-rpi.gpio
If you are not sure whether those things are installed just run them, it will either download the latest version or tell you that you have the latest version already installed. It may prompt you to answer 'Y' to confirm the installations.
Next, shutdown your pi and power it off.
Connect the + pin on the PIR sensor to the Raspberry Pi pin 2, the - Pin to the Raspberry Pi pin 6, and the Out pin to the Raspberry Pi Pin 11 (the pins are numbered in an alternating fashion, with pin 1 being closest to the red power LED, odd numbered pins are in one row, even numbers are in the next row).
Thats it!!! Just 3 pins!!!
Once the devices are connected you can power up the Raspberry Pi and dowload the attached code, pir.py, and perform the following command in the directory where you downloaded pir.py
chmod 755 pir.py
This should only be necessary right after you download the file, it sets the permissions of the file to allow it to be executed.
To run the file simply type the following in the same command window:
The sudo command is necessary to execute the command as the super user ensuring you have adequate permissions to access the GPIO pins. The ./ in the command simply indicates to the command window that the pir.py program is in the current directory.
The code is listed below:
#!/usr/bin/python import time import RPi.GPIO as GPIO # Use board based pin numbering GPIO.setmode(GPIO.BOARD) pirPin = 11 # setup pirPin as input GPIO.setup(pirPin, GPIO.IN) while True: if GPIO.input(pirPin): print("ACK!!! Someone's here!!!") time.sleep(0.5)
As you can see, it is very simple code, all it will do is print the message "ACK!!! Someone's here!!!" whenever the sensor determines someone is in the room. You can point the device towards the door and call someone into the room, when they appear in the sensors range the message will be printed to the screen.
What can you do with your new found knowledge? Well I have seen PIR sensors employed as power saving devices on vending machines. The external lights are turned off until the sensor determines someone is close enough to the vending machine to purchase something, then the front panel will light up telling you it is time to purchase a Coke.
In addition it could be used to signal when someone enters a room, or to control a nightlight when it senses someone is in the hallway, There are many uses for a Passive Infrared Motion Sensor.
pir.py.zip 328 bytes