Raspberry Pi Đèn Giao Thông

Trong hướng dẫn này, chúng ta sẽ học cách sử dụng Raspberry Pi để điều khiển module đèn giao thông. Cụ thể, chúng ta sẽ học:

Linh Kiện Cần Thiết

1×Raspberry Pi 5
1×Traffic Light Module
1×dây jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Raspberry Pi
1×(Khuyến nghị) Raspberry Pi Prototyping Base Plate & Breadboard Kit
1×(Khuyến nghị) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Module Đèn Giao Thông

Sơ Đồ Chân

Module đèn giao thông bao gồm 4 chân:

  • GND pin: Chân mass, kết nối chân này với GND của Raspberry Pi.
  • R pin: Chân điều khiển đèn đỏ, kết nối chân này với một chân đầu ra số của Raspberry Pi.
  • Y pin: Chân điều khiển đèn vàng, kết nối chân này với một chân đầu ra số của Raspberry Pi.
  • G pin: Chân điều khiển đèn xanh, kết nối chân này với một chân đầu ra số của Raspberry Pi.
traffic light sơ đồ chân

Cách Hoạt Động

Sơ Đồ Đấu Nối

Raspberry Pi traffic light sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Raspberry Pi

# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi này được cung cấp để sử dụng công khai, không có ràng buộc. # Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: # https://newbiely.vn/tutorials/raspberry-pi/raspberry-pi-traffic-light import RPi.GPIO as GPIO import time # Define GPIO pins PIN_RED = 7 # The Raspberry Pi GPIO pin connected to the R pin of the traffic light module PIN_YELLOW = 8 # The Raspberry Pi GPIO pin connected to the Y pin of the traffic light module PIN_GREEN = 25 # The Raspberry Pi GPIO pin connected to the G pin of the traffic light module # Define time durations RED_TIME = 4 # RED time in seconds YELLOW_TIME = 4 # YELLOW time in seconds GREEN_TIME = 4 # GREEN time in seconds # Set up GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(PIN_RED, GPIO.OUT) GPIO.setup(PIN_YELLOW, GPIO.OUT) GPIO.setup(PIN_GREEN, GPIO.OUT) try: while True: # Red light on GPIO.output(PIN_RED, GPIO.HIGH) GPIO.output(PIN_YELLOW, GPIO.LOW) GPIO.output(PIN_GREEN, GPIO.LOW) time.sleep(RED_TIME) # Yellow light on GPIO.output(PIN_RED, GPIO.LOW) GPIO.output(PIN_YELLOW, GPIO.HIGH) GPIO.output(PIN_GREEN, GPIO.LOW) time.sleep(YELLOW_TIME) # Green light on GPIO.output(PIN_RED, GPIO.LOW) GPIO.output(PIN_YELLOW, GPIO.LOW) GPIO.output(PIN_GREEN, GPIO.HIGH) time.sleep(GREEN_TIME) except KeyboardInterrupt: # Clean up GPIO on exit GPIO.cleanup()

Các Bước Nhanh

  • Đảm bảo bạn đã cài đặt Raspbian hoặc hệ điều hành tương thích khác cho Raspberry Pi.
  • Đảm bảo Raspberry Pi của bạn đã kết nối với cùng mạng cục bộ với PC.
  • Đảm bảo Raspberry Pi của bạn đã kết nối internet nếu bạn cần cài đặt một số thư viện.
  • Nếu đây là lần đầu tiên bạn sử dụng Raspberry Pi, hãy xem Raspberry Pi - Cài Đặt Phần Mềm
  • Kết nối PC của bạn với Raspberry Pi qua SSH sử dụng SSH client có sẵn trên Linux và macOS hoặc PuTTY trên Windows. Xem cách kết nối PC với Raspberry Pi qua SSH.
  • Đảm bảo bạn đã cài đặt thư viện RPi.GPIO. Nếu chưa, hãy cài đặt bằng lệnh sau:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Tạo file Python script traffic_light.py và thêm code sau:
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi này được cung cấp để sử dụng công khai, không có ràng buộc. # Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: # https://newbiely.vn/tutorials/raspberry-pi/raspberry-pi-traffic-light import RPi.GPIO as GPIO import time # Define GPIO pins PIN_RED = 7 # The Raspberry Pi GPIO pin connected to the R pin of the traffic light module PIN_YELLOW = 8 # The Raspberry Pi GPIO pin connected to the Y pin of the traffic light module PIN_GREEN = 25 # The Raspberry Pi GPIO pin connected to the G pin of the traffic light module # Define time durations RED_TIME = 4 # RED time in seconds YELLOW_TIME = 4 # YELLOW time in seconds GREEN_TIME = 4 # GREEN time in seconds # Set up GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(PIN_RED, GPIO.OUT) GPIO.setup(PIN_YELLOW, GPIO.OUT) GPIO.setup(PIN_GREEN, GPIO.OUT) try: while True: # Red light on GPIO.output(PIN_RED, GPIO.HIGH) GPIO.output(PIN_YELLOW, GPIO.LOW) GPIO.output(PIN_GREEN, GPIO.LOW) time.sleep(RED_TIME) # Yellow light on GPIO.output(PIN_RED, GPIO.LOW) GPIO.output(PIN_YELLOW, GPIO.HIGH) GPIO.output(PIN_GREEN, GPIO.LOW) time.sleep(YELLOW_TIME) # Green light on GPIO.output(PIN_RED, GPIO.LOW) GPIO.output(PIN_YELLOW, GPIO.LOW) GPIO.output(PIN_GREEN, GPIO.HIGH) time.sleep(GREEN_TIME) except KeyboardInterrupt: # Clean up GPIO on exit GPIO.cleanup()
  • Lưu file và chạy Python script bằng cách thực hiện lệnh sau trong terminal:
python3 traffic_light.py

Script chạy trong vòng lặp vô hạn liên tục cho đến khi bạn nhấn Ctrl + C trong terminal.

  • Kiểm tra module đèn giao thông

Điều quan trọng cần lưu ý là cách hoạt động chính xác của đèn giao thông có thể khác nhau tùy thuộc vào thiết kế và công nghệ cụ thể được sử dụng ở các khu vực và giao lộ khác nhau. Các nguyên lý được mô tả ở trên cung cấp hiểu biết tổng quan về cách đèn giao thông hoạt động để quản lý giao thông và tăng cường an toàn trên đường.

Code ở trên minh họa việc điều khiển từng đèn riêng biệt. Bây giờ, hãy cải thiện code để tối ưu hóa hơn.

Tối Ưu Hóa Code Raspberry Pi

  • Hãy cải thiện code bằng cách triển khai hàm điều khiển đèn.
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi này được cung cấp để sử dụng công khai, không có ràng buộc. # Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: # https://newbiely.vn/tutorials/raspberry-pi/raspberry-pi-traffic-light import RPi.GPIO as GPIO import time # Define GPIO pins PIN_RED = 7 # The Raspberry Pi GPIO pin connected to the R pin of the traffic light module PIN_YELLOW = 8 # The Raspberry Pi GPIO pin connected to the Y pin of the traffic light module PIN_GREEN = 25 # The Raspberry Pi GPIO pin connected to the G pin of the traffic light module # Define time durations in seconds RED_TIME = 2 # RED time in seconds YELLOW_TIME = 1 # YELLOW time in seconds GREEN_TIME = 2 # GREEN time in seconds # Define indices for the light states RED = 0 YELLOW = 1 GREEN = 2 # Create lists for pins and times pins = [PIN_RED, PIN_YELLOW, PIN_GREEN] times = [RED_TIME, YELLOW_TIME, GREEN_TIME] # Set up GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(PIN_RED, GPIO.OUT) GPIO.setup(PIN_YELLOW, GPIO.OUT) GPIO.setup(PIN_GREEN, GPIO.OUT) def traffic_light_on(light): for i in range(len(pins)): if i == light: GPIO.output(pins[i], GPIO.HIGH) # turn on else: GPIO.output(pins[i], GPIO.LOW) # turn off try: while True: # Red light on traffic_light_on(RED) time.sleep(times[RED]) # keep red light on during a period of time # Yellow light on traffic_light_on(YELLOW) time.sleep(times[YELLOW]) # keep yellow light on during a period of time # Green light on traffic_light_on(GREEN) time.sleep(times[GREEN]) # keep green light on during a period of time except KeyboardInterrupt: # Clean up GPIO on exit GPIO.cleanup()
  • Hãy cải thiện code bằng cách sử dụng vòng lặp for.
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi này được cung cấp để sử dụng công khai, không có ràng buộc. # Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: # https://newbiely.vn/tutorials/raspberry-pi/raspberry-pi-traffic-light import RPi.GPIO as GPIO import time # Define GPIO pins PIN_RED = 7 # The Raspberry Pi GPIO pin connected to the R pin of the traffic light module PIN_YELLOW = 8 # The Raspberry Pi GPIO pin connected to the Y pin of the traffic light module PIN_GREEN = 25 # The Raspberry Pi GPIO pin connected to the G pin of the traffic light module # Define time durations in seconds RED_TIME = 2 # RED time in seconds YELLOW_TIME = 1 # YELLOW time in seconds GREEN_TIME = 2 # GREEN time in seconds # Define indices for the light states RED = 0 YELLOW = 1 GREEN = 2 # Create lists for pins and times pins = [PIN_RED, PIN_YELLOW, PIN_GREEN] times = [RED_TIME, YELLOW_TIME, GREEN_TIME] # Set up GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(PIN_RED, GPIO.OUT) GPIO.setup(PIN_YELLOW, GPIO.OUT) GPIO.setup(PIN_GREEN, GPIO.OUT) def traffic_light_on(light): for i in range(len(pins)): if i == light: GPIO.output(pins[i], GPIO.HIGH) # turn on else: GPIO.output(pins[i], GPIO.LOW) # turn off try: while True: for light in range(RED, GREEN + 1): traffic_light_on(light) time.sleep(times[light]) # keep light on during a period of time except KeyboardInterrupt: # Clean up GPIO on exit GPIO.cleanup()
  • Hãy cải thiện code bằng cách sử dụng hàm millis() thay vì delay().
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi này được cung cấp để sử dụng công khai, không có ràng buộc. # Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: # https://newbiely.vn/tutorials/raspberry-pi/raspberry-pi-traffic-light import RPi.GPIO as GPIO import time # Define GPIO pins PIN_RED = 7 # The Raspberry Pi GPIO pin connected to the R pin of the traffic light module PIN_YELLOW = 8 # The Raspberry Pi GPIO pin connected to the Y pin of the traffic light module PIN_GREEN = 25 # The Raspberry Pi GPIO pin connected to the G pin of the traffic light module # Define time durations in seconds RED_TIME = 2 # RED time in seconds YELLOW_TIME = 1 # YELLOW time in seconds GREEN_TIME = 2 # GREEN time in seconds # Define indices for the light states RED = 0 YELLOW = 1 GREEN = 2 # Create lists for pins and times pins = [PIN_RED, PIN_YELLOW, PIN_GREEN] times = [RED_TIME, YELLOW_TIME, GREEN_TIME] # Set up GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(PIN_RED, GPIO.OUT) GPIO.setup(PIN_YELLOW, GPIO.OUT) GPIO.setup(PIN_GREEN, GPIO.OUT) light = RED # start with RED light last_time = time.time() def traffic_light_on(light): for i in range(len(pins)): if i == light: GPIO.output(pins[i], GPIO.HIGH) # turn on else: GPIO.output(pins[i], GPIO.LOW) # turn off try: while True: if (time.time() - last_time) > times[light]: light += 1 if light >= 3: light = RED # new circle traffic_light_on(light) last_time = time.time() # TO DO: your other code except KeyboardInterrupt: # Clean up GPIO on exit GPIO.cleanup()

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.