본문 바로가기
공부기록

6월 20일 (2) 라즈베이파이 - 이론, LED 연결

by project100 2023. 6. 20.

저항 
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()

 

 

Tinkercad | From mind to design in minutes

Tinkercad is a free, easy-to-use app for 3D design, electronics, and coding.

www.tinkercad.com

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)