일시 : 2018.01.10 13:00 ~
내용 : PIR Sensor
//
PIR(Passive Infrared) sensor
물체에서 방출되거나 반사되는 적외선 파장의 움직임에 반응하는 센서
24번 핀을 input으로 설정하여 주기적으로 검사
실습
단순 움직임 감지 - 움직임을 감지하면 감지된 횟수를 출력
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
pir = 24
GPIO.setup(pir, GPIO.IN)
def loop():
cnt = 0
while True:
cur_stat = GPIO.input(pir)
if cur_stat == 1:
cnt += 1
print("%d번 감지됨" % cnt)
try :
loop()
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
pir과 led연동 - 감지될 때마다 LED를 켜줌
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
pir_pin = 24
GPIO.setup(pir_pin, GPIO.IN)
led_pin1 = 14
led_pin2 = 15
GPIO.setup(led_pin1, GPIO.OUT)
GPIO.setup(led_pin2, GPIO.OUT)
def loop():
cnt = 0
while True:
if GPIO.input(pir_pin) == True:
cnt += 1
print("%d번 감지됨" % cnt)
GPIO.output(led_pin1, True)
GPIO.output(led_pin2, True)
time.sleep(0.01)
else:
GPIO.output(led_pin1, False)
GPIO.output(led_pin2, False)
time.sleep(0.01)
try :
loop()
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
pir과 led연동 - 모듈 이용
import RPi.GPIO as GPIO
import time
from led_pir import *
GPIO.setmode(GPIO.BCM)
pir = 24
GPIO.setup(pir, GPIO.IN)
def loop():
cnt = 0
while True:
cur_stat = GPIO.input(pir)
if cur_stat == 1:
cnt += 1
ledOn()
print("%d번 감지됨" % cnt)
time.sleep(0.3)
elif cur_stat == 0:
ledOff()
try :
loop()
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
## led_pir.py
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
led_pin1 = 14
led_pin2 = 15
GPIO.setup(led_pin1, GPIO.OUT)
GPIO.setup(led_pin2, GPIO.OUT)
def ledOn():
GPIO.output(led_pin1, True)
GPIO.output(led_pin2, True)
time.sleep(1)
def ledOff():
GPIO.output(led_pin1, False)
GPIO.output(led_pin2, False)
3초동안 led센서 유지하려면 time.sleep(3)
while True:
if GPIO.input(pir_pin) == True:
cnt += 1
print("%d번 감지됨" % cnt)
GPIO.output(led_pin1, True)
GPIO.output(led_pin2, True)
time.sleep(3)
GPIO.output(led_pin1, False)
GPIO.output(led_pin2, False)
import RPi.GPIO as GPIO
import time
pir_pin=24
led1=14
led2=15
GPIO.setmode(GPIO.BCM)
GPIO.setup(pir_pin, GPIO.IN)
GPIO.setup(led1, GPIO.OUT)
GPIO.setup(led2, GPIO.OUT)
def loop(cnt, time_check_cnt):
cur_val=GPIO.input(pir_pin)
if cur_val == True:
cnt+=1
print("감지됨 : %d" %cnt)
GPIO.output(led1, True)
GPIO.output(led2, True)
time_check_cnt=0
return cnt, time_check_cnt
try:
main_cnt=0
cnt=0
time_check_cnt=0
while True:
main_cnt,time_check_cnt=loop(cnt, time_check_cnt)
cnt=main_cnt
time.sleep(0.1)
time_check_cnt+=1
if (time_check_cnt > 30):
GPIO.output(led1, False)
GPIO.output(led2, False)
time_check_cnt=0
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
'2017 멀티캠퍼스 > Raspberry Pi' 카테고리의 다른 글
#8 라즈베리파이 Piezo 센서 (0) | 2018.01.30 |
---|---|
#7 라즈베리파이 I2C 통신 (0) | 2018.01.14 |
#5 라즈베리파이 GPIO 조이스틱 (0) | 2018.01.14 |
#4 라즈베리파이 DC Motor (0) | 2018.01.14 |
#3 라즈베리파이 GPIO LED (0) | 2018.01.14 |