What to expect
Why do some intersections require more than just a traffic light? When and why would a city install physical barriers like crossing gates? In this hands-on activity, you'll explore these questions by programming a micro:bit-powered railroad crossing gate that works in sync with a traffic light.
A little Chicago history...
Did you know that crossing gates used to be hand operated?
photo credit: https://whippanyrailwaymuseum.net/buildings-and-structures/railroad-crossing-gates-signals/
Chicago street level railroad crossings used to look like this... Do you see any crossings that still look this way where you live?
photo credit: Mark Vogel
How did these railroad crossings work?
Connections
Last week, we built a working traffic light system to control the flow of vehicles and pedestrians at an intersection. In our next activity, we’ll take things a step further by integrating multiple systems to simulate the intersection of two one-way streets.
Did you know that Chicagoland is home to one of the most dangerous railroad crossings in the country? How would you design a safer railroad crossing for cars, buses and pedestrians?
Materials
2 Micro:bits
Stop:bit
Access:bit
USB-A to Micro-USB cable
Computer
Instructions
Imagine you are at a busy Chicago railroad crossing, and a train is rapidly approaching. What happens next? What if a driver doesn’t notice the traffic light above the tracks? This is where a gate system becomes essential for safety. In this activity, you’ll first build the Access:bit, which will serve as the crossing gate. Then, you’ll code the traffic light system to work with both micro:bits, ensuring they respond automatically to the approaching train. Finally, you’ll program the Access:bit and test your system to ensure everything functions smoothly. This hands-on experience gives you insight into real-world traffic control and automation.
Part 1: Building the Gate
Set up the servo!
Figure 1. Servo motor attachment
Screw the servo motor into the side of the access:bit as shown in Figure 1. Make sure that the white arm gear is positioned on the top before screwing it in.
Figure 2. Servo Plugged In
2. Plug the wire from the servo into the back of the access bit so that the brown wire is connected to “0V”, the red wire is connected to “+V”, and the orange wire is connected to "P2.”
Figure 3. Micro:bit Attached
3. Attach the micro:bit to the access:bit.
Line up the micro:bit on the back of the access:bit with “P0” over “0”, “P1” over “1”, “P2” over “2”, “3V” over “3V”, and “0V” over “GND” as shown in Figure 3. Secure each connection with a bolt and nut.
Figure 4. On/Off Switch
4. Insert 3 “AA” batteries into the back of the access:bit. Make sure that you turn it on as shown in Figure 4.
5. Connect your micro:bit to your computer. Download this code which cycles your gate through 3 points of a 90-degree range. The servo can reach 180 degrees, but only a range of 0-90 degrees is used with the access:bit. Once the code is downloaded, the servo will move to an open position. DO NOT PRESS ANY BUTTONS.
Figure 5. Arm Attached
6. Now, choose what you want to use for your gate. Attach the gate UPRIGHT in the servo’s white arm gear, as shown in Figure 5.
Part 2: Coding the Traffic Light
Take your stop:bit (traffic light) from the previous activity and attach it to your computer using the USB cord.
Open the Makecode website and create a copy of your code from the last activity. (Here is a completed copy of the code if you missed it!)
Figure 6. Modified Code from Activity 1.1
3. Remove the code that allows you to toggle between high traffic and low traffic modes and replace the “car time” variables in the pauses to 4,000 and the “switch time” variables in the pauses to 1,000.
Figure 7. Radio Controls
4. Add these blocks to your code (Make sure to choose a unique radio group number!)
Figure 8. Finished Code
5. We don’t want the gate open at a red light or if a pedestrian is crossing! Add this code as well. Additionally, we want the gate to close when the micro:bit logo is tapped. MAKE SURE ALL PAUSES ARE THE RIGHT LENGTH! Add this code to do that.
6. Download the code to your micro:bit and unplug it from the computer. You can download the finished code here.
Part 3: Coding the Access Gate
7. Connect your micro:bit that is attached to the access:bit to your computer using the USB cable.
Figure 9. Extension
8. Open the Makecode website and create a new project. In the extensions tab type “access:bit” into the search bar and click on the extension as shown in Figure 9.
Figure 10. Radio Group
9. In order to receive information from your first micro:bit you will have to set both to the SAME radio group, as shown in Figure 10.
Figure 11. Radio Received
10. Now we want to add an “on radio received receivedNumber” block so that the gate does things when signaled to. Add an “if/then” block for all possible received numbers as shown in Figure 11.
Figure 12. Gate Controls
11. Add “move barrier down”, “pause”, and “move barrier up” blocks to all of the spaces in the if then statement as shown in Figure 12.
Figure 13. Correct Pause Times
12. Next, we want to fill in the times for the pauses. We want the pauses to match up with the traffic light as shown in Figure 13. The gate shouldn’t be open too early if there is a train!
Figure 14. Noises Included
13. Feel free to add some fun noises for the different situations! What sound should the micro:bit play if there is a train? A pedestrian crossing? The access:bit also comes with an additional buzzer that can be programmed with the extension blocks. If you include audio, place it before the pause block, as shown in Figure 14.
14. If you are having trouble, here is the complete code for both the access:bit and the stop:bit.
Wrap-up
In this activity, we worked with the access:bit to create a train gate. We first built it and then coded it to help solve issues we see daily when taking the L or riding the Metra. We then connected it to our traffic light from the previous activity to create a working system.
Did you have any issues when using the radio functions of the micro:bit
How are train crossing controls different than Chicago?
Why does Chicago not have that many railroads going through its streets?
Next Activity
Next week we will continue to work with traffic controls and automation.