ModMyPi LTD | GPIO & Python (9/9)

von Satoshi Nakamoto

ModMyPi LTD | GPIO & Python (9/9)

On this undertaking you'll discover ways to wire and program a passive infrared sensor.

Issues you will have:

Raspberry Pi + SD Card
Keyboard + Mouse
Monitor + HDMI Cable
Energy Provide
Breadboard
1x Crimson LED
1x Blue LED
2x 330Ω Resistor
5x M/M Jumper Wire
12x M/F Jumper Wire
1x Button
1x Buzzer
1x DS18B20 Temperature Sensor
1x 4k7Ω Resistor
1x 1uF Capacitor
1x Mild Dependent Resistor (LDR)
1x PIR Sensor

Conditions:

Newest model of Rasbian put in in your SD Card
Raspberry Pi setup with a keyboard, mouse and monitor

1. Change the present listing to our gpio_python_code listing:


cd gpio_python_code

2. Begin by making a file for our pir script


contact 9_pir.py

3. Edit the 9_pir.py script utilizing nano 9_pir.py add the next code:


#!/usr/bin/python

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setup(27,GPIO.OUT)

GPIO_PIR = 7

print "PIR Module Check (CTRL-C to exit)"

# Set pin as enter
GPIO.setup(GPIO_PIR,GPIO.IN)

Current_State = 0
Previous_State = 0

strive:
print "Ready for PIR to settle ..."
# Loop till PIR output is 0
whereas GPIO.enter(GPIO_PIR)==1:
Current_State = 0
print " Prepared"
# Loop till customers quits with CTRL-C
whereas True :
# Learn PIR state
Current_State = GPIO.enter(GPIO_PIR)
if Current_State==1 and Previous_State==0:
# PIR is triggered
print " Movement detected!"
# File earlier state
GPIO.output(27,GPIO.HIGH)
sleep(1)
GPIO.output(27,GPIO.LOW)
Previous_State=1
elif Current_State==Zero and Previous_State==1:
# PIR has returned to prepared state
print " Prepared"
Previous_State=0
# Await 10 milliseconds
sleep(0.01)

# clear up gpio pins after we exit the script
besides KeyboardInterrupt:
print " Give up"
# Reset GPIO settings
GPIO.cleanup()

4. Execute your 9_pir.py script


sudo python 9_pir.py

5. Press ctrl+c to exit the script




Source link



Read the full article
Porträt von Satoshi Nakamoto

Satoshi Nakamoto

Zur Person

Satoshi Nakamoto