PIR & SOUND sensor - Raspberry Pi

The program demonstrates the sound and PIR sensor with raspberry. The sensors give voltage as output which is fed to the configured PIN's of raspberry. When the configured PIN's senses the signal the output is displayed.An LED is also made to blink on PIN 31 if any Motion/ Sound is detected.


import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(31,GPIO.OUT)
GPIO_PIR = 29
GPIO.setup(GPIO_PIR,GPIO.IN)
Current_State = 0
Previous_State = 0
try:
while GPIO.input(GPIO_PIR)==1:
Current_State = 0
print " No sound/Motion"
while True :
Current_State = GPIO.input(GPIO_PIR)
if Current_State==1 and Previous_State==0:
print " Motion detected or sound detected"
GPIO.output(31,GPIO.HIGH)
time.sleep(1)
GPIO.output(31,GPIO.LOW)
Previous_State=1
elif Current_State==0 and Previous_State==1:
print " No sound/ Motion"
Previous_State=0
time.sleep(0.01)

except KeyboardInterrupt:
print " Quit"
GPIO.cleanup()