Wednesday, January 28, 2015

Sensor-based Relay Control (Lights, Heaters, Pumps, etc., etc., etc.)


A lot of automation centres around some incredibly simple control - using a sensor to turn on/off a relay. For those who don't know, a relay is one of the most useful electronic components, and can be as simple and satisfying to Arduino-enthusiasts as the first time you get that led to turn on and off (ye old blink sketch)!

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
Relays operate with a electromagnetic field, so when you send current through it the magnetic switch closes. Alternately, when the magnetic switch opens, it sends current through your device, which can be potentially damaging to the Arduino. A rectifying diode should be used to protect the Arduino (see sketch below). This diode allows current flow in one direction only, so make sure that the stripe is facing the positive (5V) lead.

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
As I stated earlier, economies of scale can work in our favour. Available very cheaply online (eBay), you can get modules with one, two, four, six, eight, or more relays on them, for less than a single relay at your neighbourhood store! Honestly, these are the way to go, and you can reuse them over and over. These modules have the transistor, the diode, and an LED to tell you when they're on or off. They have pins that fit female jumper leads for the GND, 5V, and signal wires, and screw terminals for the high voltage pins. Get these!

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