Software apps and online services
Hand tools and fabrication machines
A Traffic Light project is a great and fun way for a beginner to get started with Arduino. In such a project, you usually learn how to turn on and off LEDs and have a certain delay between them, to simulate a set of traffic lights turning from GREEN to YELLOW to RED, and back again, endlessly.
However, in practice, the science of traffic lights can be quite complex! There can be two or more streams of traffic, pedestrian lights, protected left turns, and more.
You need to ensure that there is a perfect song and dance between the various lights. The timing between them needs to be carefully coordinated, and is usually determined by studying the traffic flow over time for a given intersection. Based on changing traffic conditions, the timing may also need to be updated/changed over time.
One can also add sensors to the mix to trigger traffic light changes - for example, using an inductive loop embedded in the road, or an ultrasonic sensor, or a video camera to detect the presence of cars to trigger a State change based on current traffic conditions, and not just on a time based approach alone. These are called Actuated Signal Controls.
The science of Traffic Intersections and signals is a fascinating one, and one that offers a lot of interesting project opportunities for Arduino (and other MCU) enthusiasts. I've included a few interesting links at the bottom for you to check out if you would like to learn more about the art and science behind traffic intersections and lights, and to give you inspiration for future projects.What this Project handles
In this particular project, I've reimagined the simple Traffic Light example and decided to make things a little more challenging - keeping it manageable for the beginner, but offering plenty of opportunity for more complex customizations for the intermediate and advanced users, and learning a couple of programming tips along the way.
We will model two streams of traffic (East-West, and North-South, for example, with flow in both directions), and pedestrian walk signals for each stream. The streams of traffic are as shown below.
We use only one set of lights for each stream for the purpose of brevity. You can quite easily add the additional sets (one for each direction of a stream), and have them all coordinated.
So we will model two sets of traffic lights with pedestrian walk signs (one for each stream), as shown below (RED, YELLOW and GREEN for vehicular traffic, and ORANGE and WHITE for pedestrians):
We will also add two buttons - a "walk request" button for each set of pedestrian lights.
Here is the Fritzing diagram for the circuitry:
Thus, you can see that from a hardware perspective, this project has gotten a little bit more complex, with more LEDs and more switches, but is still completely doable by the beginner enthusiast.
Where it gets a little more complex (but easier to program and reason about as you add more lights and states later on) is the introduction of Finite State Machines programming concept and some other programming tricks used (like bitmaps to manage the states of the LEDs).
The beginner can get started easily by using the provided code sample and making small tweaks (such as for changing cycle and phase times) but without worrying too much about the intricacies of the code. The intermediate and advanced users can start to modify the code to add more states to the State Machine. Once you understand the concepts, you will find that it will become a lot easier to add more states to your traffic light system - such as actuation based on ultrasonic sensors, protected left-turn turn signals, etc. We will talk more about this below.Assembling everything
A shameless little plug for ProtoStax here! 😊 I'm the creator of ProtoStax - Stackable and ModularEnclosures that you can use in different configurations to support different stages of your prototyping. Think of it as the Lego or Meccano set of enclosures. Start off prototyping in a "Platform-configuration" (basically as prototyping plates with rubber feet that protect your Arduino (and your table!)) for open access; Add side walls and the top as you go from a work-in-progress prototype to something you can show off in a neat little enclosure. ProtoStax enclosures also stack (both side-by-side and one on top of the other), allowing you to add more modules to your project as you go along. Find out more at protostax.com!
I typically have my micro-controller and breadboard mounted on my ProtoStax in "Platform configuration" ready for my next project - mounted, protected (and with rubber-feet, protecting my table surface)!. In this project, I'm using an Arduino and a Breadboard that I have readily mounted in "Platform configuration" as shown below.
I first populate my breadboard as per my Fritzing diagram, as follows:
When I have populated the breadboard appropriately, the next step is to hook up the connections from the Arduino. At this point I have three choices.
- Leave the Arduino and Breadboard separate. However, as ProtoStax enclosures can be stacked, why not keep the two boards attached? They become easier to handle as a unit, given that you will have a mess of wires between them. How many times have you moved one board and then had the wires disconnect or loosen? I have done it many times!
- Stack the Arduino and Breadboard side-by-side
- Stack the Breadboard above the Arduino (as the Breadboard represents the stuff you want to show off - the "User Interface" in your demo)
I can hook up the Arduino and the Breadboard side-by-side, and finish the wiring, as shown below:
Or I can stack the Breadboard on top of the Arduino. This helps to organize the mess of wires and makes the Breadboard on top more presentable. This will be useful, say, in a science fair or competition where you would like your prototype to look as nice as possible to get maximize marks for presentation! 😊It also puts the focus on the LEDs and buttons (the User Interface) as you explain the concepts and workings of the Traffic Light.
[Note:It is possible to add side walls to the bottom enclosure with the Arduino. I've chosen to keep the side walls out for more open access. I'm also using the side wall slots to help organize my mess of wires! 😊]Next Step - Programming
Next up, upload the sketch to the Arduino and run it! You can find the sketch on GitHub - find the link to the repository below and follow the instructions on the README.
The traffic lights go on a cycle of green for 5 seconds, yellow for 2.5 seconds, followed by a red of 7.5 seconds (Green 5 + Yellow 2.5 seconds of the other stream). The individual state transition times are easily adjustable, which allows you to experiment with different timings for different phases.
[Note:In real life, the cycle time and phase times are dependent on factors like historic data of flow of traffic, peak vs. non-peak times, and so on. For the purpose of my demo, I chose phase times to be able to demonstrate the concepts quickly! 😊]
Currently, both streams have the same phase and cycle times, but you can easily change that. You could make one of the streams a main stream of traffic (meaning that it has a longer green and yellow cycle) and the other a secondary stream with a shorter cycle, for example.
Pedestrian walks stay "DONT WALK" (orange LED) until a wall request is issued via the Walk Request buttons. When a walk request has been received for a stream, at the next cycle where the vehicular stream is GREEN, the side walk lights up with a "WALK" light. It then goes through a Flashing DONT WALK phase (corresponding to the yellow of the vehicular stream), followed by a transition to DONT WALK when the vehicular stream goes RED. Corresponding light changes happen on the other stream as well.
The logic is implemented by means of a State Machine - the lights transition from one state to another and there are a finite number of such states that we transition through. We can identify the different states of the traffic lights as follows :
[Note:the Pedestrian Walk/Don't Walk signs may or may not be activated, depending on whether the walk request button has been pressed) - the Pedestrian states listed below assume that the walk request is present]
Thus we see that there are 4 distinct States to transition through before going back to the first and repeating the process. I have named these states MS_GREEN_STATE, MS_YELLOW_STATE, MS_RED_STATE and SS_YELLOW_STATE. We can see that adding the second stream of traffic has increased the number of states by 1 (where the yellow happens on the secondary stream, the primary stream continues to stay red).
The State Machine I have implemented also has intermediate states (TO_MS_GREEN_STATE, TO_MS_YELLOW_STATE, TO_MS_RED_STATE, and TO_SS_YELLOW_STATE) in between the main states. These intermediate states allow for any logic to be executed when transitioning between the main states. In this example, the intermediate states are used to mark the time of entry into a main state, so we can track how long to stay in a particular state. I also do some book-keeping for walk requests and tracking when the Walk signs are in an activated state or not.
I have also modeled the pedestrian walk signs as sub-states that operate within the context of the same 4 main states (i.e. either staying orange the entire time, or when a walk request is received, to transition between ORANGE, FLASHING ORANGE and WHITE that corresponds with one of the 4 states). You can implement it as a secondary state machine that has its own timing between ORANGE, WHITE and FLASHING ORANGE if you like.Taking It Further
The State Machine approach allows you to reason about the various states and the transitions to the states in an intuitive manner, which allows for easier debugging of any issues and fixing them. For example, I found that I had mistakenly tied the Pedestrian Walk signals to the opposite stream of traffic - it was fairly easy to find and correct the issue.
It also allows you to add more States (for example, if you choose to add a protected Left-Turn signal in one or more streams. Or if you want to model a very complex 6-way intersection with protected left turns! Once you model the various States and their transitions, you can update the logic to handle the new states and the transitions between them.
It is also possible to have a secondary state machine for the Pedestrian Walk signs (that run between the GREEN and YELLOW states of the main state machine), allowing for WALK/DONT WALK intervals to be different than the GREEN/YELLOW intervals (though they should add up to or be less than the GREEN+YELLOW interval, for the safety of the poor pedestrians! I leave that as an exercise for you.
You can also add sensors to the mix to make Actuated Signal Controls - instead of the timing based state transitions, the states can change in response to a sensor input (which reflects current traffic conditions) while keeping in mind certain minimum times, for example, a yellow light needs to be on for a certain length of time in order to give time for vehicles to clear the intersection, before the opposite stream is allowed through.
The Pedestrian Walk Request buttons can also be used to influence/speed-up any Pedestrian Walk cycles, as opposed to only being activated by the press of the button as is done currently.Optional Resources to learn more about Traffic Lights and Intersections
- How Do Traffic Signals Work? - YouTube video - "Traffic management in dense urban areas is an extremely complex problem with a host of conflicting goals and challenges. One of the most fundamental of those challenges happens at an intersection, where multiple streams of traffic - including vehicles, bikes and pedestrians - need to safely, and with any luck, efficiently, cross each others’ paths. However we accommodate it now or in future, traffic will continue to be one of the biggest challenges in our urban areas and traffic signals will continue to be one of its solutions."
- Traffic Signal Timing Manual - Federal Highway Administration - 273 page document that has a ton of information. Use the table of contents to zero-in on any areas of interest.
- A Beginner's Guide to Traffic Signal Timing - Learn about basic concepts and terminology with respect to traffic signals and their timing
- Traffic Signal Timing and Phasing - A discussion on Signal Timing and Phasing Issues and how they affect intersection efficiency and utilization
- Push Buttons & Signal Timing - A discussion of various things to consider for pedestrian push buttons
- Ask CityLab: Do ‘WALK’ Buttons Actually Do Anything? - An interesting discussion on how and IF walk buttons actually do anything! I'm sure all of us have wondered about that at some point or another! 😊
- Traffic Detector Handbook - Sensor Technology - Learn more about the types of sensors used in traffic intersections.
I hope you have enjoyed this project. As you can see, traffic lights are an area where you can go as simple or as complex as you like! I also hope you have learnt a few neat tricks by using the State Machine approach to modeling the traffic light transitions! The code sample also uses bitmaps to enable specifying which of the entire set of LEDs to light up, in a compact manner.
If you have enjoyed this project and the other projects I have created, I encourage you to subscribe to the Hackster ProtoStax Hub and check out new and existing projects! If you have any project ideas using ProtoStax enclosures, I encourage you to post them to the Hackster ProtoStax Hub!
Feel free to write back with questions or comments!
Happy Making! 😊
Sridhar (and the ProtoStax Team! 😊)