Skip to content

Instantly share code, notes, and snippets.

@donrestarone
Created January 19, 2025 19:16
Show Gist options
  • Save donrestarone/e2211040b94e0072e078d5d8ad201c65 to your computer and use it in GitHub Desktop.
Save donrestarone/e2211040b94e0072e078d5d8ad201c65 to your computer and use it in GitHub Desktop.
raspberry pi infrared sensor activated LED 'security light'
#!/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")
@donrestarone
Copy link
Author

donrestarone commented Jan 19, 2025

circuit diagram:
Screen Shot 2025-01-19 at 2 14 54 PM

trim controls on infra sensor

Screen Shot 2025-01-19 at 2 34 53 PM

@donrestarone
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment