.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _py_traffic_light: 7.6 Traffic Light ================================= `Traffic Light `_ is a signal device located at roadway intersections, crosswalks and other locations to control the flow of traffic. Traffic signals are standardized by the `Vienna Convention on Road Signs and Signals `_. Provides users with the right-of-way by alternating LEDs in three standard colors. * **Red light**: Traffic should stop if it sees a flashing red light, equivalent to a stop sign. * **Yellow light**: A warning signal is about to turn red. Yellow lights are interpreted differently in different countries (regions). * **Green light**: Allows traffic to move in the indicated direction. In this project, we will use three colors of LEDs to implement traffic light changes and a 4-digit 7-segment display to show the time of each traffic state. **Schematic** |sch_traffic_light| * This circuit is based on the :ref:`py_74hc_4dig` with the addition of 3 LEDs. * The 3 red, yellow and green LEDs are connected to GP7~GP9 respectively. **Wiring** |wiring_traffic_light| **Code** .. note:: * Open the ``7.6_traffic_light.py`` file under the path of ``euler-kit/micropython`` or copy this code into Thonny, then click "Run Current Script" or simply press F5 to run it. * Don't forget to click on the "MicroPython (Raspberry Pi Pico)" interpreter in the bottom right corner. * For detailed tutorials, please refer to :ref:`open_run_code_py`. .. code-block:: python import machine import time from machine import Timer # [Green, Yellow, Red] lightTime=[30, 5, 30] # display SEGCODE = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f] sdi = machine.Pin(18,machine.Pin.OUT) rclk = machine.Pin(19,machine.Pin.OUT) srclk = machine.Pin(20,machine.Pin.OUT) placePin = [] pin = [10,13,12,11] for i in range(4): placePin.append(None) placePin[i] = machine.Pin(pin[i], machine.Pin.OUT) def pickDigit(digit): for i in range(4): placePin[i].value(1) placePin[digit].value(0) def clearDisplay(): hc595_shift(0x00) def hc595_shift(dat): rclk.low() time.sleep_us(200) for bit in range(7, -1, -1): srclk.low() time.sleep_us(200) value = 1 & (dat >> bit) sdi.value(value) time.sleep_us(200) srclk.high() time.sleep_us(200) time.sleep_us(200) rclk.high() def display(num): pickDigit(0) hc595_shift(SEGCODE[num%10]) pickDigit(1) hc595_shift(SEGCODE[num%100//10]) pickDigit(2) hc595_shift(SEGCODE[num%1000//100]) pickDigit(3) hc595_shift(SEGCODE[num%10000//1000]) # led # 9Red, 8Yellow,7Green pin = [7,8,9] led=[] for i in range(3): led.append(None) led[i] = machine.Pin(pin[i], machine.Pin.OUT) def lightup(state): for i in range(3): led[i].value(0) led[state].value(1) # timer counter = 0 color_state= 0 def time_count(ev): global counter, color_state counter -= 1 if counter <= 0: color_state = (color_state+1) % 3 counter = lightTime[color_state] tim = Timer(period=1000, mode=Timer.PERIODIC, callback=time_count) while True: display(counter) lightup(color_state) When the code runs, the green LED stays on for 30 seconds, the yellow LED stays on for 5 seconds, and the green LED stays on for 30 seconds.