import RPi.GPIO as GPIO # GPIO 모듈 호출
Rpi.GPIO 클래스(객체) 안의 모든 변수와 매소드를 출력할 수 있다.
python -c 'import RPi.GPIO as GPIO; print(dir(GPIO))'
import한 모듈의 버전 정보 확인
GPIO.RPI_INFO
GPIO에 관련된 참고자료
https://sourceforge.net/p/raspberry-gpio-python/wiki/Examples/
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setmode(GPIO.BOARD)
ex) BOARD 모드의 11번 핀 = BCM모드의 17번 핀

GPIO.setup(pin_number , GPIO.IN)
GPIO.setup(pin_number , GPIO.OUTPUT)
GPIO.setup(pin_number , GPIO.OUTPUT , initail = GPIO.HIGH)
GPIO.setup(핀번호,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(핀번호,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
pin = [12 ,15]
GPIO.setup(pin , GPIO.OUTPUT)
GPIO.output(17 , GPIO.HIGH)
GPIO.output(17 , GPIO.LOW)
GPIO.input(14) # low값 또는 high 값을 읽어온다.
# 아날로그 값을 읽기 위해서는 DAC 모듈 필요
GPIO.wait_for_edge(pin/port number , 상태 , timeout)
GPIO.wait_for_edge(15 , GPIO.FALLING , timeout = 5000)
pin/port number :
상태 : 1) GPIO.FALLING , 2) GPIO.RISING , 3) GPIO.BOTH
timeout : 특정 시간동간 기다리려면 값을 입력
add_event_detect(pin , 상태 , callback , bouncetime)
해당 핀을 이벤트에 등록하는 함수
def button_pressed_callback():
print("인터럽트 발생!)
GPIO.add_event_detect(15, GPIO.FALLING,
callback=button_pressed_callback, bouncetime=100)
#15번 핀의 interrput 발생 조건을 FALLING edge를 감지하면 동작하도록 설정한다
#그리고 FALLING edge 변화를 감지하면 사용자가 정의한 콜백 함수가 동작하게 된다.
#콜백 함수를 사용하기 위해서는 callback함수가 먼저 정의되어야 한다.
#bouncetime(ms) 보다 짧은 시간에 여러번 인터럽트가 트리거 되는 경우 콜백은 한번만 호출
channel = GPIO.wait_for_edge(channel, GPIO_RISING, timeout=5000)
if channel is None:
print('Timeout occurred')
else:
print('Edge detected on channel', channel)
# event_detected 사용
GPIO.add_event_detect(channel, GPIO.RISING)
if GPIO.event_detected(channel):
print('Button pressed')
#add_event_detect 사용
def my_callback(channel):
print('This is a edge event callback function!')
print('Edge detected on channel %s'%channel)
print('This is run in a different thread to your main program')
#add rising edge detection on a channel
GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback)
#remove rising edge detection on a channel
GPIO.remove_event_detect(channel)