Introduction: Controlling 120-240 VAC With a Relay Using Arduino
I have come across a lot of instructables, tutorials, videos, showing how to control these relays and that's great that people create these. But every single of them tells me that if I don't know how to deal with high voltages, you should stay away. And yes, that is true for the most part, they can kill you. But if you are reading up on how to control these relays and have no experience, should you just stay away? No, of course not, you want to learn. In this tutorial I will hopefully show you how to control these high voltages, messing with them, controlling them and much more.
Now, before I start, I do not claim to be better than everyone else creating these kind of tutorials, I hope to make it a little bit clearer on how to deal with these voltages and how to work with the relays.
Enough of me rambling, let's begin.
Step 1: Some Tips
So there are some great tips out there on how to work with high voltages when your nervous about being killed, when you just don't want to be messing with them, sure you could just go and pick up this product from adafruit but that is no fun, is it?
One great technique is the one hand one, simply keep one hand in your pocket or make sure not to use it, this will prevent any potential current from flowing through your body.
Another one is to always have another person close to you. This might sound silly, but even if you are older the presence of another person is almost crucial, as that person can help you get away from high voltages. And if you do get electrocuted, the other person cannot just walk to you and grab you, but that person has to push you away from the electricity using some force, as the current will flow through your helper immediately once he touches you with both hands.
Finally, never do something you are not 100% sure you know it will work, do not take ANY shortcuts, were talking about a you, a life.
Step 2: The Relay Itself
The relay looks pretty complicated at first, and that is what I thought, but after some look arounds I found out what does what.
There are three pins on the relay that you connect to the arduino. Of course, there can be up to 10 pins if you have a relay module that features 8 relays on that PCB. These pins are usually labelled something like VCC, IN, GND. Where VCC is the arduino's 5 volts, the GND is the arduino ground. The in pin is where you will be controlling your relay from. You can connect this to any pin of the arduino, as long as it's an output pin.
On the other side of the relay, there are usually three screw terminals. One called NC (normally closed), one COM (common connection) and NO (normally open).
NC is pretty much nothing we are going to use, it stands for normally closed and there is always a connection between COM and NC until the relay is triggered HIGH.
The NO on the other side is one of the terminals we will use. IS stands for normally open and is only closed when we trigger the relay LOW. When we trigger the relay HIGH, it is open and no current flows through.
COM is the common connection, we will be using this along with the NO terminal. We connect the hot wire to this.
Read more about hot wire in the next step.
Step 3: Hot Wire and Connections
I will be controlling AC light from the relay. It has been standing and doing nothing for some time so I though I would use it for this tutorial. First we will have to remove the 1st layer insulation, it is usually colored black or white. On my light it was white but on yours it could be black, who knows.
When you have cut off some insulation you will immediately see two wires, these refer to neutral or ground and positive. We will be connecting the hot wire to the COM port, go ahead and do that. No just kidding, the hot wire basically means the positive wire coming from the wall outlet. In my light there were to cables, one blue and one brown. This kind of confused me, because which is positive and which is neutral?
With a quick google search I found this page. It tells me that in Europe and according to IEC brown is positive (single phase) and blue is ground, or neutral if that matters to you. Of course if you don't live in Europe or is in Europe and doing this, there are several charts where you can see which color is which.
So I stripped the insulation of the brown cable, do this according to your regions color code, if you live in Europe, it is most likely going to be brown.
Next use your pliers to cut the copper wire and connect the hot wire to COM and whether wire through NO. That's it, now just connect the pins on the relay to the arduino, it is fairly simple:
GND -> GND
VCC -> 5V
IN -> D8 (digital pin 8), you can use any you want but you will have tochange it accordingly in the code.
Step 4: Code
Now just upload this code to your arduino, plug your lamp into the wall outlet and walls, you got yourself a lamp that is on for 10 seconds and off for 10 seconds.
The code is fairly simple, mainly initiating pin 8 as an output, turning the output LOW to turn the lamp on, waiting a second then turning the lamp HIGH to turn the lamp off and finally waiting for a second, here is the code:
//Control relays with arduino
int relay = 8; //Relay pin = 8
void setup() { pinMode(relay, OUTPUT); //Set relay as output
}
void loop() { digitalWrite(relay, LOW); //Turn lamp on delay(10000); //Wait for 10 sec digitalWrite(relay, HIGH); //Turn lamp off delay(10000); //waiting for 10 sec
}
Step 5: Finished
Now thanks for reading and I really hope you enjoyed and learned something. That's it, now go and have fun, but be careful with the high voltage, as said, it can kill you pretty easily! Have fun!
Samuel