 In this video I'll be showing you how a relay works and how to use one yourself. Just do me a favor, hit the like and subscribe button for the algorithm. Let's get started. First off before we use a relay we have to understand how it works. Inside this relay there's a switch and a magnet and the magnet can be activated with electricity which then moves the switch and allows us to control power without actually having to use our hands. Now just so you understand I have a demo here. This is called a linear actuator and when I give it power it activates an electromagnet in here and then moves this rod back and forth. Now I imagine if this was here and then we had an actual switch and that went and flicked on and off. It works similar to that. Obviously it has to be compacted and a little bit more efficient so that it can fit in such a small unit. So it allows us to use low power to control high power things because otherwise if you can control your lamp with an Arduino you would just plug your lamp into here turn your lamp on and off with your Arduino you wouldn't have to use any of this stuff but the lamp needs so much power that this Arduino will never be able to provide without something like this. So we get our power from somewhere else but we still use a relay to switch it on and off. It lets us delegate our power source but it doesn't make us lose control of if the power is being activated or not. On the left we have our power side and on the right we have our control side. Our control side uses low power like 5 volts or 3.3 to control the relay whereas the power side as you'll see on top here you'll see numbers and this one can take 10 amps and 30 volts DC or 250 volts AC which is a lot for something small. So just off these numbers I could actually control my laptop charger a lamp the fan my room all these things that take a decent amount of power can be controlled just by one of these and this was like less than a dollar off of some cheap website. So for the power side we have NO, COM and NC mine are kind of cut off but NO stands for normally open NC stands for normally closed and the middle COM is for the common ground. So what happens is you put your power source in the middle and then you can choose normally open or normally closed normally open would be if the relay loses power you'd want it to be always on. So if you were to lose control you could either have it on or have it off. Personally my rule of thumb unless I really have an exception is always to have it on normally closed so if my relay dies something burns not sure that Arduino turns off I lose power whatever it is it always goes to normally closed and closes the connection to whatever it's activating. So instead of having a lamp go on permanently because the relay or something there broke it would just shut off the lamp and it would stay shut off and then you would know there's a problem personally that's what I'd rather do. It's pretty much the default state of the magnet so that's pretty much it for how it works now let's go and actually connect it up to a little circuit and make it happen. I'm just going to use a light here and a little resistor so that the light doesn't get burned out and we're going to plug in everything here so that we can use our relay to switch this light on and off. You're going to need a couple cables I just have different colors so we'll use red black and purple for our power side now since we're using low power we can use these smaller cables but if you were to activate something like a lamp a fan whatever a microwave whatever it is you want to use some some big chubby cables that won't just get burnt out for any high amperage use okay and then we're going to have these three for connecting our relay so our relay has a signal a positive and a negative signal is to turn it on and off so that's going to be orange then we got red for positive black for negative I'm going to plug in my red into the five volt right here I'm going to plug in the black into the ground right next to it so now the relay has power but no signal so it's just going to stay close then we're going to plug in our signal cable to seven and look at that it's freaking out I think it's because I have some code on it that might activate a relay so let's just go quickly on the computer okay now that's been fixed so we plug in our signal cable we're going to plug it into seven on the digital side so I might take a screwdriver you might need one of these I'm just going to open up all the terminals black is going to be our ground to the board and that's actually going to be grounded through our led because remember we're we're diverting the power into the relay blocking it until we want to open it but then the power has to continue and go somewhere else we're going to be grounding it to the board I would not do this if you were powering something high powered but because it's an led it's fine typically this ground would be going to a legit ground from a serious power source if you have to power something like a fan all right so now we're going to take our red and we're going to put it in the common now there's be a good way to know that's where the power is going I should be using two red cables but I don't actually have another one then purple is going to be going into normally closed just this one we can try after doing the opposite so you guys can see what what happens now our red is from our power source so again we're going to pretend like we're powering from something serious in my case I don't have another power source I'm just looking and there's a five over here here we go and then our purple which is normally closed is going to go to our led so this should not turn on the light oh so we have something wrong something is not right so I'm going to take out power in the relay the light staying on which tells me that I plugged into the wrong side because that's got to be normally opened now if I just touch it to the side it should not turn on there we go okay so I just plugged into the wrong side so that's what happens when you use the normally opened instead of the normally closed now we're normally closed and even though power is plugged into the relay if I unplug the power the magnet still stays in its default position all right here we go plug that back in all right so just to go over everything we have our signal power and ground for the control side we have our input power and then it goes into the relay and it goes to this resistor led setup and then it grounds into the board all right so let's open up our obs and then we can write some code to make this work all right so like always with the first thing we have to do is declare our pins we only have one that we really care about it's going to be our input pin so I'm just going to put that as seven and in our setup we have to say what that pin is so we got to do this pin mode and inside pin mode we got to call pin and we're going to say it's an output pin then in our loop we have to do something with the pin we just declared and now since it's an output pin we're going to write to it instead of digital read we're going to digital write and then in there we can say what pin we want to write to and then we want to tell it to go into low mode low just means off high means on so if the relay is in low the relay should not work if the relay is high it should work we're saying low so that if it is already high it's going to be turned off it's just a good practice then we'll do a little delay 300 milliseconds just enough to flick the light on and now we're going to tell it the total opposite which is going to be which is going to be high so it's going to make sure it's off it's going to wait 0.3 of a second and it's going to turn it on and we can leave it on for I don't know maybe a whole second so on for a second off for 0.3 of a second and then repeat over and over and over I'm going to save that I'm going to make sure that you're in tools here you select the board you have winds up Arduino Uno and then the port which is com3 usually just play with those and it'll work so you click upload I do not want to do that something happened here I used the wrong now we just do that I do not want to save it so it's going to say compiling sketch uploading give it a second here and there we go make sure that the camera can see this and you can feel it like I'm holding it you can feel the tap it would be enough to I mean I could feel it all the way through the screwdriver I could feel it through the table it's it's pretty strong you can feel that magnet going back and forth back and forth and then because of that our light turns on now let's go in the code quickly and let's comment out everything except for the digital right high so when I refresh this and it uploads to my board it's just going to leave the relay okay so now our light stays on and our relay stays permanently open we're going to go in here quickly we're going to unplug our normally closed and we're going to put it into normally open which is now going to reverse everything so if you wrote any code you're going to have to turn it around because watch what happens you can see the lights on the relay is open right now but the light is off now I'm going to unplug the relay it's not going to have any power look at that the light turns on because this is the default state of the relay we're inside normally open right now so that it's normally open so hopefully that makes sense if it does please do me a favor give the video a like and subscribe to the channel so you can see more of these reviews builds explanations whatever you want to call it I will see you in the next one