When I bought my Raspberry Pi, this is the main thing I wanted to do with it. I wanted to implement low level hardware using the GPIO – and sensors were the components which really interested me. So, motion sensing was the first step I took.
I did a lot of research for this one – components weren’t that cheap. They’re not earth-shatteringly expensive, but they’re the kind of price where you don’t want to be re-buying a lot of stuff if you keep making it set on fire.
Finding a motion sensor which would work with the 3.3v or 5v lines provided by the GPIO on the Raspberry Pi was the first step, followed closely by how best to provide power and receive a signal from it.
Passive Infra-Red (PIR) sensors basically detect temperature changes between sections of a viewing range. This means if a person (a source of heat) moves between two sectors within the sensors field of ‘vision’ – this is detected as movement. PIR sensors are well used in alarm sensors. The great thing about being based on temperature is that they work in the dark and ideally suited for sensing people rather than…curtains in the wind (let’s say) which could cause problems for other, more literal interpretations of ‘motion’ sensors.
The overlap with security systems can be annoying. Search for ‘PIR sensors’ on Maplin’s website and you”ll be fronted with the security system kind and motion activation security lights…none of which are useful for our purpose. I found Rapid had a fairly good selection of PIR sensors which would work within the 3.3v-5v range the Pi can support. I opted for the Panasonic one that they sell. They come in a few varieties, but each has a fairly decent range (2.5-10m).
To get started, I went for the standard (5m) range component (in white – part no. 61-1502). The plan being that if it worked, I might try the 10m one later.
Wiring it up
Someone else had already asked on the Raspberry Pi forum which resistors were best to use for this kind of sensor (which was helpful) – so in the end the circuit looked the same for mine. (NB: they also did a very helpful write-up of their project here)
One slight difference to this diagram – I attached the ‘out’ line to GPIO2 on p1-13. (NB: For a look at the GPIO pins and reading their names, check this resource)
I also left the LED hooked up from my first exercise with GPIO as a separate circuit as follows:
3.3v -> 620 ohm resistor (probably far too high, but it’s all I had in a reasonable range) -> Green LED (~2.1V) -> GPIO 7 (p1-07)
I will update with a breadboard layout diagram (which might be a bit easier to replicate) shortly.
Just to recap, we want the software to take the input signal from the sensor and, if there is movement detected, light the LED.
So here’s the Python script I wrote, using the RPi.GPIO library again:
from time import sleep import RPi.GPIO as GPIO GPIO.setup(13,GPIO.IN) # Pin 13 - input from motion sensor GPIO.setup(7, GPIO.OUT) # Pin 7 - output to LED while 1: move = GPIO.input(13) # Check input from sensor False=no movement, True=movement GPIO.output(7,not(move)) # Turn LED on/off print move sleep(0.5) # Wait 0.5 seconds
Interestingly, I haven’t changed any settings with the GPIO pins – so the way my LED is set up sending ‘False’ turns the LED on and vice versa – hence the not(move) statement.
And that’s it really. The (pretty rubbish) video of the finished thing is here:
I have some close-up photos of the breadboard with the circuit a little clearer – I will update with them soon.
Thanks for reading – hope this is helpful!