Thursday 1 May 2014

I just made a discovery about the available relay shields. There are plenty of good optoisolated relay shields on the market and to prizes making it questionable building your own. But be aware, that most common relay shield are switching on input LOW. What does it mean?
Let's make a quick trip to the good old "Hello World" sketch I guess everybody knows by heart.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
ledPin = 13;                          //defining the led pin

void setup(){

  pinMode(ledPin, Output);     //setting the pin to output

  digitalWrite(ledPin, LOW);    //setting it to low (off)

}

void loop(){

  digitalWrite(ledPin, HIGH);  //setting it to on the internal led lights up

}


That's I guess is the normal way of switching something on or off, setting it to LOW, no current flow, everything is off, - setting it to HIGH current flows everything is on. Normal thinking now would be if I hook up a relay to switch a load which I can't or don't want to run direct though the Arduino, it switches when I set the output pin to HIGH and is in neutral when the output pin is LOW. Personally no current flow would be my idea of a circuit when it is in neutral or on standby.
Back to the therm switching on LOW means that our relay shield would switch when the output pin is set to LOW, which means I would need to set the output pin on HIGH to have the relay in neutral. Switching on HIGH means the relays are switching on output pin HIGH and there is no current flow on neutral. Buying one of the ready made relay shields do need some planing in electronic design and in your sketch. If you are in need of a relay shield switching on HIGH and you are having a problem finding one ready made, there is an example of putting one together for your self.
What we need per channel:
1 relay with a 5 Volt coil
1 diode N4148
1 NPN transistor BC337
1 resistor 1K

for testing:
some jump wires and a half size bread board

The above example is showing a i channel relay circuit on a bread board. Please note that the relay is getting it's supply from the Arduino board. The 9 Volt battery and the DC motor are only examples for the secondary circuit. There we can switch what ever we want to within the specifications of the used relay.  For testing we can use the "Hello World" sketch. And if you add in the main loop where we set the ledPin to HIGH the following:

delay(5000);
digitalWrite(ledPin, LOW);
delay(5000);

That will switch the relay every 5 seconds on and every 5 seconds off. If you didn't connect anything to the relay you will at least hear a click every 5 seconds. Please don't do this before you go on a 2 weeks vacation and forget to switch it off. If you build a multichannel relay shield you need 1 Arduino pin for each channel except if you want to go into using shift registers which I will cover in a seperate post. If you are in desperate need have a look at http://arduinopraxis.blogspot.com/2014/03/room-management-system-coding-output.html



Above an example of a 4 channel relay shield. For 8 channels just keep on adding relays with there respective components (diode, transistor and resistor). The LED's are optional and only indicating which channel is activated or indicating the presence of the 5V power supply.


Above is the PCB layout I am using for my projects. If you like to copy it, please check it careful cause it's a double sided circuit board and be aware of the used relays because pin positions may vary.