006 - MicroPython TechNotes: Traffic Light LED Module

Introduction

In this article, we will learn on how to use the Traffic Light LED module. It is a small model or miniature of the traffic light.

Pinout

  1. G – for the ground.
  2. V – for the supply voltage.
  3. Green – for the control signals for green LED.
  4. Yellow – for the control signals for yellow LED.
  5. Red – for the control signals for red LED.

Bill Of Materials

  1. ESP32 development board.
  2. Gorilla Cell ESP32 shield (optional)
  3. 5-pin dupont jumper wires.
  4. Traffic Light LED module.

Hardware Instruction

  1. Attach the ESP32 on top of the ESP32 shield making sure that the pins are properly aligned and both the USB port are on the same side.
  2. Attach the dupont jumper wires to Traffic Light LED module by following a color coding which is:
    • black – for the ground.
    • red – for the VCC.
    • yellow and following colors – for the control signal.
  3. Attach the other side of the dupont jumper wires on ESP32 shield by matching the colors of the dupont wires to the pin headers of ESP32 shield pin headers.
  4. Power the ESP32 shield with the USB type-C cable.
  5. Power ON the shield by sliding the slide switch to ON / BATT.
  6. Connect the ESP32 to computer by connecting the micro USB cable.

Software Instruction

  1. Open the Thonny Python IDE. If you don’t have this installed, be sure to watch the article tutorial # 1.
  2. Press the “Stop” button to let the Thonny Python IDE to connect to ESP32.
  3. Create a new Python file by clicking the “New” button.
  4. Copy and paste the provided source code below to your Thonny.
  5. Press “Run the current script” to execute the code.
  6. Enjoy and feel free to modify it according to your liking.

Video Demonstration

Call To Action

For any concern, write your message in the comment section.

You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.

Thank you and have a good days ahead.

See you,

– George Bantique | tech.to.tinker@gmail.com

Source Code

1. Traffic Light LED: Example # 1, simple turn ON and OFF:

 1from machine import Pin
 2from time import sleep
 3
 4g = Pin(23, Pin.OUT)
 5y = Pin(25, Pin.OUT)
 6r = Pin(26, Pin.OUT)
 7
 8# The following code should be 
 9# tested on the REPL.
10g.on()  # Turn ON green LED.
11g.off() # Turn OFF green LED.
12
13r.value(1) # Turn ON red LED.
14r.value(0) # Turn OFF red LED.
15
16y.value(True)  # Turn ON yellow LED.
17y.value(False) # Turn OFF yellow LED.

2. Traffic Light LED: Example #2, running LED:

 1from machine import Pin
 2from time import sleep
 3
 4g = Pin(23, Pin.OUT)
 5y = Pin(25, Pin.OUT)
 6r = Pin(26, Pin.OUT)
 7led = [r, y, g]
 8
 9while True:
10    for x in range(3):
11        led[x].on()
12        sleep(0.3)
13        led[x].off()

3. Traffic Light LED: Example #3, single traffic light:

 1from machine import Pin
 2from time import sleep
 3
 4g = Pin(23, Pin.OUT)
 5y = Pin(25, Pin.OUT)
 6r = Pin(26, Pin.OUT)
 7
 8traffic_state = ['SOLID_GRN',
 9                 'BLINK_GRN',
10                 'SOLID_RED',
11                 'SOLID_ORN']
12
13while True:
14    for x in traffic_state:
15        if x == 'SOLID_GRN':
16            g.on()
17            sleep(6)
18            g.off()
19        
20        if x == 'BLINK_GRN':
21            for i in range(4):
22                g.on()
23                sleep(0.3)
24                g.off()
25                sleep(0.3)
26        
27        if x == 'SOLID_RED':
28            r.on()
29            sleep(7)
30            r.off()
31            
32        if x == 'SOLID_ORN':
33            y.on()
34            sleep(1)
35            y.off()

References And Credits

  1. gorillacell.kr/


Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.