Using a Phidgets relay, and how electrical wiring works

From a project a while ago, I had some extra Phidgets laying around. Specifically, I had an interface board, and a dual power-relay. If you are not familiar,

are interfaces, controllers, and sensors that you can hook up to your computer via USB. If you want to use controllers or sensors in a standalone fashion, then you need a Netduino, Gadgeteer, or Raspberry Pi. However, if you are OK with the electronics being tethered to your computer in order to work, Phidgets are a good way to go.

The Problem:
The problem which immediately came to mind is this: in my office, I have two paper floor lamps in the corners. However, my desk is in the way, and then I have full-size speakers in front of them too. The result is that I can’t really get to the switch on the floor to turn them on and off, but I’d like to.

The Easy Solution:
I could probably just go get an extension cord with a switch, and use some standard hardware, and that would’ve solved it.

The Complexticated Solution:
Instead of doing it the easy way, I figured I could have a multiple-birds/one-stone sort of scenario. I could:

  • Use the phidgets that I already have
  • Gain the ability to turn things on and off from my computer, instead of a physical switch
  • Learn more about electricity

So, I set out to build a junction box with some outlets, hook those outlets to the phidgets relay, and then plug that junction box into a wall outlet. Here’s an overview of how that is laid out:


I learned a lot in this little project, so I wanted to write it down so I’ll have it as a reference. However, if you want to try any of this, I have to warn you:

WARNING: Electricity can easily kill you. I strongly suggest you not attempt anything you see in this blog post. Consult a qualified electrician if you have any interest in this!

OK, hopefully you get that this can be very, very dangerous. So let’s move on. Looking at the picture above, hopefully it’s somewhat obvious about how this will work:

  • I plug the black power cord into the wall.
  • The bottom outlet is live all of the time.
  • The middle outlet is switched on an off via the 2nd relay (inside that acrylic case in the bottom-right of the picture)
  • The top outlet is switched on and off via the 1st relay (inside that same box, they are right next to each other)
  • I will have to write some software that allows me to turn those relays on and off – similar to turning a light-switch on and off.
  • Oh, and I happened to have a Light Sensor and a Temperature Sensor, so I threw those on there too!

With all that said, let’s break this down and dig into each section.

What is electricity?
I struggled with this question most of my life. I just didn’t quite understand it – and I still don’t, really. In short, as I understand it, electricity is an imbalance of electrons that are “bumped” from molecule to molecule, trying to stabilize. Trying to “find ground”, or a place to dissipate. Meaning, it is always looking to complete the circuit.

Electricity doesn’t flow TO any place. Instead, a device (when hooked up properly) DRAWS current from the electrical source. With that in mind, you want to be deliberate about what gets in the path of the electric circuit, because if it offers a pathway to ground, then it is going to take it – and that includes your human body!

What is a switch?
A switch simply makes it so you can turn something on or off. It does this by interrupting the circuit. In the diagrams below, the light-gray wire represents the “common” or ground wire, and the black (which you should think of death!) is the “hot” wire, from where electricity flows.



When the switch is on, it completes the circuit and the device gets electricity.

What is a relay?
A relay is similar to a switch, but it has a mechanism that lets your remotely switch a circuit on a off. The switching mechanism is separate from the triggering mechanism.



In this case, we use very low voltage (like 5V DC) to charge an electromagnet. The electromagnet then controls the actual high-voltage switching mechanism. The electromagnet can be used to push or pull the switch open or close. The example above is a “normally open” relay where you must activate the relay to turn the switch on. You can wire it up the opposite way if you need too, where the circuit is “normally closed”, and you activate the relay to turn the switch off.

There are a couple of reasons why you would use a relay. First, I’m controlling this witch electronically. I need to “flip a switch”, and using an electromagnet is the easiest way to do that. However, another added safety bonus is that I now have separation from that circuit. Imagine I have 100 volts flowing through that line and there is a power spike? If my electronics (and subsequently my PC) were connected, that power surge would fry everything!

So, it is good to use a relay as a separation between high-voltage and low-voltage devices.

Common House Wiring:
One thing I wasn’t clear on was typical house wiring. if you’ve ever taken the wall plate off a switch or power outlet, you see lots of black, white, green, and/or bare wires. What’s all that about?

  • White is your “common”, or ground – which completes the circuit, when power is going to the device.
  • Black (think “death”) is the live wire – from where electricity flows from your power panel.
  • Green (or bare) is a safety ground. This is hooked to junction boxes and anything else metal to complete the circuit (even if it’s a short circuit), so that you don’t get electrocuted. In other words, if you had a black wire touching your metal junction box, you won’t necessarily know until you touched it and died. With the green wire connected, the black wire touching the junction box would short immediately, and hopefully saving you from getting electrocuted.

Here are some important, random notes too on house wiring (and I still HIGHLY recommend you talk with an electrician too, if you are interested in any of this):

  • Don’t do anything until the power has been cut to the box you are working on.
  • Use a tester to test EVERY wire in that box to CONFIRM that the box as no power going to it.
  • Make sure to use the proper size “wire nuts”. Consider wrapping your wire nut connections in electrical tape.
  • Make sure to screw down the screw terminals on the side of outlets to reduce the chance of arcing or short circuits. If you are work on a bare-metal junction box, consider wrapping those screws with electrical tape, by wrapping tape around the perimeter of the outlet.
  • Don’t ever run wire into a junction box that might move, without a grommet. If you have an outdoor box, or test box like I have, get a grommet that makes the cord immovable.

Wiring It All Up!
OK, enough background. How is this all put together? First, I gathered some supplies:

  • An outdoor, “3 gang” junction box. I got an outdoor one because this will be portable, and I didn’t want sharp edges or any holes on the outside. The outdoor boxes have smooth edges and are painted.
  • A “replacement cord” that was 3-prong, and had 14-2 wire – which should be fine for this. The relay supports up to 10amps at 110volts.
  • A outdoor (water-tight) grommet to secure the incoming power cable, and makes it immovable.
  • A pack of various-sized rubber grommets for the relay wires that enter the box.
  • Double-stick tape to secure the relay and interface board.
  • Electrical tape to secure the relay screw holes (they are otherwise exposed, causing an electrocution hazard!).
  • Various size wire nuts.

Then came the part about wiring this up. Now that we know all the pieces of this, this should be pretty logical:

  • The incoming green wire can be hooked to the outlets, and then terminated against the green screw in the box.
  • The incoming white common can be daisy-chained across each outlet (note the directions on the back of your outlet as to where this connects)
  • The incoming black needs to be split (with a wire nut). One lead goes to the “always on” outlet (picture: top). Then two leads need to go outside of the box to the two relays. Then two more leads come from the other side of the relays, and feed power to the other two outlets

Here is a diagram:


And here are some pictures – first, a close-up of the relay. The power feeds into the middle screw of each relay, then the switched power (which might be on or off) comes out screw to the right (on each relay) to the respective outlet. Note too, the rubber grommet before it was affixed in the access hole:


Next is the actual wiring of the outlets. Notice on the left, the green wire terminates to the green grounding screw inside of the box – there are a couple.


Bottom line:
Well that’s about it. This was a little side-project I’ve been Phidgeting with for a while and now it’s pretty much all set. Phidgets code is pretty easy to write, so I whipped up a little app to switch the relays, and to read from those two sensors:


I currently have a couple of problems:

  1. The interface board, which is connected via USB simply disconnects are completely random times. With a lamp plugged in, that means that the light suddenly goes out too – which is jarring. 🙂
  2. Both of the sensors show nonsense values until the sensor changes at least once. So, you see bizarre values until several minutes later when there is a light or temperature change.

As time permits, I’ll see if I can get to the bottom of those issues. Meanwhile, hopefully this post is useful – especially to Future Robert!

Posted in Computers and Internet, General, Infrastructure, Sensors, Sensors and Controllers, Uncategorized
2 comments on “Using a Phidgets relay, and how electrical wiring works
  1. Kat Dornian says:

    Hey Rob, great job figuring this out. One thing you could do in your program is to poll the sensors once you initialize the Phidget object.


    • Robert Seder says:

      Thanks! Yeah, that was on my list of things to try. With the controller board resetting, I assume that is because I have it plugged into an old/cheap USB hub. I’m going to try plugging it directly into my PC. It’s not that it disconnects at the USB level, it just shows disconnected (by firing an event in code), and then immediately re-connecting. I’ll dig back into this again this weekend


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

%d bloggers like this: