액추에이터 : LED(Light EmittingDiode)
- 저항 : 흑갈적등황 녹청자회백 0123456789 은5% 금10%
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
LED = 18
GPIO.setup(LED, GPIO.OUT)
try:
# LED(18)번 채널을 출력용으로 설정
# 예외처리 구문
while (True):
GPIO.output(LED, GPIO.HIGH)
time.sleep(1)
GPIO.output(LED, GPIO.LOW)
time.sleep(1)
except KeyboardInterrupt: # 예외: 키보드 중단(Ctrl+C 등)
GPIO.output(LED, GPIO.LOW) # LED(18)에 LOW 상태 인가
GPIO.cleanup() # 프로그램 종료
finally:
GPIO.cleanup()
PWM 출력
PWM을 이용한 LED 밝기 제어
import RPi.GPIO as GPIO
import time
LED = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)
# PWM 인스턴스 생성: LED 채널, 0~100
# PWM 출력 0에서 시작 (0-100 사이)
LED = GPIO.PWM(LED, 100)
LED.start(0)
Delay = 0.1
try:
while True:
for i in range(0, 101): # [0, 101) 범위에서 1 증가 반복
LED.ChangeDutyCycle(i) # PWM 출력값 i로 변경
time.sleep(Delay) # 0.1초 지연
for i in range(100, -1, -1): # [100, -1) 범위에서 -1 증가 반복
LED.ChangeDutyCycle(i) # PWM 출력값 i로 변경
time.sleep(Delay) # 0.1초 지연
except KeyboardInterrupt:
LED.stop()
GPIO.cleanup()
finally:
GPIO.cleanup()
Grove Pi+
Grove Pi를 사용한 led 실습
import time
from grovepi import *
led = 4
pinMode(led, "OUTPUT")
time.sleep(1)
while True:
try:
digitalWrite(led, 1) # LED ON
print("LED ON!")
time.sleep(1)
digitalWrite(led, 0) # LED OFF
print("LED OFF!")
time.sleep(1)
except KeyboardInterrupt:
digitalWrite(led, 0)
break
except IOError:
print("Error")