In this lesson we show the basic framework for an Alarm System with operator control through a keypad, and output to an LCD screen. We incorporate a PIR sensor to detect motion. In the next lesson we will add an audible alarm. For your convenience, the code developed in the above video is included below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) PIRpin=12 GPIO.setup(PIRpin,GPIO.IN) import LCD1602 import KPLIB from time import sleep import threading myPad=KPLIB.keypad(retChar='D') LCD1602.init(0x27,1) myString='' pwd='1234' def readKP(): global myString while myString != '*': myString=myPad.readKeypad() sleep(.5) readThread=threading.Thread(target=readKP,) readThread.daemon=True readThread.start() while myString != '*': CMD=myString if CMD=='A'+pwd: LCD1602.write(0,0,'Armed ') moveVal=GPIO.input(PIRpin) if moveVal==1: LCD1602.write(0,1,'Intruder Alert') if moveVal==0: LCD1602.write(0,1,'All Clear ') if CMD=='B'+pwd: LCD1602.write(0,0,'UnArmed ') LCD1602.write(0,1,' ') if CMD=='C'+pwd: LCD1602.write(0,0,'Password? ') LCD1602.write(0,1,' ') while myString=='C'+pwd: pass pwd=myString LCD1602.write(0,0,pwd+' ') sleep(2) LCD1602.write(0,0,' ') LCD1602.clear() sleep(1) GPIO.cleanup() LCD1602.clear() print('GPIO Good to Go') |