Hi, welcome to an Arduino tutorial from Robojax. in this video I'm going to
show you how to drive 5 volts relay or 12 volt relay using Arduino and sample
1 transistor , 1 sisters and one diode to turn on and off or control
and AC or DC load like this this is now a DC load you will get the code for this
by clicking at the link which is below the description which will take you to
www.robojax.com/learn/arduino let's get started with this
in my application you need to turn on and off an AC or DC DC load and you need
to be able to handle higher current for that reason you will use even need to
use relay these relays are very cheap to be available you just need to have a
transistor resistor and a diode for protection of transistor so you can you
will be able to send signal and turn this on and off and using this one you
can turn on and off your load which could be AC or DC
Hardware explained
so the components that you need this is 2N3904 NPN transistor this is 1kΩ
kilo ohm resistor this is diode on the top of the relay and
between the VCC and this side that is connected to the collector of the
transistor you put a diode the job of this diode is when the relay turns off
the energy that is released or kickback voltage will be absorbed by this relay
and it protects this transistor and this transistor is acting as a switch when
the signal comes it connects the collector to emitter it become a short
circuit approximately that's ideally a short
circuit and VCC is on the other side ground is on this side the relay will
energize and this contact will go down meaning this line this line and these
two wires the connection will be established and your load will be
connected the transistor has three pins this is called collector C with a no sign
or nothing in this side we have arrow this is emitter E the arrow is outside
this is NPN or negative type transistor and this is base which is shown as B and
here are the when you hold the transistor these are the emitter base
collected from left side and the resistor is 1 kilo ohm the diode has a
line here this side is called anode and this side is called cathode and the
cathode has a line and if you are using 104001 1N4148
this diode also has a black line so that is cathode and this is anode
Wiring explained
of the time you need the load to be turned on when it's energized so you
will be connecting it on this side so normally open NO these two panels are
called normally open NO this is normally closed NC. now I have here five volts and
this power supply the relay doesn't have a polarity so if I connected here
corrected you can hear that click doesn't matter if I change the polarity
because it's just winding and these are the two pins of the multimeter when you
connected continuity tester with buzz just connect hundred buzzes so to to
show you these connections these two are normally closed NC now they are
connected if I power this up
the relay energized the load is disconnected connected disconnected now
let's go to the other one do not change that one if you connected here it it the
load is not connected the switch is off and now when I energize it
so we will be connecting it there now the diode which is this diode anode
and cathode we will be connecting it between these two pins to make it simple
I'm gonna put it here so we will reduce the usage of extra wires anode
Wiring explained
and I have prepared the relay I put a diode here
the anode is on this side the cathode is on this side which is the positive I put
the red wire and this is the black wire these two or I put a header for
connection and the middle wire also I put herer from
positive this goes to anode and cathode goes to the negative so that LED will
turn on and these two wires will be connected to the load whatever load what
I want to turn on AC or DC. I have here five volts now when I correct the relay
to five volts
it goes on and off and here is how you connect your load this is the load and
it needs two wires to be connected to power this doesn't matter if it's easy
or DC through the two wires are coming to the load and the circuit completes
like that now to control it with the relay we just cut here or do something
like this the power goes to relay, relay needs to wire when
switched it it connects this wire back here and the
load will turn on so these two extra well one we created if you just cut here
it will be two wires this is the 2N3904
transistor as you can read it and on this side this is emitter E base B collector C
according to this so emitter base collector emitter E goes to the ground I'm
gonna put one wire here for emitter this is emitter and this side will go to the
ground of this Arduino I'm gonna control the base I put 1 kilo ohm resistor and
then this one will be connected to the pin 2, I'm gonna use pin 2 so here this
is a base they said the metal pan is base B we have
1 kilo ohm resistor here and from the resistor I'm gonna put one wire to pin 2
this will be connected to pin 2 these are my two wires a black and red for the
relay to the positive I put the red wire this side goes to the positive I'm gonna
connect it to 5 volts of Arduino and the negative on this side I labeled it as
negative this will go to the collector C of transistor I'm gonna go here and the
collector of transistor is the last pin here so this is the last pin and that is
a collector C now this is done the circuit is ready and now these 2 wires or the
load wire so we are connected
Code explained
now let me explain the code we're defining a pin for relay so this in this
case it's pin 2 so we call it a relayPin now from now on I'm gonna use this
word instead of number 2 so it will not be confusing in case if you're using
different pins for different purpose and then this is a setup() which will run only
once inside this pin setup we say pinMode and relayPin which has been - as
an output also we define pin 2 to be able to send signal out and then we
initialize the serial monitor with 9600 baud this is the monitor if I click here
it shows this serial monitor and at the bottom on the right side you see here
this number 9600 must match this number in order to print the information for
you and then I printed this Robojax.com relay module. inside the loop
we say digitalWrite pin 2 (relayPin) to be HIGH this will turn on the relay for us and
then we print this one on the screen so let me upload it so this is when it
program runs it says relay ON and then wait for 5 seconds and then send a LOW
signal to pin 2 with the digitalWrite so this will turn OFF the relay from
this text really off and wait for 3 second and it continues like that really
on off on off on it starts from on at the beginning if
your application needs the relay to be off or on at the beginning before doing
anything just one of this line should be here and then you can set the relay to
be either HIGH or LOW and also if your application needs so the program or
relators and sort of the few seconds let's say a machine or something speeds
up so you can add here a delay let's say 2 seconds
until this is not finished the loop will not start so this will give your other
devices to boot up if you need
Demonstration
now let me demonstrate it. now I'm connecting this base to pin two and let
me turn this ON, when I run this is it it shows ON and the light is ON and it's
OFF the text and also we have a status LED here what tells us
that the relay is ON and the load(bulb) is also ON you can change the wait time here for
example you can make it 300 milliseconds and then one second
this will cause the relay to turn ON for very small moment and then off now this
is a new signal
That's it. thanks for watching this was how to drive a relay using Arduino with
sample transistor if you learn and like this video please thumb up 👍 and also
subscribe so you can get updates of my upcoming videos
Typed and corrected by Ahmad Shamshiri on Mar 09, 2020. Please subscribe .