![]() |
A relay is an electromagnetic switch, opening and closing at the behest of a 5V signal from the Arduino, but electrically isolated so that it can be hooked to high-voltage without frying your Arduino. Long story short, used an Arduino to turn on/off something you plug into your wall. In Canada and the US we use 120V AC power in our homes, so whether you want to turn on a light switch, a blender, a coffee pot at a set time, whatever, these are the right tool for the job.
When you buy a relay, you're looking for one that states the voltages it can handle. If you want to control home electronics, you are looking for ones that say something like 125VAC and/or 250VAC. Don't worry if they say 120 VAC or 130VAC, just buy the cheapest ones. Here is a little tip... Electronic components are very sensitive to economies of scale, which means the more of something they make, the cheaper it is. Common sense sure, but what has served me well (mostly) is that if I'm buying a component for a use that others buy them for too (i.e. a relay to turn on/off house power), then the cheaper item is usually just fine. If you're looking at two relays and one is 125VAC for $3 and another is 126.34VAC for $12... get the $3 one!
There are a couple things to note about using relays, and I'll give you the easy to use option, and the harder but still easy way to use them! ;) First the harder way:
Harder (but still easy) method
![]() |
a diode |
CAUTION: All relays have a 5V pin, a ground, a higher voltage pin, a normally open (NO) pin, and a normally closed (NC) pin. There are many models out there, and the organization of these pins can be completely different depending on the model. Google the model and check the pin diagram to know which is which!
There are many examples out there that use transistors (switches) to control the relay, and while this doesn't hurt, I think it is a hold-over from non-Arduino use, where you would use a button to control the relay. For our purposes the Arduino acts as the switch, but feel free to use the Arduino to control the transistor if you like.
Easiest method
![]() |
4 relay module |
Wiring
The example below is of a water level sensor, but you could use just about anything (temperature sensor, relative humidity sensor, photoresistor, photodiode, etc.).
As you can see, the water level sensor is connected to ground, 5V, and analog pin 0 (A0). The relay is connected to ground, 5V, and digital pin 4. The relay has two power cables attached to it, one with two prong power, and one with three, so that I can plug it into whichever I choose. Currently I want to use the second relay from the left, so I plugged Arduino pin 4 into the IN2 pin on the relay module. The electrical cable I'm connecting the relay to was cut like this:
Open the cord, cut the positive wire, and stick one end in the high voltage (middle) screw clamp, and the other end in the NC or NO screw clamp. If you want the Arduino to shut something off that is normally on, use NC, but to turn something on that is normally off, use the normally open, NO.
![]() |
Sorry, the yellow pin 4 wire connects to the white IN2 relay wire. Sorry to be confusing, my bad! |
Code
I wrote this little program to keep a heavy outflow tank full. Basically, I have a water level sensor, and when the water reaches it, the pump shuts off for 10 seconds, then turns back on.
That's it, hook it up and you're laughing! You can easily change this to a photosensor controlled light, light controlled blinds, dark controlled lights, temperature controlled heaters, etc. Have fun, and let me know if you have any questions.
No comments:
Post a Comment