1. Tact 스위치
2. 로커 스위치
3. Push 스위치
눌렀을 때 딸깍거리는 스위치 - 주로 4개의 단자로 이루어져 있고, 일반적으로는 하나의 회로만 사용함
회로
polling 방식
from gpiozero import Button
from time import sleep
button = Button(2) # GPIO 2번핀
while True:
if button.is_pressed:
print("Button is pressed") # 버튼 눌렸을 때 출력
sleep(1)
else :
print("Button is not pressed") # 버튼 눌리지 않았을 때 출력
sleep(1)
interrupt 방식
from gpiozero import Button
from signal import pause
def say_hello():
print("Hello!")
def say_goodbye():
print("Goodbye!")
button = Button(2)
button.when_pressed = say_hello # 버튼 눌렸을 떄 Hello 출력
button.when_released = say_goodbye # 버튼 땠을 때 Goodbye 출력
pause()
from gpiozero import Button, LED
from signal import pause
flag = 0
led1 = LED(3) # LED는 3번핀
def go():
global flag
if flag == 0 :
flag = 1
led1.on()
else :
flag = 0
led1.off()
button = Button(2) # 스위치는 2번핀
button.when_pressed = go # 버튼 눌리면 go 함수 실행
pause()
깔끔하게 잘 정리 되있어서 좋았어요!
라즈베리파이에 관련된 지식은 주로 공식문서에서 얻나요? 추천해줄 만한 링크가 있으면 감사하겠습니다!