Steps to Create a Simple LED Traffic Light
1
Open OmniBoard Studio and create a new project.
2
Drag and drop three LED blocks onto the canvas to represent the red, yellow, and green lights. You will need at least three LED blocks, a way to control the timing of the lights, and a way to loop the sequence.
Try to figure out how to connect the blocks on your own! If you need a hint, click here. ▼
- ● Use the Start and While True blocks to create a loop that will run indefinitely.
- ● Inside the loop, turn the Red LED ON, then wait with a Timer block (e.g. 3000 ms), then turn the Red LED OFF.
- ● Next, turn the Yellow LED ON, wait (e.g. 1000 ms), then turn the Yellow LED OFF.
- ● Finally, turn the Green LED ON, wait (e.g. 3000 ms), then turn the Green LED OFF. The loop will then repeat from the beginning.
3
Build the correct circuit by connecting three LEDs to your microcontroller.
- ● Connect the Red LED anode (long leg) through a resistor to the GPIO pin assigned for red (e.g. pin 2), and the cathode (short leg) to ground.
- ● Repeat the same wiring for the Yellow LED on a second GPIO pin (e.g. pin 3).
- ● Repeat the same wiring for the Green LED on a third GPIO pin (e.g. pin 4).
4
Compile the code and upload it to your microcontroller.
5
Watch your traffic light cycle through Red → Yellow → Green and repeat!
Bonus Information
- Here is an example of simplified MicroPython code for an LED traffic light project:
import machine # Needed for controlling the GPIO pins
import time # Needed for creating delays
red = machine.Pin(2, machine.Pin.OUT) # Red LED on GPIO 2
yellow = machine.Pin(3, machine.Pin.OUT) # Yellow LED on GPIO 3
green = machine.Pin(4, machine.Pin.OUT) # Green LED on GPIO 4
while True: # Create an infinite loop
red.on() # Turn Red ON
time.sleep(3) # Wait 3 seconds
red.off() # Turn Red OFF
yellow.on() # Turn Yellow ON
time.sleep(1) # Wait 1 second
yellow.off() # Turn Yellow OFF
green.on() # Turn Green ON
time.sleep(3) # Wait 3 seconds
green.off() # Turn Green OFF
import RPi.GPIO as GPIO # Needed for controlling the GPIO pins
import time # Needed for creating delays
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.OUT) # Red LED
GPIO.setup(3, GPIO.OUT) # Yellow LED
GPIO.setup(4, GPIO.OUT) # Green LED
while True:
GPIO.output(2, GPIO.HIGH) # Red ON
time.sleep(3)
GPIO.output(2, GPIO.LOW) # Red OFF
GPIO.output(3, GPIO.HIGH) # Yellow ON
time.sleep(1)
GPIO.output(3, GPIO.LOW) # Yellow OFF
GPIO.output(4, GPIO.HIGH) # Green ON
time.sleep(3)
GPIO.output(4, GPIO.LOW) # Green OFF