My name is Ahmad Shamshiri and I am presenting this tutorial from Canada.
Hi, welcome to Robojax in this video I'm going to show you how you can make
an AC light or LED light on and off using a push-button for example when you
push this button the LED is on and stays on even if you remove your hand and push
it again LED is off even if you remove your hand or using a relay
turning on and off an AC bulb. stays on and I press it again stays off so let's
get started I'll explain you how this how you connect this and also explain
you the code the code also will be provided the link at the description
below or you can go directly to www.robojax.com/learn/arduino
So now let me explain the relay. this is a relay module with all the
terminals and everything the circuitry this is a five volt
relay it needs a transistor diode another circuitry to drive it. they put them all
as one module. we have here three terminals that you connected to your
load AC or DC in this case I'm connecting an AC load. you will have
two wires one wire will be connected to common COM this is called normally closed NC
and this is called normally open NO. when you correct your two wires, you connect
one of them always here at the middle and the other wire will be connected
here or here so if you got an issue or problem not understanding it never
change the middle one always change the switch the wire either here or here.
normally closed NC means if there is no power no energy nothing if you connect
the wire here the wire comes it's always connected without anything it
will be on. But normally open NO means this is normally open if you connect two wire
one here and one here so it will turn ON or connected when there is power or
energy to the relay or the signal that comes so this is normally open and with
the signal comes it will turn the light or another device that you have will ON
turn them ON . now on this side we have three pins here
and also we have red LED and green LED so the red LED is the power LED when
this module needs separate power this will always be on if the power is
connected so the green LED will turn on when there is a signal or command to
command and to control the relay. we will have VCC this is connected to 5
volts this is connected to ground or negative (GND) wire as some people call it or
zero volts so these two must be connected to the power or to the Arduino
and this is connected to the signal this is IN or input so when the signal comes
this light goes ON and the relay goes ON and normally open NO will be connected to
common so the power comes and goes it allows it to pass through. let me
explain the AC bulb here. this is a table light that is connected to the
wall outlet and then the wire goes and will be turned ON but I have
cut one of these two wires here so let me show you. I've just cut this
wire from here now this can be connected to that relay. Job of relay is to just
connect it so it will be ON and disconnected to it'll be OFF I've just
twisted them and then put some solder so this can be inserted into a connector
with a screw. This LED is light emitting diode. it has two pins the
long one is anode which is a positive and the short one is cathode so which is
negative so the positive voltage must come here to turn it ON on and off
I'm connecting it now here. we have two types of push-button here the tiny
one so one is with a two pins that you can insert it that you do to breadboard
and have longer pin (I have a video on how to use a breadboard. the link is below this video) this is shorter pin you might not be
able to easily insert it in breadboard. but both of them can be used so if
you want to use this one you can solder a wire and then insert it into that
breadboard. top of both of these are exactly the same except the pins
different and this one has four pins so this and this have two pins. this can
connect two wires with one push which can connect the two switch but this can
do with one switch so both of these can be used to turn the light on and off. now
I have connected the AC wires the two wires here between the normally open NO and
common COM. and on this side I've connected these three wires so this is VCC this is
ground and the green on the right side is a signal and here is the breadboard
so the green from the relay from the signal (the IN pin)
this comes connected to this pin on the breadboard which is connected through
this yellow wire to pin 10 so that's connected to pin 10. then pin two of the
Arduino is connected to the this switch so one side is connected here and then
we have this switch push-button switch and then the other side of the
push-button so there are two wires for the push-button one is this one the
other is this one this is connected to the ground which is this white line this
line is all connected and then from here also the positive of this is connected
to positive of relay and the yellow is the negative to the relay this black
goes to the Arduino negative or ground and this goes to five volts which is
here so now let me show you here the and the Fritzing (free software) I've also created this
this will be also provided so you can download it
so this is not a fully compatible relay module but I just made it like that so
you can see it so these two wires are connected to the AC vault the two that
you have will be connecting here and here and then we have three pins here. VCC
goes to 5 volts ground goes to ground and then IN goes to the pin 10 this way
it goes to pin 10 we have also an LED on the on the breadboard so you can see it
also here. this it was just for the test you can remove it if
you just use a relay
so this is for learning purpose and this is a switch which is connected from 2 to
the switch and then switch goes to the ground this is the code now on the
Arduino. pButtonPin or push-button pin is pin 2 which is connected to the
push button and then relayPin which is connected to pin 10 to the relay or LED
so you can change these if you don't have this pin 2 or 10 available if they
are occupied or used for something else you can change it accordingly it doesn't
matter as long as you change one of those digital pins on the right side of
the Arduino one of these turns except pin 1 and 2 anything between 2
to 13 so you can put those and then this one you do not touch it so this is a
value which is a push-button value from pin 2. this is a light ON and then this
is the push status so this is for the purpose of keeping it ON and OFFso
please do not touch it other way the sketch will not work. so now we have this
I setup we are just putting this for printing on the serial monitor which I
will show you next and then we are defining push-button pin
with input pull-up it's very important that you should have
pull up not just INPUT okay so this could work also with pin input without
the pull up but we would need extra resistor. here okay to eliminate that
resistor use, I have use INPUT_PULLUP so this eliminates
using of extra resistor and then relayPin which is defined here as pin 10 is the
output so you don't need to touch this one because if you make changes the
change will reflect here so it doesn't matter now
inside the loop the loop is a feature that everything here will be repeated so
this is a push-button pin the value is stored here when I pushed the value is
stored here we say if the value is equal HIGH and the light is equal ON AC light
the variable at the top which is LOW we say push button is equal 1 minus
pushed and we wait hundred so this is memorizing or keeping
the status of the push-button it just detected and here it updates it when is
pushed when it's pushed first time it gets one value when it's pushed for the
second time it gets different value so light on will change depending on this
value of this when it's pushed so anyway so we say if pushed is equal HIGHthen
we say light ON prints that text here for us let me bring that click on tools and
then serial monitor oh it's not connected so let me connect the boot
and then now serial monitor it should have light ON and then when I push it
you see light OFF so it shows relay to the low the output so this will keep the
light ON and then it says light OFF it turns light OFF otherwise and keeps
their pin 10 HIGH and now we will get that status here so saves light on so
when you connect it initially you can keep it off if you wish otherwise the
light is on so now I am pushing it the light off and also this value also is
reflecting it when I push it again the light on or so you see it show LIGHT ON
on so this way you can control the light this was an example of a push-button
keeping the light ON and keeping the light OFF with a push button. thank you
for watching please thumb up 👍 the video, please share it and also if you have not
subscribed please do so now I will have a lot of cool other projects coming so
you get updates
Typed and corrected by Ahmad Shamshiri on Mar 13, 2020.
I did my part to help you learn and use this code. The least you can do for me is to 👍 the video.
Subscribe to my channel. Just say thank you in the comment. Simply do not skip the ads. Keep watching it.
I appreciate it.
You may send me money via PayPal. The link is below this video. so I can create more contents like this. thank you.