
Programming a traffic light system: Introduction

Upgrade for more content
True or false? A LED can be programmed.
Oh, how long Lina has to wait for this traffic light. It just seems to stay red! Lina wonders: how do traffic lights work exactly? Maybe like this? Green… yellow… red… yellow… green… yellow… red… Bus coming!
Change to green. Or maybe not. But this is very close to what is happening: something decides when the lights should turn on, and off, and recognises when a bus comes, but it is not small people who do it, it’s something we can program. Let’s use a microcontroller for the programming. And LEDs to represent the lights in the traffic signal.
To connect all these parts we need cables. For an LED to turn on we need current. For current we need a closed electric circuit, with a positive pole and a negative pole, to make the electrons want to move. We use the microcontroller to direct the current to the LED. Let’s take a closer look at the microcontroller.
The microcontroller has two buttons: A and B. There are pins with different names, like: 0, 1, 2, 3V and GND. Each pin has a hole designed for connecting cables, with crocodile clips or banana plugs. To get current we need voltage. Voltage is measured in volts.
Aha! 3V stands for 3 volts. This is the positive pole on the microcontroller. What does GND mean then? It is short for ground.
The english word. That’s the negative pole on the microcontroller. An LED has two legs. One of the legs is longer than the other. Connect this one to the positive pole.
Then the current flows so that the LED lights up. Gently separate the LEDs legs so that both of the legs do not connect with the positive pole. Or the current will take the shortest route, and there will be a short circuit, from one leg to the other. Connect a cable from the short leg of the LED to the negative pole on the microcontroller. There!
Now we have closed the circuit. The LED lights up. How strongly it lights depends on the voltage. Here we have 3 volts. Too high a voltage causes the LED to break, because the current through the LED then becomes too high.
The current can be limited by connecting a resistor in series with the LED. Since we chose a three-volt LED, and our microcontroller uses three volts, there's no danger. Now the LED lights up as long as the circuit is closed. That’s not good enough for a traffic signal, though: one light, shining all the time. Imagine if it just shone red!
We need to make sure we can control when the LED lights up or not. We need to program the microcontroller to open and close the circuit. Connect the cable that goes from the long leg of the LED to any of the numbered pins. The numbered pins can be programmed to close or open the circuit. Let’s use pin zero.
Move the plus cable from 3V to zero. Let’s first explain in small steps what we are telling the microcontroller to do. We use what is called pseudocode, which can then be translated into programming code. What we want the microcontroller to do is: “Turn on the LED” “Pause” “Turn off the LED” “Pause” “Turn on the LED” and so on... When we want something to be repeated in programming, we use a loop.
A loop is performed as long as a condition is true. If we want a loop to never end, we make sure that the condition is always true. For instance, “as long as two is larger than one.” So we add a while line: “While condition is true” above the pseudocode we previously wrote. To show which rows are involved in the loop, tab those rows under the "While" row like this: Now it's time for you to change this pseudocode into programming code. Off you go!