Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code to detect motion on Raspberry Pi with Miniature Reflective Infrared Optical Sensors - ITR20001/T from Adafruit https://www.adafruit.com/product/3930
import logging
import datetime
from time import sleep
from gpiozero import MotionSensor
pir = MotionSensor(4, pull_up=True, sample_rate=200, threshold=0.2)
logging.basicConfig(filename='optical-sensor.log',level=logging.DEBUG)
datetime_set = False
motion_counter = 0
if __name__ == '__main__':
logging.info("Date: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
logging.info("Start Optical Sensor Pin " + str(pir.pin))
while True:
try:
pir.wait_for_motion()
if (pir.motion_detected):
datetime_set = True
else:
datetime_set = False
if (datetime_set):
logging.info("Motion captured: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
sleep(0.1)
except KeyboardInterrupt:
print("Quit")
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.