Introduction: Arduino Traffic Light Controller
Sometime around 2003, I purchased a full-sized traffic light on eBay. Why? Well, because I thought it would be a cool thing to have in the garage. The price was reasonable, shipping too. If you cant tell, its a poly-carbonate shell, and not metal, so it's pretty light for its size.
It did not come with any sort of controller, just lights and wires. So, I just wired a three rocker switch so I could turn the lights on/off manually. But what I really wanted was a controller. There were a few to be found commercially, but they cost more than the light, so I waited. The micro-controller revolution was still a few years away so, I waited. And waited. Fast forward 13 years.
Finally, I realized that with the addition of relays, this would be the perfect Arduino project. Why relays? If you don't know, relays are switches that allow low load circuits to control high load circuits. The traffic light works on 110v AC power, a micro-controller on 5v-12v DC. The only way to control the AC power from Arduino, would be with relays. Thankfully, there are many relays available for usage with Arduino. Lastly, I had never worked with Arduino, and was looking for a project, so here we are!
Wires - use what you have or buy pre-made
Traffic Light - Have one or get one. Look on eBay or Craigslist.
That should be enough to get you started... let's get down to business.
Step 1: Connections on the Arduino and Relay
We need to have the Arduino board communicate with the Relay board.
To do this, first we need to provide power to the Relay board. One could power the Relay board with a separate 5v DC power supply, but Arduino is already designed with a 5v output. As you can see on the pin out toward the bottom of the board, there is a 5v and a GND. For consistency, put your BLACK wire in GND and RED wire in 5V. Connect them to the corresponding pins on the Relay board. BLACK for GND and RED for VCC.
Great. Now you have power to your Relay.
Now, we need a way for the Arduino to trigger the actual relays. For this , we use the Output pins on the Arduino, and connect them to the Input pins on the Relay board. Color doesn't matter, but try not to use black or red so you don't get confused.
I used outputs 5, 6, and 7 on Arduino and connected to Inputs IN1, IN2, and IN3 on the Relay board. I'm only using 3 relays, as I only have three lights to control. Someday, if I ever get that traffic light with the turn signal arrow, I'll be set ;-)
Now that our boards are connected... we need to do some programming... so lets jump from hardware to software.
Step 2: Programming!
In order for the lights to flash, we need to tell them what to do.
Personally, I am not an Arduino programming expert. Thankfully, there are lots of tutorials and examples on the internet.
With all the things that this hardware can do, this quite possibly the simplest thing.
Attached is the program file.
First we define the output PINs, then we initialize the PINs into output mode.
Lastly, we create a loop that triggers each relay. First, we start with Relay 1 and set it to Low (meaning Normally Opened to Closed). This closes the circuit and the light is energized. Next, we wait 2 seconds, then trigger the relay to HIGH (which sets the relay to Normally Opened), which then opens the relay, disconnects the electricity and the light goes off.
We then repeat this with Relays 2 and 3, then back to Relay 1, ad infinitum...
Now this is a great start. We can increase or decrease the timer... we could randomize the wait time. Heck ,we could even randomize the order. We could even get various sensors (sounds, proximity, pressure, temperature, NFC) to trigger the lights. We'd just need to get the hardware, and program for it.
Once you're happy with your code, save, compile and upload to your Arduino.
Not sure how to do any of this?
Here's a great getting started tutorial.
Can we test it before we install it? Sure, can! Read on!
Step 3: Test Before Install.
After getting the software successfully loaded on the Arduino, it was very easy to tell that the software was working correctly with the hardware.
If you watch the video on this page, you can see the LEDs flashing one by one every two seconds on the relay board. The LED shows that the relay is being activated. In addition, you should even be able to hear the mechanical 'CLICK' of the relay being closed every two seconds.
With programming out of the way, now, the hard part.
Let's get these parts working with our traffic light!
Step 4: Integration
We have completed connecting the Arduino to the Relay board; we've programmed it; we've tested it. Now we have to integrate these components into our traffic light circuitry.
There are two BIG steps.
(1) Wiring the relay side of the Relay board.
Each relay has three electrical inputs.
Your power comes into the relay on the COM connection. Depending on the nature of you electrical appliance, and what you want the relay to do ,it can trigger the connection to open or close. If you have an appliance that would always be on, but then you want to trigger it off, you would connect to COM and Normally Closed. This means the circuit is Normally Closed UNTIL the relay is triggered and then the circuit is opened. Thereby turning off your device.
In our case ,we are using Normally Opened ,meaning the circuit is OPEN, with no current flowing, UNTIL the relay is triggered, closing the circuit and allowing power to flow.
So, what we need to do is get power to the relay on the COM connections.
We start with an old extension cord that we cut an end off of, so that we are left with a two prong plug, and the wire. One end of the wire will connect directly to the light circuit in the traffic light. The other side, we connect to our three COM connections ,one in each relay. We can daisy chain them instead of running three separate wires to the one wire on the extension cord.
Then we run one wire from each of the Normally Open connections to each individual light circuit. In this way, once the relay is triggered, it allows electricity to flow from the COM to Normally Open, to the light, and then back down to the other end of the plug.
(2) Wiring the terminal block in the traffic light.
This one wasn't too bad. Basically, there are two sides of the terminal block. One side connects to the light fixtures, the other side to external power. My terminal block had one connection for each Red, Yellow Green hot, and one for each Red , Yellow , Green Common. For ease or connectivity, I merged all my commons from the light fixtures into one block.
Then, as you can see in the drawing and the pictures, the hot for each light is connected to the hot from each relay. When the relay closes, boom. Circuit is closed, electricity flows.
Hopefully, my drawing, including the power plug will help you understand the complete circuit.
Step 5: Plug It In!
Now that that is all done, all we need to do is plug in the traffic light, and then plug in the Arduino,
If you've done everything right, the boards should power up, upon initialization all lights will come on, and then they will go through their loop sequence and light for two seconds.
Hope you enjoyed!