Humans and other animals emit heat all the time.
A PIR sensor detects changes in the amount of IR radiation (heat) it receives. When there is a change, then a pulse is triggered. This means that a PIR sensor can detect when a human (or any animal) moves in front of it.
The pulse emitted when a PIR detects motion needs to be amplified, and so it needs to be powered. There are three pins on the PIR; they should be labelled Vcc, Gnd, and Out. If these labels aren't clear, they are sometimes concealed beneath the Fresnel lens (the white cap), which you can temporarily remove to see the pin labels.
- As shown above, the
Vccpin needs attaching to a5Vpin on the Raspberry Pi. - The
Gndpin on the PIR sensor can be attached to any ground pin on the Raspberry Pi. - Lastly, the
Outpin needs to be connected to any of the GPIO pins.
- With your PIR circuit complete, you are now ready to use Scratch to sense motion. Launch the Scratch program by clicking on Menu followed by Programming and selecting Scratch.
Our version of Scratch on Raspbian is extra special. It allows you to access and control the GPIO pins.
-
Click on control in the top-left display. Drag the
block onto the scripts area. -
Drag a
broadcastblock to your scripts area and attach it to the
block. Click on the drop-down menu on the broadcast block and select new.Type
config4inin the message name box. This instruction will tell the Raspberry Pi to set GPIO pin 4 as an input.
-
Press the green flag in the upper-right corner of the Scratch window. This executes the instruction to set GPIO pin 4 as an input.
-
Scratch uses the 'Sensing' blocks to check if there is any input on the GPIO pins. If there is an input, the value of the pin changes from
0to1. As you connected the PIR sensor to GPIO pin 4 of the Pi, we need to monitor that. Click on the drop-down menu on thesensor valueblock and choosegpio4. -
Tick the checkbox to the left of the block to display the pin value on screen.
-
Test the PIR sensor by waving your hand in front of it. When it detects movement, the value on the screen should change from
0to1. -
If the value doesn't change, check that the correct pins are connected.
- Why not try and wire up a buzzer as well, to make a burglar alarm?
- Have a go at making a Santa Detector
Back to Physical computing with Scratch



