728x90
[1] GPIO
1. PUSH 버튼
1) PUSH버튼 예제1
- 풀다운 방식으로 연결
push_test1.py
import RPi.GPIO as GPIO
import time
push_btn = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(push_btn, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
try:
while True:
if GPIO.input(push_btn) == GPIO.HIGH:
print("버튼이 눌려짐")
else:
print("버튼이 안 눌려짐")
time.sleep(0.1)
except KeyboardInterrupt:
print("end")
finally:
GPIO.cleanup()
2) PUSH버튼 예제2
- 위의 예제에서 문제로 채터링 문제가 있음
- 아두이노와 달리 디바운싱을 위한 복잡한 작업을 별도로 하지 않고, event 방식으로 해결할 수 있음
push_event_test.py
import RPi.GPIO as GPIO
import time
# button의 상태가 변하면 호출될 callback function을 정의
def mycallback(channel):
print("버튼이 눌려짐")
push_btn = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(push_btn, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Event 방식으로 핀의 상태를 처리 - GPIO.RISING(LOW->HIGH), GPIO.FALLING(HIGH->LOW), GPIO.BOTH
GPIO.add_event_detect(push_btn, GPIO.FALLING, callback=mycallback)
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
print("end")
finally:
GPIO.cleanup()
3) PUSH버튼 예제3
- 풀업 방식으로 연결
- GPIO.wait_for_edge(channel, edge, timeout) 메소드
- 입력핀으로부터 RISING(or FALLING)이 발생할 때까지 timeout에 지정한 시간(ms)만큼 기다렸다가 작업
- ex) GPIO.wait_for_edge(push_btn, GPIO.RISING, timeout=1000)
push_event_test2.py
import RPi.GPIO as GPIO
import time
# button의 상태가 변하면 호출될 callback function을 정의
def mycallback(channel):
print("버튼이 눌려짐")
push_btn = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(push_btn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Event 방식으로 핀의 상태를 처리 - GPIO.RISING(LOW->HIGH), GPIO.FALLING(HIGH->LOW), GPIO.BOTH
GPIO.add_event_detect(push_btn, GPIO.RISING, callback=mycallback)
# GPIO.wait_for_edge() 메소드
# 입력핀으로부터 RISING(or FALLING)이 발생할 때까지 timeout에 지정한 시간(ms)만큼 기다렸다가 작업
# GPIO.wait_for_edge(push_btn, GPIO.RISING, timeout=1000)
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
print("end")
finally:
GPIO.cleanup()
4) LED 예제
led_exam3.py
# led_exam3.py
# led 2개가 번갈아 켜지고 꺼지도록 구현하기
import RPi.GPIO as GPIO
import time
led_pin1 = 23
led_pin2 = 24
led_state1 = GPIO.LOW
led_state2 = GPIO.HIGH
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin1, GPIO.OUT)
GPIO.setup(led_pin2, GPIO.OUT)
try:
while True:
led_state1 = not led_state1
led_state2 = not led_state2
GPIO.output(led_pin1, led_state1)
GPIO.output(led_pin2, led_state2)
time.sleep(1)
except KeyboardInterrupt:
print("end")
finally:
GPIO.cleanup()
5) PUSH 버튼+LED 예제1
- 풀다운 방식 연결
push_led_exam.py
# push_led_exam.py
# push를 누르면 led가 켜지고 push누르면 led가 꺼지도록 작업하기
import RPi.GPIO as GPIO
import time
led_pin = 24
push_btn = 26
led_state = GPIO.LOW
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)
GPIO.setup(push_btn, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def push_btn_callback(channel):
global led_state
led_state = not led_state
GPIO.output(led_pin, led_state)
GPIO.add_event_detect(push_btn, GPIO.RISING, callback=push_btn_callback, bouncetime=100)
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
print("end")
finally:
GPIO.cleanup()
6) PUSH 버튼+LED 예제2
push_led_exam2.py
# push_led_exam2.py
# push버튼 두 개를 이용해서 하나를 누르면 led가 켜지고 다른 push 버튼을 누르면 led가 꺼지도록 작업하기
import RPi.GPIO as GPIO
import time
led_pin = 24
push_btn1 = 19
push_btn2 = 26
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)
GPIO.setup(push_btn1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(push_btn2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def push_btn_callback1(channel):
GPIO.output(led_pin, GPIO.HIGH)
def push_btn_callback2(channel):
GPIO.output(led_pin, GPIO.LOW)
GPIO.add_event_detect(push_btn1, GPIO.RISING, callback=push_btn_callback1, bouncetime=100)
GPIO.add_event_detect(push_btn2, GPIO.RISING, callback=push_btn_callback2, bouncetime=100)
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
print("end")
finally:
GPIO.cleanup()
7) PUSH버튼+LED 예제3
push_led_exam3.py
# push_led_exam3.py
# push버튼 4개, led 4개
# 각각의 push버튼이 led를 제어할 수 있도록 - 1
# 함수로 정의해서 사용해보기 - 2
# 객체를 정의해서 사용해보기 - 3
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
class PushLED:
def __init__(self, led_pin, push_btn):
self.led_pin = led_pin
self.push_btn = push_btn
self.led_state = GPIO.LOW
GPIO.setup(self.led_pin, GPIO.OUT)
GPIO.setup(self.push_btn, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(self.push_btn, GPIO.RISING, callback=self.push_btn_callback, bouncetime=200)
def push_btn_callback(self, channel):
self.led_state = not self.led_state
GPIO.output(self.led_pin, self.led_state)
if __name__ == "__main__":
obj1 = PushLED(22, 19)
obj2 = PushLED(23, 20)
obj3 = PushLED(24, 21)
obj4 = PushLED(25, 26)
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
print("end")
finally:
GPIO.cleanup()
- 끝 -
728x90
'프로젝트형 IoT 서비스 개발 4회차 > 3. 게이트웨이 디바이스 제어' 카테고리의 다른 글
[Day54] 2022-04-14(목) 라즈베리파이 준비7 - Visual Studio Code - 김서연 강사님 (0) | 2022.04.14 |
---|---|
[Day53] 2022-04-13(수) 라즈베리파이2 - GPIO2(PWM) - 김서연 강사님 (0) | 2022.04.13 |
[Day52] 2022-04-12(화) 라즈베리파이 준비6 - Camera 연결 - 김서연 강사님 (0) | 2022.04.12 |
[Day52] 2022-04-12(화) 라즈베리파이 준비5 - 한글 폰트 세팅 - 김서연 강사님 (0) | 2022.04.12 |
[Day51] 2022-04-11(월) 라즈베리파이 준비4 - VNC, GPIO 사용 준비 - 김서연 강사님 (0) | 2022.04.11 |