- # http://www.rototron.info/using-a-motion ... pberry-pi/
# Set up input pin
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Set up LED output
GPIO.setup(20, GPIO.OUT)
# Callback function to run when motion detected
def motionSensor(channel):
lcd.clear()
GPIO.output(20, GPIO.LOW)
if GPIO.input(21): # True = Rising
global counter
counter += 1
lcd.message('Motion Detected\n{0}'.format(counter))
GPIO.output(20, GPIO.HIGH)
# add event listener on pin 21
GPIO.add_event_detect(21, GPIO.BOTH, callback=motionSensor, bouncetime=300).
counter = 0
try:
while True:
sleep(1) # wait 1 second
finally: # run on exit
GPIO.cleanup() # clean up
print "All cleaned up."
Code: Select all
pi@raspberrypi2:~/i2c_lcd20x4/motion $ sudo python motion.py
Traceback (most recent call last):
File "motion.py", line 3, in <module>
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
NameError: name 'GPIO' is not defined