라즈베리파이 led 버튼
-------------------------------------------------------------------------------------------------------------------
라즈베리파이 40개의 커넥터 pin 설명
3.3v , 5v파워 (+)부분으로 거의 쓸일없음
GND 접지 (-)로 생각해도 무방
led의 다리가 긴부분이 + 짧은쪽 -
GPIO 범용 입출력
GNB
이상전압에 의해 발생하는 전류를 흘려보내는 역할,안전 차원에서 필수
-------------------------------------------------------------------------------------------------------------------
브레드보드
http://www.ntrexgo.com/archives/21821
-------------------------------------------------------------------------------------------------------------------
회로도 스케치 프로그램 Fritzing
https://ddangeun.tistory.com/13
사용법 단축키
https://kocoafab.cc/tutorial/view/655
-------------------------------------------------------------------------------------------------------------------
led 버튼 만들기 참고자료
https://www.hackster.io/hardikrathod/push-button-with-raspberry-pi-6b6928
버튼 누르고 있을때 켜지기
회로도
LED_Button.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # -*- coding:utf-8 -*- #한글 입력 import RPi.GPIO as GPIO #gpio라이브러르 import time #sleep사용 GPIO.setmode(GPIO.BCM) #gpio 모드 셋팅 GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Button 입력 GPIO23 GPIO.setup(24, GPIO.OUT) #LED 출력GPIO24 try: while True: button_state = GPIO.input(23) #버튼 상태 확인 if button_state == False: #눌러진상태면 GPIO.output(24, True) #출력 print('Button Pressed...') time.sleep(0.2) else: GPIO.output(24, False) except KeyboardInterrupt: #ctrl-c 누를시 GPIO.cleanup() | cs |
각 함수 설명
https://studymake.tistory.com/498
GPIO.setup함수
실행
python LED_Button.py
-------------------------------------------------------------------------------------------------------------------
위 코드를 응용하여 버튼 누르면 켜지고 다시누르면 꺼지기
회로도는 동일
LED_Button2.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | # -*- coding:utf-8 -*- #한글 입력 #버튼 지속하기 import RPi.GPIO as GPIO #gpio라이브러르 import time #sleep사용 GPIO.setmode(GPIO.BCM) #gpio 모드 셋팅 GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Button 입력 GPIO23 GPIO.setup(24, GPIO.OUT) #LED 출력GPIO24 try: a=False while True: button_state = GPIO.input(23) #버튼 상태 확인 if button_state == False: #눌러진상태면 if a: a=False else: a=True if a: GPIO.output(24, True) #출력 else: GPIO.output(24, False) time.sleep(0.15) except KeyboardInterrupt: #ctrl-c 누를시 GPIO.cleanup() | cs |
실행
python LED_Button2.py