Created
August 27, 2019 20:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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