Created
January 19, 2025 19:16
-
-
Save donrestarone/e2211040b94e0072e078d5d8ad201c65 to your computer and use it in GitHub Desktop.
raspberry pi infrared sensor activated LED 'security light'
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
#!/usr/bin/env python3 | |
######################################################################## | |
# Filename : SenseLED.py | |
# Description : Control led with infrared Motion sensor. | |
# auther : www.freenove.com | |
# modification: 2023/05/11 | |
######################################################################## | |
from gpiozero import LED,MotionSensor | |
import time | |
ledPin = 18 # define ledPin | |
sensorPin = 17 # define sensorPin | |
led = LED(ledPin) | |
sensor = MotionSensor(sensorPin) | |
sensor.wait_for_no_motion() | |
def loop(): | |
# Variables to hold the current and last states | |
currentstate = False | |
previousstate = False | |
while True: | |
# Read sensor state | |
currentstate = sensor.motion_detected | |
# If the sensor is triggered | |
if currentstate == True and previousstate == False: | |
led.on() | |
print("Motion detected!led turned on >>>") | |
# Record previous state | |
previousstate = True | |
# If the sensor has returned to ready state | |
elif currentstate == False and previousstate == True: | |
led.off() | |
print("No Motion!led turned off <<") | |
previousstate = False | |
# Wait for 10 milliseconds | |
time.sleep(0.01) | |
def destroy(): | |
led.close() | |
sensor.close() | |
if __name__ == '__main__': # Program entrance | |
print ('Program is starting...') | |
try: | |
loop() | |
except KeyboardInterrupt: # Press ctrl-c to end the program. | |
destroy() | |
print("Ending program") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
circuit diagram:

trim controls on infra sensor