How do you know when it’s safe to cross a busy street?
Ever wonder why cars don’t constantly crash into each other on busy streets, or how you know it’s safe to cross at a crosswalk? In this activity, we’ll explore the traffic signals that help keep everyone on the road safe.
The Shibuya Crossing in Japan is considered to be the busiest pedestrian intersection in the world with up to 3,000 people sometimes crossing the street at once. Watch the video below and think more about how vehicle and pedestrian traffic coexist through urban planning.
Shibuya Crossing in Japan
In this activity you’ll use a micro:bit and a special traffic light extension—designed to look and work just like the real-world traffic signals you see all over Chicago. First, you’ll learn how to connect your micro:bit to the traffic light component. Then, you’ll program it to create a working traffic cycle, including a pedestrian crossing signal when the light is red. By the end of the activity, you’ll have a clearer understanding of how everyday systems like traffic lights help cars and pedestrians share the road without colliding.
Connections
This year in SUPERCHARGE, we’ll explore ways to get around more sustainably. With new technologies emerging, automation and electric vehicles are becoming key parts of modern transportation. In our previous activities, we learned how to program with a micro:bit—the same microcontroller we’ll use here. Next, we’ll take two micro:bits to simulate a train crossing, showing how these systems help keep people and vehicles moving safely.
Materials
Stop:bit
Micro:bit
USB-A to micro-USB cable
Computer
Instructions
Traffic lights are crucial in keeping cars and pedestrians safe in Chicago. In this project, we'll use a micro:bit and a stop:bit to simulate the steps that real traffic lights go through every day.
First, using a timer, you’ll build a basic stoplight that cycles through each color. Next, you’ll adjust the speed of the cycle to match how busy the street is. Finally, you’ll add a pedestrian crossing function, giving people a clear signal for when it’s safe to walk. By the end, you’ll understand how traffic lights keep everyone moving smoothly and safely!
Building the Stop:bit
Figure 1. Deconstructed Stop:bit
Gently bend the “foot piece” back and forth until it detached from the stop:bit as shown in Figure 1.
A = Stop:bit
B = “foot piece”
Figure 2. Stop:bit with attached “foot piece”
2. Take the stop:bit and attach the “foot piece” into the slot at the bottom.
Figure 3. Completed Stop:bit
3. Place the micro:bit on the back of the stop:bit, so that 0, 1, 2, power (3V), and ground (GND) line up as shown in Figure 3. Secure the micro:bit to the stop:bit at each connection using the bolts and nuts.
Figure 4. Extension tab
Coding the Traffic Cycle
Open up the Makecode website on your computer. This will be the site we use to code our micro:bits.
Click on the gray “+ Extension” tab on the left side of your screen, search for the “stop:bit,” and add it to your code as shown in Figure 4.
Figure 5. Stop Light Cycle
3. First, think about what lights cycle through the traffic light on the streets. In the “Forever” block, use the “Turn ___ Traffic Light ___” block from the extension tab and “pause (ms)” block from the basic tab to turn on and off the lights to create a traffic cycle of: red light, green light, yellow light, then red light again with pauses in between as shown in Figure 5.
4. Pauses allow previous code to work before your next code starts. Forever block allows code inside to repeat indefinitely. The extension blocks control the lights on the stop:bit.
Figure 6. Stop Light Cycle with Pause Variables
Coding the High Traffic / Low Traffic Toggle Function
What if there is a large amount of traffic? The lights should last longer to allow more cars to pass through. How do we modify the program to do this?
5. First replace the pause times you made with different variables. Go to the variables tab and hit the “Make a Variable…” button above the blocks. Create “Switch Time”, “Car Time”, and “Toggle” variables as shown in Figure 6.
Figure 7. “if/else” block
Variables allow you to change their value. You can create an infinite amount of variables with unique names. This allows us to change the pause lengths with a button press.
6. Now, add a toggle function when you press the “B” button that tells the light which “toggle mode” it should be in. We will use blocks from the logic, variable, and input tabs. (The colors of the blocks match the color of the tabs!)
7. Use an “if/else if” block to tell the micro:bit that it will need to do something if it is “0” or “1” as shown in Figure 7.
Figure 8. Toggle Function
The “on button B pressed” block allows code function only if the “B” button on the micro:bit is pressed.
8. Next, code the micro:bit so it knows to switch to “1” if it's “0” or to “0” if it's “1” already.
Figure 9. Forever “if/else if” block
This code allows us to swap the value of Toggle between 0 and 1. If B is pressed and Toggle is 1, it will change to 0. If B is pressed and Toggle is 0, it will change to 1.
9. Now that the micro:bit can change toggle between “0” and “1”, we want to code the micro:bit so something changes depending on the toggle. Create another forever block and copy the code in the previous step.
Multiple forever blocks allow two lines of code to function indefinitely simultaneously.
10. Delete the blocks inside the “if/else if” block.
Figure 10. Time Toggle
11. Use the “set variable to” blocks to create a high-traffic mode (toggle = 1) and low-traffic mode (toggle = 0) for your traffic light. Remember that 1,000 ms = 1 second.
This code allows us to switch the variable values whenever the Toggle variable is changed between 0 and 1.
Figure 11. Pedestrian Request Button
Coding the Pedestrian Crossing
I hope you didn’t forget about the pedestrians! We need to create a function that allows pedestrians to cross the street when cars are not moving.
12. Create a new variable called “Crossing Request” in the variables tab.
13. Create a crossing request when the “A” button is pressed using the new variable. Make sure to add an image so the pedestrian knows not to walk!
Figure 12. “if/else if” in Traffic Cycle
The “show LEDs” block allows us to put an image on the LED screen of the micro:bit.
14. Add an “if/then” block into the “Forever” block that contains the traffic cycle to make something happen when a request is placed. Think about what color the light should be when adding the block.
Figure 13. If Crossing Request
An if block within a series of codes will only interrupt the sequence if the condition in the if/then is met.
15. Like before, create code that tells the micro:bit to do something if the Crossing Request is “1”.
Figure 14. Display Image
This code sets the conditions that must be met for the if/then statement to trigger.
16. When the request is “1,” a picture should be displayed to let pedestrians know it is time to cross, there should be a pause to let them cross, and the request should be canceled or changed to “0.” The clear screen allows us to turn all the LED lights off.
You can download the code for today's activity here.
EXTENSION:
When we cross the street, there is usually a button that the pedestrian can hit similar to the one we just made! In real life, however, the button usually gives an audio cue to wait until it is time to cross. Think about how we can implement this into the code. Mess around with the blocks to add additional prompts, such as audio and visual visuals.
Wrap-up
In this activity, we started to learn how to code a micro:bit. We experimented with functions, extensions, and inputs to create a working traffic light. We have a working traffic cycle with toggleable speeds and a pedestrian crossing function!
How difficult was it to work with the micro:bit?
Was coding the micro:bit less challenging than you expected?
What makes our traffic light different from the ones in Chicago?
How do you think it might be different than the signals use in the Shibuya crossing in Japan?
Next Activity
We will combine our traffic light with an access gate in the next activity. This will help us manage more of our transportation needs!