라즈베리 파이 피코 교통 신호등

이 가이드에서는 Raspberry Pi Pico를 사용하여 신호등 모듈을 관리하는 방법을 알려드리겠습니다. 다음 내용을 다룰 것입니다:

라즈베리 파이 피코 신호등

준비물

1×라즈베리 파이 피코 W 아마존
1×라즈베리 파이 피코 (또는) 아마존
1×마이크로 USB 케이블 아마존
1×신호등 모듈 쿠팡 | 아마존
1×점퍼케이블 쿠팡 | 아마존
1×(추천) 라즈베리 파이 피코용 스크루 터미널 확장 보드 아마존
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

교통 신호 모듈에 대하여

핀아웃

신호등 모듈에는 네 개의 핀이 있습니다.

  • GND 핀: 이 그라운드 핀을 Raspberry Pi Pico의 GND에 연결하세요.
  • R 핀: 이 핀은 빨간 불빛을 제어합니다. Raspberry Pi Pico의 디지털 출력에 연결하세요.
  • Y 핀: 이 핀은 노란 불빛을 제어합니다. Raspberry Pi Pico의 디지털 출력에 연결하세요.
  • G 핀: 이 핀은 초록 불빛을 제어합니다. Raspberry Pi Pico의 디지털 출력에 연결하세요.
신호등 핀아웃

작동 방식

선연결

라즈베리 파이 피코 교통 신호등 배선도

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

라즈베리 파이 피코 코드

/* * 이 라즈베리 파이 피코 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 피코 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pico/raspberry-pi-pico-traffic-light */ import machine import time # Define pin numbers (you can change these to match your wiring) PIN_RED = 19 # GPIO19 on Raspberry Pi Pico PIN_YELLOW = 21 # GPIO21 on Raspberry Pi Pico PIN_GREEN = 22 # GPIO22 on Raspberry Pi Pico # Define times in seconds (MicroPython uses seconds for time.sleep) RED_TIME = 4 # RED time in seconds YELLOW_TIME = 4 # YELLOW time in seconds GREEN_TIME = 4 # GREEN time in seconds # Setup pins as output red = machine.Pin(PIN_RED, machine.Pin.OUT) yellow = machine.Pin(PIN_YELLOW, machine.Pin.OUT) green = machine.Pin(PIN_GREEN, machine.Pin.OUT) # Main loop while True: # Red light on red.value(1) # turn on red yellow.value(0) # turn off yellow green.value(0) # turn off green time.sleep(RED_TIME) # keep red light on for the defined period # Yellow light on red.value(0) # turn off red yellow.value(1) # turn on yellow green.value(0) # turn off green time.sleep(YELLOW_TIME) # keep yellow light on for the defined period # Green light on red.value(0) # turn off red yellow.value(0) # turn off yellow green.value(1) # turn on green time.sleep(GREEN_TIME) # keep green light on for the defined period

자세한 사용 방법

아래 지침을 단계별로 따르십시오:

  • 컴퓨터에 Thonny IDE가 설치되어 있는지 확인합니다.
  • Raspberry Pi Pico에 MicroPython 펌웨어가 설치되어 있는지 확인합니다.
  • Raspberry Pi Pico를 처음 사용한다면 라즈베리 파이 피코 - 시작하기 튜토리얼에서 자세한 설명을 참고하세요.
  • 제공된 다이어그램에 따라 Raspberry Pi Pico를 신호등 모듈에 연결합니다.
  • USB 케이블을 사용하여 Raspberry Pi Pico를 컴퓨터에 연결합니다.
  • 컴퓨터에서 Thonny IDE를 실행합니다.
  • Thonny IDE에서 Tools Options 메뉴로 이동하여 MicroPython (Raspberry Pi Pico) 인터프리터를 선택합니다.
  • 인터프리터 탭에서 드롭다운 메뉴로부터 MicroPython (Raspberry Pi Pico)를 선택합니다.
  • 올바른 포트가 선택되었는지 확인합니다. Thonny IDE가 포트를 자동으로 감지하지만, 수동으로 선택해야 할 수도 있습니다 (예: Windows의 COM3 또는 Linux의 /dev/ttyACM0).
  • 위 코드를 복사하여 Thonny IDE의 편집기에 붙여넣습니다.
  • 스크립트를 Raspberry Pi Pico에 저장합니다:
    • 저장 버튼을 클릭하거나 Ctrl+S 단축키를 사용합니다.
    • 저장 대화상자에서 "This computer"와 "Raspberry Pi Pico" 두 섹션이 표시되면, Raspberry Pi Pico를 선택합니다.
    • 파일 이름을 main.py로 저장합니다.
  • 녹색 실행 버튼을 클릭하거나 F5 키를 눌러 스크립트를 실행합니다. 스크립트가 실행됩니다.
  • 신호등 상태를 확인합니다.

신호등은 각 위치의 설계에 따라 다양한 방식으로 작동합니다. 여기 신호등이 교통을 관리하는 방법에 대한 간단한 설명이 있습니다.

위에 나온 코드는 각 조명을 개별적으로 제어할 수 있습니다. 이제 코드를 개선하여 보다 효과적으로 작동하도록 만들 것입니다.

라즈베리 파이 피코 코드 최적화

  • 빛을 관리하는 함수를 만들어 코드를 개선해봅시다.
/* * 이 라즈베리 파이 피코 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 피코 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pico/raspberry-pi-pico-traffic-light */ import machine import time # Define pin numbers (you can change these to match your wiring) PIN_RED = 19 # GPIO19 on Raspberry Pi Pico PIN_YELLOW = 21 # GPIO21 on Raspberry Pi Pico PIN_GREEN = 22 # GPIO22 on Raspberry Pi Pico # Define times in seconds (MicroPython uses seconds for time.sleep) RED_TIME = 2 # RED time in seconds YELLOW_TIME = 1 # YELLOW time in seconds GREEN_TIME = 2 # GREEN time in seconds # Define indexes RED = 0 YELLOW = 1 GREEN = 2 # Setup pins as output and store them in a list pins = [ machine.Pin(PIN_RED, machine.Pin.OUT), machine.Pin(PIN_YELLOW, machine.Pin.OUT), machine.Pin(PIN_GREEN, machine.Pin.OUT) ] # Define the times array times = [RED_TIME, YELLOW_TIME, GREEN_TIME] def trafic_light_on(light): for i in range(RED, GREEN + 1): if i == light: pins[i].value(1) # turn on else: pins[i].value(0) # turn off # Main loop while True: # Red light on trafic_light_on(RED) time.sleep(times[RED]) # keep red light on during a period of time # Yellow light on trafic_light_on(YELLOW) time.sleep(times[YELLOW]) # keep yellow light on during a period of time # Green light on trafic_light_on(GREEN) time.sleep(times[GREEN]) # keep green light on during a period of time
  • 우리는 for 루프를 사용하여 코드를 개선할 수 있습니다.
/* * 이 라즈베리 파이 피코 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 피코 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pico/raspberry-pi-pico-traffic-light */ import machine import time # Define pin numbers (you can change these to match your wiring) PIN_RED = 19 # GPIO19 on Raspberry Pi Pico PIN_YELLOW = 21 # GPIO21 on Raspberry Pi Pico PIN_GREEN = 22 # GPIO22 on Raspberry Pi Pico # Define times in milliseconds (MicroPython can handle time in milliseconds with time.sleep_ms) RED_TIME = 2000 # RED time in milliseconds YELLOW_TIME = 1000 # YELLOW time in milliseconds GREEN_TIME = 2000 # GREEN time in milliseconds # Define indexes RED = 0 YELLOW = 1 GREEN = 2 # Setup pins as output and store them in a list pins = [ machine.Pin(PIN_RED, machine.Pin.OUT), machine.Pin(PIN_YELLOW, machine.Pin.OUT), machine.Pin(PIN_GREEN, machine.Pin.OUT) ] # Define the times array times = [RED_TIME, YELLOW_TIME, GREEN_TIME] def trafic_light_on(light): for i in range(RED, GREEN + 1): if i == light: pins[i].value(1) # turn on else: pins[i].value(0) # turn off # Main loop while True: for light in range(RED, GREEN + 1): trafic_light_on(light) time.sleep_ms(times[light]) # keep light on during a period of time
  • time.sleep() 대신 millis() 함수를 사용하여 코드를 개선해 봅시다.
/* * 이 라즈베리 파이 피코 코드는 newbiely.kr 에서 개발되었습니다 * 이 라즈베리 파이 피코 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/raspberry-pico/raspberry-pi-pico-traffic-light */ import machine import time # Define pin numbers (you can change these to match your wiring) PIN_RED = 19 # GPIO19 on Raspberry Pi Pico PIN_YELLOW = 21 # GPIO21 on Raspberry Pi Pico PIN_GREEN = 22 # GPIO22 on Raspberry Pi Pico # Define times in milliseconds (MicroPython can handle time in milliseconds with time.sleep_ms) RED_TIME = 2000 # RED time in milliseconds YELLOW_TIME = 1000 # YELLOW time in milliseconds GREEN_TIME = 2000 # GREEN time in milliseconds # Define indexes RED = 0 YELLOW = 1 GREEN = 2 # Setup pins as output and store them in a list pins = [ machine.Pin(PIN_RED, machine.Pin.OUT), machine.Pin(PIN_YELLOW, machine.Pin.OUT), machine.Pin(PIN_GREEN, machine.Pin.OUT) ] # Define the times array times = [RED_TIME, YELLOW_TIME, GREEN_TIME] # Initialize variables last_time = time.ticks_ms() light = RED # start with RED light def trafic_light_on(light): for i in range(RED, GREEN + 1): if i == light: pins[i].value(1) # turn on else: pins[i].value(0) # turn off # Initialize the first light trafic_light_on(light) # Main loop while True: current_time = time.ticks_ms() if time.ticks_diff(current_time, last_time) > times[light]: light += 1 if light >= 3: light = RED # reset to RED for a new cycle trafic_light_on(light) last_time = current_time # TO DO: your other code

동영상

비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.