Table of Contents
Introduction
This project uses the PIR sensor included in the TINKERkit to turn on an alarm and power a bright LED lamp when motion is detected. While the code is reasonably simple, the connections are a little complicated and a USB powered lamp and a modified USB extension cable are required.
Required Parts
- A TINKERplate attached to a preprogrammed Raspberry Pi
- From a TINKERkit
- the mini breadboard
- three male to female jumper wires
- male to male jumpers as required
- the 74HCT125 integrated circuit
- the beeper
- the HC-SR501 PIR Sensor
- The modified USB extender cable from the the previous project - find it here
- A USB powered desk lamp like the one here.
Assembly
PIR Sensors
PIR (Passive InfraRed) sensors are used in a number of devices from motion detecting floodlights to home security systems. And while there's ample documentation for these devices on the internet, we found the tutorial at the LastMinuteEngineers website to be the most useful. We borrowed the image below from their site to explain the purpose of each of the adjustments available on the HC-SR501 PIR sensor included with the TINKERkit:
There are two potentiometers on the board to adjust a couple of parameters:
- Sensitivity– This sets the maximum distance that motion can be detected. It ranges from 3 meters to approximately 7 meters. The topology of your room can affect the actual range you achieve.
- Time– This sets how long that the output will remain HIGH after detection. At minimum it is 3 seconds, at maximum it is 300 seconds or 5 minutes.
We found that both the Time-delay and Sensitivity Adjust settings had to be turned counter clockwise (or anti clockwise for our UK readers) quite a bit while we were developing our code. As shipped, the default setting makes the output stay high for a long, long time which is a little annoying when you're trying to verify the operation of your code. Finally, you may notice that the motion sensors shipped with the TINKERkit do not include the header that allows you to choose between the Repeat/Can't Repeat Trigger option. Instead, our boards are hard wired to continue to output a high signal as long as motion is being detected.
Beeper
This is the first project that uses the beeper. You will soon discover what we discovered: it makes a REALLY annoying sound when enabled.
Connections
For this assembly, it is necessary to use the 74HCT125 integrated circuit as a buffer between the motion sensor and the TINKERplate. Also, the lamp shown actually has a clip on it which we removed from the image to simplify the drawing.
Code
The code is a simple loop that samples the motion detector every 100msec. If motion is detected, the relay driving the lamp is enabled and the beeper is turned on. If no motion is detected both the lamp and the beeper are turn off.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import piplates.TINKERplate as TINK import time TINK.setDEFAULTS(0) #initialize Digital I/O ports TINK.setMODE(0,2,'din') #set port 2 as an input for the motion sensor TINK.setMODE(0,3,'dout') #set port 3 as an output for the siren while(True): motion=TINK.getDIN(0,2) #read motion sensor status if(motion==1): #if motion detected TINK.relayON(0,1) #turn on lamp TINK.setDOUT(0,3) #turn on siren else: #if no motion TINK.relayOFF(0,1) #turn off lamp TINK.clrDOUT(0,3) #turn off siren time.sleep(0.1) #wait 100msec and repeat |
The above code can be downloaded from github by typing the following instructions on the command line:
- git clone https://github.com/pi-plates/TINKERplate-Projects.git
- cd TINKERplate-Projects
- python3 motionAlarm.py