Skip to content

Instantly share code, notes, and snippets.

@elchingon
Created August 27, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elchingon/0a818fa3e3b6ddf0ee5e639048ccf140 to your computer and use it in GitHub Desktop.
Save elchingon/0a818fa3e3b6ddf0ee5e639048ccf140 to your computer and use it in GitHub Desktop.
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