Step 4: Create the relay control circuit (theory)
The arduino can directly power LEDs because the voltage needed to light them is less than the 5 volts the arduino can put out. But the lamps in a standard US stoplight use 120 volts and are AC. Controlling them from an arduino requires additional circuitry to allow the 5 volt control pins to safely switch on the 120 volt AC lamps without ever directly touching them.
This can be accomplished via relays. A relay is combination of a magnetic switch and an electromagnet. Current flowing through the electromagnet's coil creates a magnetic field that forces the nearby switch to open or close. So large currents can be switched on and off by smaller currents flowing through one part of the relay without directly being part of the larger currents circuit.
I wasn't able to find reasonably priced and sized relays that could control 120 volts from 5 volts, so I used 9 volt relays instead. This leads to a similar problem in that arduino STILL can't control the 9 volt portion of the circuit directly. Fortunately, the gap isn't nearly as dangerous, and transistors can be used to switch the 9 volt relay currents using 5 volt arduino pins. Since the arduino in this project is powered by a 9 volt adapter, we can use the same power source to power the relays. To do this we can pull 9 volts off of the VIN pin on the arduino. The VIN pin exposes whatever current is used to power the arduino, unlike the 3.5 and the 5 volt pins which are the power after it has been regulated to what the arduino needs internally.
The two diagrams below show how the three different voltages can live in the same device. Click the "i" in the upper left corner of each of them to access the full sized (and not fuzzy) versions.
Each of the four control lines control a lamp or outlet as follows. I'll use the red light to illustrate:
- When the control pin is turned on, current is allowed to flow from RED-DC (pin 14) through a resistor into the base of a transistor.
- As the 5 volt current flows from the base to ground via the transistor's emitter, this "switches on" the transistor. This allows current to flow from the 9 volt VIN pin on the arduino through the relay's coil and into the transistor's collector; eventually to exit via the emitter to ground.
- As current flows though the relay's coil, an electromagnetic field is created and the switch in the relay is drawn closed with a satisfying "click". 120 volts flow through this switch and light the red lamp.
- When the control pin is turned off, the 9 volt current stops flowing through the transistor, the electromagnetic field collapses and the relay clicks back off. The diode is in place to protect the transistor from the sudden flood of reversed current pushed back across the contacts of the coil.
The next steps will discuss creating this circuit.