Step 4: Create the relay control circuit (theory)

This section can be skipped if you don't care WHY the circuit works, or if you already understand how relays and transistors work.

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:
  1. 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.
  2. 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.
  3. 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.
  4. 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.
where did you buy those lights? <br>thanks! <br>marC:)
<p>i found it either on eBay but this project will cost to much to build a sleeping light or while you use your computer with games you want dim light. </p>
<p>i found it either on eBay but this project will cost to much to build a sleeping light or while you use your computer with games you want dim light. </p>
See step 1: &quot;Materials&quot; for some suggestions on where to get stoplights. These came from http://www.trafficlights.com/polysigs.htm
<p>Holy sh*t this is awesome!</p><p>Maybe i could build a smaller version one :)</p>
Hey, just wanted to add to the accolades and say this was a great tutorial. Also wanted to let you know that I ended up getting a PCB made for this and it worked great. I also wrote a PHP class to make sending the HTTP quests easier. I posted a little write up on my blog, giving you due credit of course, and included photos of the PCB and the finished circuit: <br> <br>http://su.percilio.us/2012/05/arduino-stoplight-web-server/ <br> <br>Thanks for the great work!
I'm so excited that you printed the board and more than that, it actually works and fits in the box! Logically it should have, but I've miss-measured things wrong often enough that I'm always amazed when something comes out right the first time. <br><br>Thank you for your kind words and for testing the gerber file for me. :) It's really cool to see the printed version of the board.<br><br>I'll update the article to indicate it's a valid board.<br><br>I noticed that you put in the 4th relay. Are you using it for anything?<br><br>
Yeah, the PCB seems to work great. I did have little trouble when only two of the relays seemed to be sending power to the screw terminal, but I think that was down to my soldering. I resoldered a number of the joints and it seemed to be fine after that. <br> <br>I did install the fourth relay, mostly because I thought I MIGHT use it and the relays came in a pack of eight, but we are not currently using it for anything. It does click whenever you send it a &quot;beacon&quot; command, but, for now, that's all it does.
Im not sure if its me doing something wrong, but when ever i try and use someone elses code from here, it never works ! <br><br><br>stopduino.cpp:12:23: error: SdFatUtil.h: No such file or directory<br>stopduino.cpp: In function 'void setup()':<br>stopduino:97: error: 'FreeRam' was not declared in this scope<br>stopduino.cpp: In function 'void loop()':<br>stopduino:156: error: 'FreeRam' was not declared in this scope<br><br><br>Not sure :S
You are missing the &quot;sdfat&quot; library, or specifically, the SdFatUtil.h file that comes with it. You can get the latest version from http://code.google.com/p/sdfatlib/downloads/list. I apologize for not putting it in the article, it snuck in because I was initially following the tutorial at http://www.ladyada.net/learn/arduino/ethfiles.html and was planning to have a prettier web page with the html stored on an SD card.<br><br>Since I ended up not doing that, I commented out the line that loads &quot;sdfat.h&quot;. But the lines that print the free ram use a function that is in sdfatutil.h, and I forgot that it didn't come with the arduino.<br><br>I'll update the article to reflect this library later today.<br><br>In the mean time, you have two choices.<br>1) download and install sdfat from the link above.<br>2) comment out the line that loads sdfatutil.h AND all for lines that refer to &quot;FreeRAM&quot;<br><br>Let me know if this helps or if you need any further assistence.
I have updated the sketch to a) remove the dependency on that library and b) make it compatible with the 1.0 version of the arduino IDE as well as earlier ones.<br><br>http://stopduino.googlecode.com/svn/trunk/stopduino/stopduino.pde<br><br>Let me know if this fixes everything for you.<br><br>
Errorrrrrr Errooorrrrrr<br><br><br>stopduino.pde:-1: error: cannot declare parameter 'client' to be of abstract type 'Client'<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:7: note: because the following virtual functions are pure within 'Client':<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:12: note: virtual size_t Client::write(uint8_t)<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:13: note: virtual size_t Client::write(const uint8_t*, size_t)<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:14: note: virtual int Client::available()<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:15: note: virtual int Client::read()<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:17: note: virtual int Client::peek()<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:18: note: virtual void Client::flush()<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:10: note: virtual int Client::connect(IPAddress, uint16_t)<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:11: note: virtual int Client::connect(const char*, uint16_t)<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:16: note: virtual int Client::read(uint8_t*, size_t)<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:19: note: virtual void Client::stop()<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:20: note: virtual uint8_t Client::connected()<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:21: note: virtual Client::operator bool()<br>stopduino.pde:-1: error: cannot declare parameter 'client' to be of abstract type 'Client'<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:7: note: since type 'Client' has pure virtual functions<br>stopduino.pde:-1: error: no matching function for call to 'Server::Server(int)'<br><br>As of Arduino 1.0, the Server class in the Ethernet library has been renamed to EthernetServer.<br><br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Server.h:4: note: candidates are: Server::Server()<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Server.h:4: note: Server::Server(const Server&amp;)<br>stopduino.pde:-1: error: cannot declare variable 'server' to be of abstract type 'Server'<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Server.h:4: note: because the following virtual functions are pure within 'Server':<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h:48: note: virtual size_t Print::write(uint8_t)<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Server.h:6: note: virtual void Server::begin()<br>stopduino.pde:-1: error: cannot declare parameter 'client' to be of abstract type 'Client'<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:7: note: since type 'Client' has pure virtual functions<br>stopduino.cpp: In function 'void doform(Client)':<br>stopduino.pde:-1: error: cannot allocate an object of abstract type 'Client'<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:7: note: since type 'Client' has pure virtual functions<br>stopduino.cpp: At global scope:<br>stopduino.pde:-1: error: cannot declare parameter 'client' to be of abstract type 'Client'<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:7: note: since type 'Client' has pure virtual functions<br>stopduino.cpp: In function 'void loop()':<br>stopduino.pde:-1: error: 'class Server' has no member named 'available'<br>stopduino.pde:-1: error: cannot declare variable 'client' to be of abstract type 'Client'<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:7: note: since type 'Client' has pure virtual functions<br>stopduino.pde:-1: error: cannot allocate an object of abstract type 'Client'<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:7: note: since type 'Client' has pure virtual functions<br>stopduino.pde:-1: error: cannot allocate an object of abstract type 'Client'<br>/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Client.h:7: note: since type 'Client' has pure virtual functions
These errors are going to be trickier. It seems that between the time I wrote this and now, the areduino environment moved to version 1.0 and at that time they broke a bunch of stuff by renaming functions and doing other things that make it not backwardly compatible.<br><br>I can't really update the actual stoplight because it's doing it's job in production, but I have another ethernet shield I can experiment with. I'll try to make a version that works with 1.0 this coming week, since other people are likely to run into this in the future.<br><br>In the mean time, you could probably just install an earlier version of the arduino IDE and (with the library mentioned on the comment above) have it compile. If you are using an UNO, I believe you need to have installed 1.0 first because it provides a driver for it, but you can have multiple versions of the IDE on the same machine.
I forgot the mention, the offending changes are to the ethernet library, which comes with the current version and older versions of the IDE. They changed &quot;Server&quot; to &quot;EthernetServer&quot;. :(
I hadn't done any soldering in a while, so as a way to ease into this project one of the first things I did was solder the headers to the ribbon cable - easy to fix and re-do if I screwed up (plenty of ribbon cable and extra headers). <br><br>When I came back to this step to attach the 6-pin connector after doing the rest of the project I realized I could just make out the location of the red marked cable in this picture of the connector and the ribbon cable - at this orientation the red is on the top. It was easy enough to confirm with my digital multimeter as well but I didn't want to crimp it the wrong way and (I think) ruin the connector.
Since you could plug the connector in either way, it doesn't really matter which way the red ribbon cable goes, as long as it's the one you end up plugging it in so it ends up at pin 1.<br><br>It would be important if the pins were set in a socket matching the header, which would be a much more elegant way of doing it than the 6 bare pins I recommended.<br><br>I've added a note in the picture to show which one was the red one for those with less sharp eyes than you, and added a bit more info to the following step. Thanks again for the feedback!
I used 20ga wire (or 22ga? it was solid wire rated for 300watts) for the 120v portion and nothing seems to be heating up or catching on fire yet. I didn't see a mention of a size for the 120v portion, but it seems like we're dealing with such a small amperage max (limited by the 5A fuse) that it should be OK.
According to http://www.powerstream.com/Wire_Size.htm, 22 gauge wire is appropriate for up to 7 amps, so that's why I specified that size.<br><br>20 is good for 11, so you are twice as good. :)
I finally had time to finish my stoplight, thanks again mkanoap for this great instructable. <br><br>I ended up purchasing the LED model stoplight from LightsToGo LLC - it really didn't have room inside the lights to put much of anything (except the 5amp fuse) so I have my boxes outside the stoplight. <br><br>Here's my mouser list of parts in case anyone else is interested, I started without any parts, just tools:<br><br>1 - 2x3 socket for ribbon cable: 649-71600-106LF<br>5 ft (plenty extra) - 9 pin ribbon cable: 523-191-2801-109<br>1 - 9 volt power supply: 552-PSC-12R-090-R<br>4- NPN Transistor: 863-2N3904RLRAG<br>1 - Arduino kit (the breadboard and jumpers similar to what's in the LED test setup): 782-A000032<br>4 - Relays: 817-LZ-9HE<br>1 - 36 pin block of headers: 649-68001-436HLF<br>4 - Diodes (rectifiers): 625-1N4004-E3/54<br><br>I couldn't find the right screw terminal blocks on mouser (the ones I bought didn't have legs to solder through the PCB). I found them at Radio Shack 2761388 PCB Term 2P 5MM. I also bought 22 gauge wire there, as I said I really didn't have any materials when I started. <br><br>A friend gave me some 1/2 watt 1K ohm resistors, they weren't in the list but they're needed. <br><br>This is some other stuff I got that is pretty obvious but I'll list it anyway:<br><br>1 - Arduino Ethernet shield: 782-A000056<br>1 - Arduino board (it is the Uno): 782-A000046<br>1- Arduino enclosure: 782-A000009<br>3 - Red/Yellow LEDs: 606-4301F11/17<br>3 - Red/Green diffused LEDs: 607-4301F1/5<br>6 - 1/2 watt 180 ohm resistors: 660-MF1/12LCT52R181G<br><br>I'm a newbie and I wanted to follow the instructions as closely as possible so as not to mess myself up by diverging too much from the original project. It was tedious wiring everything on the protoboard myself. If we like this stoplight as a way to get notifications on the build I'm probably going to build some more, and I'll have the PCB printed for me at that point - maybe even dabble with POE, or try making it wireless somehow.
It's great to hear that you built it, and I'm happy to hear my instructions were helpful.<br>I was going to say &quot;what do you mean the resistors aren't listed?&quot; But then I checked and saw that yup, they are on the schematic, but not on the parts list! <br><br>Sorry about that! All I can say is that they WERE on the project list that mouser deleted. :)<br><br>I'll update the article to reflect the missing resistors, and see if I can't dig up the screw terminal block part number.
Hi, im a small website server owner and i have built your project. Could you please possibly post an example script?
nervermind i worked it our, but for people who want to control it without a browser and dont know how heres is a basic html line you can add to control it<br> <br>&lt;.script GET src =&quot;http://stoplight:port/bsecretredgreen&quot;/&gt;<br> <br>is an example of turning red and green on<br><br>except get rid of the dot before script(its just so instructables doesnt treat it as a html line)
So...I notice the traffic light in the picture is RED! So while you were taking pictures for this instructable for our benefit, your servers were flaming out.
Ha! That's the kind of sacrifice I'm willing to make for instructables. :)<br><br>But seriously, two important points.<br>1) It's a good policy to alert BEFORE a server is in an unusable state. Red might mean &quot;the disk is 90% full&quot; or &quot;cpu load is twice as high as normal&quot; as well as &quot;server not responding.&quot;<br><br>2) If you don't yet have your monitoring system hooked up, this light just displays whatever you last set it to with the web gui.<br><br>
Thanks for putting up such a great pricing and sourcing list, by the way. It's always discouraging to look at a great instructable and not be able to guess whether it's pie in the sky or actually doable. It's less intimidating when I have a link for unfamiliar parts and can identify the pricey parts (and brainstorm ways to bring down their cost). <br> <br>Actually, I would love for instructables to give authors a structured way to list the materials. Perhaps a search-as-you-type field that would then provide resource links and average prices. I know they're resourceful enough to do make this happen...
probably a way is to skip the standalone option and link it with a computer<br>Maybe home servers would benefit from this because they can attach it to the server itself (which could save the user from a trip to where it is placed, which is usually hidden)<br>
See the notes for a similar project which works that way, directly connected to a computer.<br><br>Some of the articles that appear automajically to the right of this one also may be pertinent.<br><br>If you don't need the Ethernet ability, you can save $45 right off the bat.
Thanks. I have shared those frustrations in the past and that's why I did it. Nice to know it's appreciated.<br><br>It was an even better list before mouser deleted the project so people can't just put it in their shopping cart. :)
suggestion, make it dim the lights and activate red beacons if there is a really serious error
A beacon was in the original design and there is even a relay still there for it, but we found that flashing the red light was sufficiently eye catching for the &quot;drop everything&quot; events.<br><br>I like the idea of dimming the lights for added drama.
This is a really cool project and I'd like to give it a try myself (I'm a bit new to this arduino stuff but I've always had a knack for soldering). <br><br>The mouser project links take me to an &quot;arduino&quot; project that only has 4 items listed, and when I try searching for the &quot;NPN Transistors 2N3904&quot; there are dozens to choose from. <br><br>Also I'd be interested in finding out more about which arduino ethernet board would be the drop-in replacement? (I found 4 in a search on mouser). <br><br>And one more request, sorry, any recommendations on where to get the PCB printed? If this first one works well I might make a few more :-)
Rats. It seems that mouser didn't like me sending all that traffic to their shopping cart and summarily deleted my project. Highly irritating and a bit short sighted I think.<br><br>I'll try to figure out the part numbers from PO and update the article tomorrow, maybe with digikey parts. :) Honestly though, I just picked the cheapest. 863-2N3904RLRAG is a whole 3 cents cheaper and should do fine for those transistors.<br><br>The ethernet arduinos are hot off the presses, so I'm just speculating here. I'm thinking A000050 would be the closest replacement. Note that this model requires a special cable/adapter, not just a regular USB cable. It looks like A000060 is the same thing with the the new version of that adapter stuck on.<br><br>The other two you found are versions of those two with POE (power over ethernet). I would have LOVED to have built this with POE and eliminated the separate 9 volt adapter, but it was not yet available. Most switches don't provide POE, so don't bother with those unless you know you can use it.<br><br>I have had good luck with http://batchpcb.com for getting single boards printed at $2.50 per square inch plus $10 setup per board. But you have to wait a few weeks sometimes for a board to fill up. This board is about 7.5 square inches so it would be around $29.<br><br>Similar services I want to try but have not yet are:<br>http://dorkbotpdx.org/wiki/pcb_order ($5 per square inch, no setup and you get three copies! But you also have to wait for a panel to fill up with other people's orders. That would be $37.50, but only $12.50 each if you needed all three<br><br>http://iteadstudio.com/store/index.php?cPath=19_20 ($24.90 up to 10cm X 10cm (which would probably fit this board), and you get 10 copies!) You could throw out 9 and still be cheaper, or use all 10 at $2.49 each!
Newark Electronics is another supplier option. They have the widest selection of any supplier I know of. And I think they have a parts list service.
There's a great company in upstate NY just south of Albany that sells traffic signals and parts: http://www.twingreenonline.com/signals.htm , they have LED traffic lights and it's a lot cheaper than the site mentioned in the Instructable for either LED or incandescent signals.<br><br>
Thanks, I'll make a note of it. Looking at their page reminds me that you can also get red/green lights, leaving out the amber. That's a good option for anyone who doesn't need the yellow to cut down on the cost.
Great idea. So much more high tech than what we had years ago. Each server was represented by a small toy animal that were kept on top of a cubical wall. When (not if) the server went down, someone would tip the toy nose-down. Everyone had to stand up to see if the Giraffe server was dead in the water, but the system worked. <br><br>I like your instructable better.
Now I'm envisioning some systems with solenoids to automatically tip the toys down and back. That would be cool.
Hi, nice job!<br> for us, europeans that visit this place, what would we need to make this project with 220 V bulbs?
I considered talking about that but in the end didn't because I simply don't know, and didn't want to speak from ignorance about stuff that could get people electrocuted. I have an irrational fear of 220 volt power. :)<br><br>Also, I know nothing about European wiring codes.<br><br>I will however say this:<br>1) Those relays are rated for 2.5 amps at 240 volts each, so they should not melt or anything.<br>2) The calculations I made for the &quot;don't plug more than this in&quot; rely on the equation &quot;current (amps) = watts/volts&quot;. So I looked at the bulbs (40 watts originally), added all three together and got 120 watts. It's easy to divide 120W/120V, so I knew that the lights would be drawing 1 amp max. That told me a 5 amp fuse would be plenty, and leave room for current draw from the optional socket. So the key number to consider would the be the wattage of the bulbs. If (for example) you had 60 watt bulbs, the calculation would be (3 X 60) / 220 = .82 amps. Still plenty enough to kill you if it crosses your heart, but even less amps than the 120 volt version.<br><br>So I'll tentatively say that the only modifications to the instructions would be for the plug, which I didn't have a picture of anyway.<br><br>If anyone sees anything blatantly wrong with the above assumptions, please comment before something tragic happens.
Great idea. I wish my work would adopt something like this.
Thanks for this write up. I've had a traffic light for years since I got one at a Dept. of Transportation surplus auction a long time ago. Not as long ago I got an Arduino and an EFX-TEK RC-4 relay board (http://www.efx-tek.com/topics/rc-4.html) to control it with, but was getting stuck on the little fiddly bits needed to connect it all up and where to get them. I went with the pre-made relay board so I didn't have to do as much on the 120 V side of things. This will help a lot.<br><br>I plan to set mine up a bit differently, adding some other sensors like a distance sensor so I can put it in the garage and have it tell me when to stop, or an audio or stereo connector so it will blink in time to music. Plus just do normal traffic light-style blinking.
I love that idea about having the light tell you when when to stop the car, it sounds like your light will end up being more fun than mine. :)<br><br>That board looks pretty nice and cheaper than getting a single copy of my (hypothetical) board printed. It's too bad the cost of the Solid State Relays drive it back up again, but it's still an attractive alternative.
Now you can get the Arduino Ethernet board for $60. That should cut a little off the total cost.
Indeed. And with the reduced height of the arduino ethernet board leaving more room in the light, the enclosure becomes even less important. +$32 saved!<br><br>I wish it had been out when I built this.
They have POE modules! Now I REALLY wish it had been out when I made this.
Very nice instructable, and voted for you to win a frickin' lazer beam!
I need something like this so that we all know when a particular co-worker has left his office..... RUN!! HIDE!!
Nice cat.
This is a great write-up with excellent pictures and plenty of detail, and a nice project to make monitoring server status more fun (if such a thing is possible). I think we need one for monitoring when the kettle has boiled in my office :)
Thank you very much!<br><br>If you did make one for monitoring the kettle, you could (assuming your kettle doesn't draw more than the safe amperage) plug the kettle into the auxiliary outlet and have the server turn it off. :)

About This Instructable


219 favorites


Bio: He's just this guy, you know?
More by mkanoap: Growing up with halloween Build an electronic polyhedral die Build an electronic score keeper/storage box
Add instructable to: