loading

Always wanted to take total control of your electric appliances even without being at home? Well, thanks to this instructable you will be able to do that and much more.

Some friendly suggestions before starting:

  • Electricity is very dangerous, read and follow the "DANGER:" advices written in this instructable.
  • If you don't have any experience in the field of electrics/electronics seek for supervision from a knowledgeable person, although the circuit it's very simple If you don't know why you are doing what it's being explained the odds are if you make a mistake you won't be able to notice it, leading to unpleasant consequences.
  • NEVER EVER work on the circuit while it's connected to the grid, also don't do it with it connected to the arduino, some tin solders transmit small amounts of current that can damage it.
  • A steady power supply is recommended if the relay will be on during long periods of time.

To build a plug to plug arduino based-controlled switch you will need the following materials:

-1x plug to plug with manual switch (we want our circuit to fit inside).
-1x 5V relay (that holds 240V and 10A, check the datasheet of the one I used here).
-1x 2N2222 (also called PN2222) transistor.
-1x IN4002 diode (I used a 4007, small variations are acceptable).
-1x 1000 to 500 Ohms resistor. (I used one of ~750Ω)
-1x Prototype board (1,5x2 inches should be enough).
-1Strip x Female pin header (just for convenience).


Total cost: ~$6.

And the following tools:

-Microcontroller (preferably an original Arduino® board, notice I'm using a cheap imitation).
-Tin solder.
-Multimeter.
-Thermofusible glue gun + some glue (epoxy would work even better)

Step 1: First Build and Test.

First we want to mount and test all the components just to make sure we don't do a lot of work to end with a defective circuit, you can find some pieces of code at the step 6.

We can do this easily following the diagram shown above connecting the 5V to the 5V supply pin of the microcontroller and GND to the ground pin, i/o pin will be connected to the pin we want to use to send the signal, 10 in this case, and will have a resistance attached to it, the value might vary depending on the voltage used, mine works best with a 750Ω resistance, too high and it wont switch, too low and energy will be wasted.
To place the diode notice that they usually have a white band around the anode (the side of the "bar" if we talk about the symbol).

The reason for the diode and the transistor to be there is to protect your board, the diode redirects the voltage peak the coil induces to itself when the power supply is cut and the transistor acts as a protective shield for the board, so it can operate behind it without being affected by any harmful current variation, the resistor just limits the amount of current the base of the transistor can draw, making the circuit more efficient.
Connecting your microcontroller directly to the relay would end permanently damaging your chip.

IMPORTANT: If you want to make a "value=digitalread(button)" and then an "if(value==HIGH) {}" statement (just what I did) you'll need to place a resistor in the order of some hundreds of thousands of Ohms from the pin the switch is placed to ground, that will avoid getting wrong results, and getting frustrated.

Once it's all tested we can move on to the next step.

Step 2: Soldering It.

We begin by placing all the components in it's place, you might find it's quite difficult to insert the relay, that's because the pins are not usually aligned with the board and the COM pin is also touching it, that's why you see that little hole in the picture.

I have not added the resistance to the circuit because I wanted to experiment with more resistances, I ended placing it at the signal wire, I suggest you to place it directly on the definitive circuit just for convenience.

When you have managed to fit the relay in, place and secure the other components.

TIP: It's useful to bend the legs of the components so they stay in place, also, if the relay is sturdily placed you can tape the transistor to it for a more even and easy soldering job. 


Now we're going to solder it permanently, I've uploaded pics of the final result so you can figure out how to connect it if you have some problems.

DANGER: I completely removed the cells of the board around the high voltage contacts (safety zone) with a sharp blade and some patience, I strongly recommend you to do the same to have a lower chance of having short circuits and to respect the standard spacing without making the circuit bigger, if you don't remove the cells, you must space the components at least 2 cells in order to respect it.
No connections can be made inside the safety zone, which will have a radius bigger than 1,8mm over the naked board around the high voltage terminals.
If you can't respect the safety radius, even by removing some cells, relocate again the components.

Please understand the importance of doing this.


You can also remove the useless cells before mounting the circuit by taping the cells you want to use and sanding them while the others are protected by the tape.

The "optional route" it's useful if you're going to use this circuit with low currents to make the cables stay at the right side and not below the board.

The "optional contact" is normally closed in most of the relays available, it is useful to use this contact when you are going to run devices for long periods of time only disconnecting them briefly, when connected to this contact, a HIGH output will mean the device is off while a LOW output will keep them on.

Step 3:

Once you've completed the circuit, test it again with the microcontroller, you should hear a "click" sound when the S pin gets 5 volts, if not check your circuit and code (The code I provided has been tested and guaranteed to work).

I recommend you to clean the space between the connectors (tin, residues...) with a brush or a piece of cloth and 96º alcohol.

At this point you can use your circuit to control small DC currents, such as 12-24V batteries.

Step 4: Plug Modification.

Next we want to integrate the circuit inside our plug to plug switch, when we open it we must get rid of all what it's not going to be used, in this case, the switch and a cable to power the light in it.

Notice that in the third picture the "base" for the switch has disappeared, I removed it with pliers and melting the plastic away to make room for my circuit, always trying not to damage the case.

I also made a small hole to pass the wires through when I mount the circuit.

When the switch and other useless things have been taken out cut and strip the brown cables to an optimal length to connect the circuit.

 

Step 5:

We're now ready to put our circuit inside the case, tin weld the cables, make sure you make an sturdy soldering job, we don't want high voltage cables flying around.

DANGER: make sure there isn't any loose piece that can enter in contact with the circuit, put the board away from cables, metal pieces, screws...

Once you have welded them put a bit of thermofusible glue under the relay to keep the circuit fixed to the case, then put thermofusible glue all over the circuit, covering all the exposed contacts, the more the you put the safer it will be, just make sure the case closes correctly.
The glue will keep the contacts always clean and will prevent short circuits.

I covered the hole were the switch was placed with a piece of plexiglass attached with thermofusible glue.

DANGER: cover the hole in which the switch was fit and other possible holes, specially if someone can stick a finger inside. If you can't close the case tightly or the integrity of the casing is seriously compromised detach the case and start again with another one. Never leave it plugged if someone can touch it or mess with it. A closed case with a closed relay should be completely spark proof but it's always recommendable to follow the common sense, that implies: never leave it plugged in for long periods of time, specially when using a high switching frequency (e.g: using sensitive sensors/code, trying to make an strobe light...) this not only decreases the life of the relay very fast, but the temperature caused by the sparks made by the contacts inside the relay could melt the plastic and cause a fire.
Understanding this risks is crucial, I've tried my best to cover all the safety related issues.

Step 6: The Code

If you have made your way to here I guess you'll have some sort of experience with C, anyway I'll provide you some pieces of code, just in case you are a lazy guy like me.

-Activate the relay for "x" time and then turn it off:

Download here

-Activate/deactivate the relay with the same button:

Download here

-Turn on and off a lamp at random times between defined by an interval of time (suitable to avoid burglars):

Download here (fixed)

You can also set another pin as an output and place a led just to know when the relay is on or off.

Step 7:

Once you have the circuit connected and the code loaded it's time to test it, after having done a good inspection of your work (look for short circuits with the multimeter) it's time to connect it.
 

DANGER: You never know what will happen when you connect it for the first time, if possible test it with a variable power supply, In favor of security, I connected it to a power strip and pressed the switch from a safe distance, but after having checked it so many times I was quite sure it wouldn't fail.

IMPORTANT: Relays have a small contact that it's moved to close the circuit, that contact heats when closing or opening the circuit, the more amps the circuit has the hotter it gets, to the point of welding the contacts if you use with extremely high power consuming devices.
Following the charts you can see the recommended current for each voltage AC or DC, for AC a resistive load would be an incandescent light bulb, a heater,... and an inductive load would be, for example, a motor.
For example, the maximum power you can use with resistive load is ~2200W, giving the relay a rough life of 100000 cycles.
Notice that AC loads with sinusoidal current most of the time it is less than the peak current so the average temperature rise is lower on the contacts than with DC current.
Using a breaking amperage somewhat higher than the recommended won't immediately damage the relay, but it will exponentially decrease it's life.


That said, be careful and enjoy your microcontrolled switch.

Thanks for viewing.

P.D: Due a trademark violation some pictures have been erased, sorry for the unconvenience.

<p>it took some time but i made one to. thanks for making the instructable i had fun reading your instructable and making this for my arduino.</p>
<p>Thanks for posting this instructable. I built one myself last night. Very easy to follow, while still highlightning the dangers involved. I especially liked the picture of the PCB with the traces pointed out :), it made it easy for a beginner, such as myself, to build something new while feeling relatively safe not to mess up the routing.</p><p>Only issue I had was that my leonardo kept turning on/off the relay all the time... Apparently it was unable to power both the transistor and coil while powered through USB :/ Applying external power to the transistor solved that problem though.</p>
<p>Make sure you have a resistor of at least 500 Ohms between the Arduino and the base of the transistor, if you haven't done this the base will draw too much current, even surpassing the limits of the Arduino, this would explain why you need an external power supply for the transistor.</p><p>Also the relay must be rated at 5V, with 6V ones you might have problems.</p><p>My circuit works just fine with the 5V output of the Arduino, even powered with a 9V battery it still works not showing any signal of excessive draw (e.g: power LED dimming), but this is only when using the resistor, without it the circuit clearly draws too much energy.</p>
<p>I put an 800ohm resistor between the arduino and base, so that should be fine. And the relay is of course 5V, I can even trigger it with 3xAA(slightly used) batteries.</p><p>I think its something either with the leonardo board or my USB port(my guess is the leonardo board), because when only powering from USB the output voltage fluxuate quite a lot. Powering the board externally gives a nice rock-solid 5V output.</p>
<p>What do you mean by &quot;<em>A steady power supply is recommended if the relay will be on during long periods of time.</em>&quot;?</p>
<p>I meant a power supply obtained from the grid with an adapter, just to avoid draining the battery.</p><p>Sorry if I expressed it in a confusing way.</p>
thanks :)
Please contact me regarding this device
<p>Cool project. I've been wanting to find a way to remotely turn off appliances from one main switch which would send a signal (over a home network perhaps) to turn off all &quot;listening&quot; devices. Imagine going to bed / leaving the house, flicking a switch by the door to kill all phantom loads, hot water heater, etc. A &quot;whole home&quot; off switch.</p>
<p>I've been also thinking about using an ethernet shield (or a Raspberry Pi) to control it.</p><p>Sadly I just know to program with the windows console variant of C and I still have some problems to program my arduino, I will spend this summer learning java, c++ and maybe even something about python or android app developing.</p><p>About your idea, just make sure not to exceed the relay capabilities, also not to turn off the fridge :)</p>
<p>Please reconsider spacing between mains and low power side of the arduino! Since you are not using any optocoupler, you could accidently get 230V on your arduino - not even knowing, if you are using a floating power supply. On touch, the current would flow through you to earth.</p><p>For a safe construction you would use 1,8 mm and more spacing between the mains side and everything else.</p><p>Further reading on:</p><p><a href="http://www.smps.us/pcbtracespacing.html" rel="nofollow">http://www.smps.us/pcbtracespacing.html</a></p><p>(also image source)<br></p>
<p>I sent you a message about this issue, please read it and tell me your opinion.</p><p>Thanks for your time.</p><p>This instructable will be immediately deleted if it implies any potential risk or threat to the security.</p>
<p>I wouldn't want you to delete your instructable, but please add the discussed safety spacing changes.</p><p>Everyone else might also consider the &quot;pollution degree for electronic equipment&quot;:</p><p><a href="http://www.ni.com/white-paper/2871/en/" rel="nofollow">http://www.ni.com/white-paper/2871/en/</a></p><p>Cheers!</p>
<p>I've used a solid state relay for this purpose, they're very cheap on <br>ebay and they contain an internal opto-isolator which draws very little <br>current so you drive it directly from the arduino, no transistor <br>necessary just a 220 ohm resistor.</p>
<p>pros: good and save industrial alternative</p><p>&quot;cons&quot;: not so mutch diy, takes a lot more space, sould be more expensive if it's done right (china parts might have some cheap, unsafe solution inside)</p>
<p>I'm not very comfortable with my soldering skills - How do you think this board(link below) would do as part of this project?<br></p><p><a href="http://www.ebay.com/itm/310886731873" rel="nofollow">www.ebay.com/itm/310886731873</a></p>
I've taken a look at the circuit diagram and it's basically the same except for some LED's. I don't think it would make a difference to use it instead of the circuit I've made.
<p>Thanks to all of you!</p>
<p>Pretty cool, and very useful. It will work good with an attiny45/85, but I don't really know how useful it will be, considering the attiny limitations... Anyway, good instructable.</p>

About This Instructable

50,179views

434favorites

License:

Bio: I'm an electronic engineering student. I don't usually have much spare time but I like to work on random projects to keep myself ... More »
More by Victor805:The Joule Blinker Cheap caliper battery life hack Wood LED Desk Lamp 
Add instructable to: