Creation repo sue RPi3
This commit is contained in:
35
pir/pir.py
Executable file
35
pir/pir.py
Executable file
@@ -0,0 +1,35 @@
|
||||
# MBTechWorks.com 2017
|
||||
# Use an HC-SR501 PIR to detect motion (infrared)
|
||||
|
||||
#!/usr/bin/python
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
|
||||
GPIO.setmode(GPIO.BOARD) #Set GPIO to pin numbering
|
||||
pir = 8 #Assign pin 8 to PIR
|
||||
led = 10 #Assign pin 10 to LED
|
||||
GPIO.setup(pir, GPIO.IN) #Setup GPIO pin PIR as input
|
||||
GPIO.setup(led, GPIO.OUT) #Setup GPIO pin for LED as output
|
||||
print ("Sensor initializing . . .")
|
||||
time.sleep(2) #Give sensor time to startup
|
||||
print ("Active")
|
||||
print ("Press Ctrl+c to end program")
|
||||
|
||||
try:
|
||||
while True:
|
||||
if GPIO.input(pir) == True: #If PIR pin goes high, motion is detected
|
||||
print ("Motion Detected!")
|
||||
GPIO.output(led, True) #Turn on LED
|
||||
time.sleep(4) #Keep LED on for 4 seconds
|
||||
GPIO.output(led, False) #Turn off LED
|
||||
time.sleep(0.1)
|
||||
|
||||
except KeyboardInterrupt: #Ctrl+c
|
||||
pass #Do nothing, continue to finally
|
||||
|
||||
finally:
|
||||
GPIO.output(led, False) #Turn off LED in case left on
|
||||
GPIO.cleanup() #reset all GPIO
|
||||
print ("Program ended")
|
||||
|
||||
43
pir/pir2.py
Normal file
43
pir/pir2.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# MBTechWorks.com 2017
|
||||
# Use an HC-SR501 PIR to detect motion (infrared)
|
||||
|
||||
#!/usr/bin/python
|
||||
|
||||
import RPi.GPIO as GPIO
|
||||
import time, datetime
|
||||
|
||||
|
||||
GPIO.setmode(GPIO.BOARD) #Set GPIO to pin numbering
|
||||
pir = 8 #Assign pin 8 to PIR
|
||||
led = 10 #Assign pin 10 to LED
|
||||
GPIO.setup(pir, GPIO.IN) #Setup GPIO pin PIR as input
|
||||
GPIO.setup(led, GPIO.OUT) #Setup GPIO pin for LED as output
|
||||
print ("Sensor initializing . . .")
|
||||
time.sleep(2) #Give sensor time to startup
|
||||
print ("Active")
|
||||
print ("Press Ctrl+c to end program")
|
||||
|
||||
# Function to create new Filename from date and time
|
||||
def getFileName():
|
||||
return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.jpg")
|
||||
|
||||
try:
|
||||
while True:
|
||||
if GPIO.input(pir) == True: #If PIR pin goes high, motion is detected
|
||||
print ("Motion Detected!")
|
||||
GPIO.output(led, True) #Turn on LED
|
||||
time.sleep(4) #Keep LED on for 4 seconds
|
||||
GPIO.output(led, False) #Turn off LED
|
||||
time.sleep(0.1)
|
||||
|
||||
filename = getFileName()
|
||||
print(filename)
|
||||
|
||||
except KeyboardInterrupt: #Ctrl+c
|
||||
pass #Do nothing, continue to finally
|
||||
|
||||
finally:
|
||||
GPIO.output(led, False) #Turn off LED in case left on
|
||||
GPIO.cleanup() #reset all GPIO
|
||||
print ("Program ended")
|
||||
|
||||
Reference in New Issue
Block a user