Switching 120VAC With 5V Digital Logic

After building my glorified 2 outlet power strip for my switchless desk lamp, the natural next step is to build a switched outlet that can be electronically controlled. I separated the two outlets electrically, and connected them to opposite poles of the relay, so one of them is normally open, and one of them is normally closed, and they reverse when the relay is energized.

NOTE: It is important for me to be exceedingly clear that this is primarily meant as a prototyping device.  The gang box is used as an enclosure, not because it's meant to be installed in a wall, but because it's the perfect sized enclosure.  You are dealing with mixed high and low voltages here, so implement at your own risk.

How it works is that it has a double-throw relay inside, which is controlled by the 5V logic connected to the terminal strip on the side of the junction box, and switches power between two separated outlets, so you can have either a positive output, or an inverted one, depending on what you're doing with it.
The three terminals are power, ground, and control.  This is because the relay draws 65mA, which is much more than TTL logic is designed to drive, so I used a transistor, which only draws 4mA, to switch the higher current, which is sourced directly from the TTL power supply.  The diode is called a freewheel or flyback diode, and is used to suppress the voltage spikes caused by the inductive nature of magnetic relays.

Parts list:
  • Extra deep single gang junction box (approx 65 cents at hardware store)
  • Duplex 120V outlet (Less than $1 at hardware store)
  • Single gang cover plate, matching color of outlet (Less than $1 at hardware store)
  • 2 6-32 screws to assemble outlet
  • 4 contact terminal strip ($1.10 at Halted)
  • 2 8-32 screws + nuts to attach terminal strip to junction box
  • Old 3 conductor power cable (spare computer cables are perfect for this).
  • Relay with a 5V coil, and 250V contacts. ($1.22 at Digikey)
  • 1N4002 diode ($0.26 at Digikey). This is not a critical value; Feel free to use any value diode.
  • 2N2222 NPN transistor ($0.49 at Digikey). Note that I used one from inventory, which happened to be in a TO-18 case, instead of the more modern TO-92. Don't worry about it.  Even a 2N3904 would work.
  • 1k resistor ($0.064 at Digikey)
  • 2 pieces heavy gauge (at least AWG14) wire.

First is preparing the outlet and junction box.

Normally, both plugs in an outlet are tied together, since they're all on the same circuit.  I wanted one of the two outlets to be the inverse of the other, so this had to be fixed.  Outlets are designed so that you can just cut a tab between the two screws to electrically separate them.  We only need to cut the tab on the hot (brass) side, since they're still going to share the same neutral.

For the junction box, drill two holes to attach the terminal strip.  I used a terminal strip because this is primarily meant for prototyping, so I want strain relief while still being able to easily connect and disconnect it.
And remove one of the plastic tabs on the bottom for the three TTL wires and the 120VAC power cable to come into the box.  You can punch it out with a screw driver, or your bare hands, if there is an attractive female watching.

Now fire up your soldering iron and start making some magic.  This was my first attempt at dead bug construction, where you glue the parts down upside down and solder them in the air.  Using hot-melt glue was probably a bad idea, and I probably should have covered the 5V stuff in something to protect it from the high voltage lines running on top of them... Food for thought...

Cut off a few inches of insulation from the power cable, and wire up the outlet. Be sure to feed the power cable through the junction box before soldering it to the relay!!! (Not that I would do something incredibly stupid like that... honestly...)

Once it's all wired up, feed it all into the junction box, and screw together the outlet and face plate (Of course, you tested it all before tightening down the screws, right? That'd be pretty silly to close it all up, and then have it not work...).  Wire up the terminal strip, and you're done!

There is quite a few possible uses for this thing.  We've come up with using it to automatically turn on appliances like a water heater right before your alarm, so that you can wake up to hot water for tea; or flash the lights a few times before turning them on to wake you up in the morning; or even using it to turn off a fan a few hours after you go to sleep during the summer so it doesn't run all night.


  1. that's really cool. Why not use a solid state relay though?

  2. Because the cheapest one I could find that could switch more than 1A was something on the order of $20. That resounding click is worth $15 to me. I'm looking into switching 120VAC using a TRIAC, but I just got my last order from Digikey last week, so it's going to be a while before I get together enough stuff to justify another order.

  3. Try a triac. http://www.youtube.com/watch?v=qHVtnolmr1k

  4. I get my small qty parts off ebay from china, shipping isnt fast but its free (6 triacs for a couple dollars)
    Love the TP Tube breadboard!

  5. Nice and simple, I like it.

    You might consider a "zero-crossing" detector however, as switching AC during the high portion of the sine wave will eventually cause your relays to pit, scar, and fail from the internal arcing. By sampling the AC wave and switching the relay on/off only during the zero crossing of the sine wave period - that will make your relay last for years.

    my two cents...

    ;-) Tom

  6. You should add a base-emitter resistor, something like 10K. If you leave the base unconnected noise(50/60Hz) might turn on the transistor - and the relay. It happened to me a while back while switching some lights with a very similar circuit.

  7. Thanks for the comments.

    The zero cross detector sounds like a good idea for when I end up with a longer term project. As it is, I'm just planning on playing with this, so I don't expect to approach any kind of reduced lifetime for the relay anyways.

    I'll keep the BE resistor in mind. The base is normally driven low by the TTL output driving it, so I don't expect it to be a problem, but we'll see.

  8. Great project. Imagine deriving the +5 from the USB port on your PC; you could then have a truly green PC that completely switches off its monitor, speakers, printer et all, when you hibernate/sleep/shut down the CPU!


  9. Try something like a MOC3043 zero crossing detector optocoupler. If you couple it with a triac you basically get a good switching method, similar to a SSR. Don't forget the snubber for inductive loads.

  10. Stay with the relays, triacs fail on or half on.

  11. Forgot to mention, but if you do drive the relay thru' the USB +5, you can safely get rid of the transistor/resistor and drive the relay (like the one shown) directly. DONT forget the disode, though!



  12. I did this same thing, but with a 12v relay(after seeing this). It's fairly easy to fit a 12v wall wart in the gang box. Now I just connect ground to one wire and logic HIGH to the other, and the relay switches.

  13. wow, I build almost the same but I had my relay connect parallel with a wall switch so either ttl logic or wall switch could fire up my outlet

  14. how do you feed the 5v to power the low voltage circuitry? Do you create a 5v power supply from the provided 120v? thanks!

  15. @Anonymous: Yeah, the 5V is from whatever power supply the rest of the circuit is using, though I have this setup such that if you wanted to use a 12V or 24V relay, you could do so, and even integrate the power supply for such inside the box.

  16. Have you considered what happens if one of those soldered wires drops off and makes contact with your logic part of the circuit. Make sure both sides are really well tethered with physical fixings like a tie wrap, such that if the solder joint fails the tail cannot touch anything and maintain good separation distances throughout.

  17. so how would you power it up, can you run a regular power cord, running from wall. then cut the end and run it like that? i want to do something like this but with like 4 different outlets. then want to run my arduino to it, and controll it using processing and touch osc on my ipad. want to be able to turn on and off a lamp, tv, ps3 ect. is it possible? already have my code and ipad set up to arduino and processing, just need to figure out if i can make me a something like what you made, with like 4 relays to control different thins.

  18. Thanks! I have been thinking about something to switch on/off a heating lamp for my mini winter greenhouse. This or a Triac version looks like it might work, however I am also thinking about adding the guts from a USB power wallwart to supply power to my arduino.

  19. its one thing to have a great project, its far more difficult to properly describe it, give directions and most importantly have a complete list of parts used. You have completed the trifecta. thanks for sharing.


Post a Comment

Popular Posts