저항
1K옴
10K옴 - 아날로그
220옴 - LED
핀 4개일 경우 RGB LED
GND - 그라운드, 제일 긴 핀
저항계산을 할 줄 알아야 함.
개발 - Thonny
save
import RPi.GPIO as GPIO
import time
led_pin = 14 #GPIO 14
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # pin mode setting
GPIO.setup(led_pin, GPIO.OUT)
for i in range(10):
GPIO.output(led_pin, 1)
time.sleep(1) # 1second delay
GPIO.output(led_pin, 0)
time.sleep(1)
GPIO.cleanup()
횡단보도 신호등 만들기
1. 초록불 점등 5초 후 0.5초 점멸 3회
2. 초록불 꺼지고 빨간불 점등 10초
1, 2 반복 10회
import RPi.GPIO as GPIO
import time
green_led = 15
red_led = 14
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(green_led, GPIO.OUT)
GPIO.setup(red_led, GPIO.OUT)
for i in range(10):
GPIO.output(green_led, GPIO.HIGH)
GPIO.output(red_led, GPIO.LOW)
time.sleep(5)
for j in range(3):
GPIO.output(green_led, GPIO.LOW)
time.sleep(0.5)
GPIO.output(green_led, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(green_led, GPIO.LOW)
GPIO.output(red_led, GPIO.HIGH)
time.sleep(10)
GPIO.cleanup()
import RPi.GPIO as G
import time
button_pin = 18
red = 14
green = 15
G.setwarnings(False)
G.setmode(G.BCM)
G.setup(button_pin, G.IN, pull_up_down=G.PUD_DOWN)
G.setup(red, G.OUT)
G.setup(green, G.OUT)
while True:
if G.input(button_pin) == G.HIGH:
print("button pushed!")
G.output(red, 1)
G.output(green, 0)
else:
G.output(red, 0)
G.output(green, 1)
time.sleep(1)
import RPi.GPIO as GPIO
import time
def button_callback(channel):
#to do when button pushed
print("Button Pushed!")
btn_pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(btn_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# button down = GPIO.RISING, button up = GPIO.FALLING
GPIO.add_event_detect(btn_pin, GPIO.RISING, callback=button_callback)
while True:
time.sleep(0.1)
'공부기록' 카테고리의 다른 글
6월 22일 라즈베리파이 - 온습도, 초음파거리, PWM장치 (0) | 2023.06.22 |
---|---|
6월 21일 라즈베리파이 - VS Code 연동, LED 2 (0) | 2023.06.21 |
5월 25일 Spring - 게시글 수정 처리 (0) | 2023.05.25 |
5월 24일 Spring - 홈페이지 삭제 처리 (0) | 2023.05.24 |
5월 24일 Spring - 게시판 다운로드, 댓글 처리 (0) | 2023.05.24 |