Ready-to-teach lab
Arduino traffic light lab: three LEDs, one sequence
This Arduino traffic light lab runs green, yellow and red LEDs through a timed sequence, and it is where students first meet a function of their own and a program with states.
The lesson
Objective and parts
By the end, students can
- Control several outputs in a fixed sequence.
- Read a function with parameters and say what calling it does.
- Change the timing of a program and add a new step to it.
Parts, already on the bench
- An Arduino Uno
- A green LED on pin 3, a yellow LED on pin 4 and a red LED on pin 5
- Three 220 ohm resistors, one for each LED
Nothing to hand out or collect: the circuit opens built and wired.
Steps
What students do
Each step is short enough to read out. Students work in the simulator beside this page.
- Open the lab and press Run. Time each color with a watch.
- Open the serial monitor. The sketch prints the name of each state as it starts.
- Open the code and find the lights() function. What do its three values mean?
- Make green last eight seconds and yellow one second.
- Add a new phase: red and yellow lit together for one second before green, as signals in the United Kingdom do.
- Add a flashing yellow mode: yellow blinks five times before the sequence starts again.
Questions
Questions for students
Hand these out as they are. The expected answers are under the list, for you.
- How long is one full cycle in the sketch as it opens?
- What does a call such as lights(HIGH, LOW, LOW) do?
- Why does each LED have its own resistor, and not one shared by all three?
- While delay(5000) runs, can the sketch notice anything else, such as a button?
Answer key for the teacher
- Twelve seconds: green for 5, yellow for 2 and red for 5.
- It sets all three LEDs at once: the first value is green, the second yellow, the third red. So this turns green on and the others off.
- Each LED needs its own current limit. Sharing one resistor would change the brightness whenever more than one LED is lit.
- No. delay stops the whole program. A crossing with a button has to check the button during the wait.
Go further
For students who finish early
- Add a pedestrian button that shortens the green.
- Build a second set of lights for the crossing road and keep the two from being green together.
Your class
Give this lab to every student
Assign to my class puts this circuit on each student’s class page as a starter. They change their own copy and hand it in, and the work comes back to you in one list.
The class tools are free for 30 days for one class of up to 20 students, with no card. Opening and running the lab needs no account and stays free.
More labs
The other labs in the set
Ohm's lawCircuits. 40 min.Series and parallelCircuits. 45 min.Voltage dividerCircuits. 40 min.LED and resistorCircuits. 30 min.RC circuitElectronics. 50 min.Transistor switchElectronics. 45 min.Logic gatesElectronics. 40 min.Arduino blinkArduino. 35 min.Button and LEDArduino. 40 min.Ultrasonic distanceArduino. 45 min.Servo sweepArduino. 40 min.All labsThe whole set on one page.