hi welcome to an Arduino step-by-step course by Robojax.
we're going to see how we can turn on four channel relay either on ac or dc bulb
by pressing push button turn on and push it again turn off or any other channels on this
many application you need to control four switches or for load let's say ac or dc with a push button
this is an example and the code that accomplishes that uh you can press each switch separately and
independently turn on each channel and the channel will stay on without an interruption
when you press the other button so this will be accomplished and the module that I’m using
is a low trigger module with four relay it has driver with a transistor on everything so the
circuitry will be very easy I’m just using four channel for the in
control of this and two more wires four wires for this and two wires for the power ground and
vcc and then we need just four wires for the push buttons and here I’m using
four push buttons I’m going to explain the wiring after that I’m going to explain the
code and at the end I’m going to demonstrate it in all my videos I’ll have a quick jump link so
you can go to different section of the video by clicking at the link which is in the description
let me explain the wiring first for the switches I will remove these four wires for the switches
I have put these four push buttons these push buttons have uh two pin you can get the four pin
two on one side will be on two on the other side so they're the same
I’m just pushing it one in between so between one and the third
so my push button in here
now this push button is connected in this line and the other pin is connected in this line
same way for this one this line
and this line and for the third one same way this line and this line the middle one is no
pin and also the last one I’ve connected the left wire the left pin of this to the left pin of this
switch and the left pin of this is connected via this wire to the left of this switch and from
here it's connected to the left of this and from here I’ve connected it to the ground of Arduino
now these four wires on the right side I’ve connected them here
like that and these are from this side they have been connected to pin two three four five relay
as you can see here I put here eight nine ten and eleven and I’ve connected it from
this side this orange is connected as you can see to n1 and then in two in three and in four
another this side this is vcc this is ground I try to put the red for vcc usually that's
the convention and the darkest color black for the ground so vcc five volts is connected here
to five volts and ground is connected to the ground because this really is five volts and
the Arduino is able to handle uh the current for this through five volts that's fine
if your relay is 12 volts you can still you use the 12 volts ready but make sure instead of vcc
connect it to your 12 volt source this is an ac desktop bulb and this is a power plug that
I connected to the wall 120 volts which will turn on and off now at this point I’ve cut one
of these two wires at this point and twisted it and soldered it can be connected to relay job of
relay is to connect this and the ac bulb is on or ac load whatever fan you have and disconnect it to
turn it off so I will be connecting these two to the relay from here I’ve connected my two wires
these three pins are for this relay one two three and then these three are for this relay
so each really has three pins and I’m using the two left side of this pin these two pins
are used for this really if you want to connect the next load connected between these two pins
these are called normally open when the relay is energized the two wires will be connected and the
light will be on if you connected in these two wires the middle and on this side initially the
light will be on and when the release energized the light will turn off the same way for all of
these and this also has four light I have separate video explaining this so I will post the length
for you can view it I will not I’m not going to go through the other explanation and components
and here is the wiring diagram I will provide you this one with the code in the same page
if you found this tutorial helpful please thumb up and also click on the
subscribe on the right side in the lower corner I really appreciate it
let me explain now the code because we are using four push buttons we need to have four inputs so I
have defined it from uh Arduino from two to five two three four five for the push button you can
define different pins if you wish and this array is a constant of type integer that's holding these
values so during the execution of the code it will not be changed because it's constant
after that we are defining four pin digital pin as an output so the relays
will be connected in here you can also change it as long as there is no conflict in between them
this is that just for cosmetic purpose like this channel 4 or something so these are just the text
is used to display the text here you can change it as long as you keep the quotation
whatever you want put inside and do not use double quotation in your text it will work and then this
do not change it this is the status of each button initially we set them as zero not pushed and then
and then the status of each relay because I’m using
low trigger relay initially they should be high which means they will be half off so this is
the initial status and that I’m setting inside the setup we have to define an input and output
as pins so I’m using the for loop because I have to copy this as you can see here three
and times four I have to copy this four times instead of that I’m using this loop
that goes from I equals zero to I smaller than four so it goes zero one two three so it goes four
times I changes from zero one two three and every time comes here we pull up from the push button we
when it's zero we pull two when it is one we pull three so the value as if I type here two or three
and then we use input pull up I do not use just input I use pull up
to avoid um using of extra resistor I have another video explaining why we need to use the
input pull up with a push button and then also we use for the relay as output so the
push button was as an input and this is now as an output the same way we picked those values
with this loop and then also we set all the relay output as high here the reason for that one is if
your release or high trigger put here low so they are turned off initially I set them as high so the
relays off are off initially when you turn it on after that we go inside the loop in the loop
now we have to check all the push button status and again I use another loop here which starts
from here and ends here so this goes up to four from zero to three less than four which is four
uh times and then this is digital read push button I when I is zero it goes and pulls
the this value and with one or two it just changes so we get the pin number we put it
here as if I type digitalwrite 2. it goes to pin 2 and reads it and the value is stored in Val as an
integer now this value is the status of the push button and we check it if it is high equal equal
high and then release status is equal low then we change the status how we do is we say that get
the status subtract from one if the status is zero zero from one is subtract one
the result will be one and one one will be stored here if this is one one from minus one
one will be zero and zero will be stored so this is just arithmetic to change the sets of zero
to one and one to zero every time the button is pressed and we give it a hundred millisecond delay
for this to happen after that we store the value of the Val whatever was pushed we
keep it as a status array it keeps the value for us and then we check here if the push now
we kept the value now we have to turn the relay on and we check if this status if pushed is equal
high then we say print this name this this text is printing this for us the serial monitor
and then digitalwrite really pin this I that sorted that is referring to one of the relay
and it will set it low which means we'll turn on the relay otherwise else we print this
the word off instead of on and then instead of low we just put high and the relay will be off but
remember if you set this to a higher value every time you press the push button it will not react
so put a small button value like 100 millisecond and this way it will react very quickly
but this will print very fast
now as you can see
when I press it reacts very quick which is good this is what you want
let me now show you the demonstration as you can see the status of release are shown very quickly
you can slow it down if you wish I set this at 100 milliseconds so as you can see all of them are off
and the layers are off and if I press this this channel 3 is now on
as you can see channel channel 2 is on now channel 4 is on and channel 1 is off
as the same time also it shows here if I press 1 channel 1 will be on because the
light is connected here the other channels will not affect or entrap or do anything I can press
on or off any channel or this one on or off and the others will keep their status
if I restart
I’ve said this that no load will be connected initially so they will be just waiting
as you can see all of them are on
thanks for watching this was how to use this four channel relay with four push buttons turn on
and turn off please make sure to subscribe by clicking at the subscribe button in here thank
you thank you for watching tutorial from Robojax. if you learned something and found this useful
please thumb up if you have comment or question please post it at the comment section below
I try to answer and reply and don't forget subscribe so you get updates on my upcoming video