Hi, welcome to an Arduino tutorial from Robojax. in this video we are going
to see how we can use Arduino as a timer to control a relay with
potentiometer to turn on and off an AC bulb or AC load load or DC load. when I
rotate this at the maximum, it shows maximum 30 seconds you're setting it for
20 seconds here it shows and let's start the load is running at any moment you
can cancel let's set it for three seconds on and it will turn off after
three seconds. you can get the code for this project by clicking on the link
which is below the description which will take you to
www.robojax.com/learn/arduino let's get started with this
My name is Ahmad Shamshiri and I am presenting this tutorial from Canada.
in this video we are going to use Arduino as a timer to control a relay
and consequently to control a DC or AC load. and in order to set that time you
need either a computer or you need a screen to set that time but in order to
make it simple I'm just using this potentiometer and you can use any
potentiometer 1 kilo ohm 1 kilo ohm and up to 100k ohm
would be fine. and also if you don't want to use this big potentiometer you can
use this tiny 3 pins potentiometer. that is also fine. I've put a knob here
and then the minimum and maximum is known for me it's very clear. what I have
written is in the code that you set your minimum and maximum and this minimum
will be represented the minimum value of the voltage that is read on analog zero
of this Arduino will set you at minimum time and when you set this to the
maximum that goes to the maximum of the value that is read here. so if you set
that at 10 second this maximum will be 10 second and if your minimum is 5
seconds the minimum of this will be 5 seconds. so you can set it virtually any
value lets you have 200 hours or more or less if you want. in Arduino when
once you upload the code just connected from computer and then supply your
powers and this will independently work with this potentiometer and you can set
the value that you set through your computer in code. and then we have these two
buttons I will be using one for set so when you start this device the relay
will be by it by default OFF. it will not engage (or turn ON) it will wait. so you press this
and relay will start working. and there is a cancel button or reset button
anytime you just press it and that's all just reset the time and relay well
disengaged (disconnect). you can use available relay modules with simple 3 pin and all
other circuits or you can have your own relay and just wire it and connect it it
works the same except you effects some for a while here or maybe if you're
using OMRON or other type of relay. for this one I have a separate video
explaining how to drive this relay the link will be provided below the
description. this is the relay that you might use or any other relay you have.
but if you're using the relay module really have these three pairs for your
load you can connect your one wire here and the other one wire here and the
relay engages it will connect it these two pants are normally closed so you
will not use it for this purpose. three wires here on the relay on this side the
first one is VCC will be connected to five volts of Arduino. this is
ground connected to a ground of Arduino. and this is IN this will be
connected to pin 8 of arduino which i'm going to show you next.
this red light the LED is turned on when you connect the power or VCC and ground
and this green will turn ON when the signal comes here. as we've mentioned
that this is LOW level triggered relay when it receives zero volts the relay will turn ON
when it receives 5 volts (anything above 2.5V) that relay will go OFF
Relay Current rating
when you're using a relay pay attention to the current rating of relay for
example this is a very tiny relay and the current rating is 2 ampere for 30
volt 0.5 amperes 100 volts and 0.6 ampere 125 volts. this is the amount of
current that it can handle. when you're using a relay pay attention to the
current rating of relay for example this is a very tiny relay and the current
rating is 2 ampere for 30 volts 0.5 amperes for 100 volts and 0.6 ampere 125
volts this is the amount of current that it can handle. and this can handle 10
ampere at 28 volts and 10A at all voltages 120 and 250 and 30 volts. so to
understand how my turf can handle in North America the AC voltages 120 volts 10
times 10 ampere is 1200 watt or 1.2 kilowatt if you're in Asia and Europe
the voltages let's say 220 or 240 volts for 220 times 10 is 2200 watts or 2.2
kilowatt this relay can handle. and the same way this one you are after also
multiply the voltage times the current to get the power. and the same way this
is a 12-volt relay but the rating is 10 ampere for majority of the voltages. to
avoid fire and overheating do not allow the current above the rated current to
pass through this relay.
Wiring explained
let me explain the wiring. this is an AC desktop bulb and this is a power plug
that I connect it to the wall and 120 volts which will turn it on and off. now at
this point I've cut one of these two wires (the live or Hot wire) at this point and twisted it and
soldered it that 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 disconnected to turn it off.
so I will be connecting these two to that relay. and the two wires that I show
you for the relay they're connected here on this side to change this wire on this
side and every time the relay is ON the load will be off and everything every
time they relay is off the load will be ON. but this is just normal
operation and the relay is on the light is on or the load is on. on the relay we
have three wires VCC connected to 5 volts here the middle one is ground
connected to the ground and the last one is IN, this is connected to pin 8. for
the push-button this is ground, i have ground here which I'm also connected to
this wire this push button is connected this is a 2 pin push button one will be
connected here which goes to the ground and the other one from here goes to pin
4 which is in here. the second push button same way on this side that is
connected to the ground to this blue which is connected to the ground and on
this side this pin is connected to this blue which is connected to pin 2. push
buttons are connected to pin 2 and pin 3. now put in potentiometer, the two side
one wire is connected to 5 volts the other one is connected to the ground. these 2 pin
if you switch them it will change the direction of setting minimum. and maximum
for example maximum is on this side and minimum is on this side. if you switch
these two wires the minimum and maximum will be changed.
the middle pin which is the actual value is connected to analog 0 or A0.
Arduino code explained
let me now explain the code. I've set pin 8 for the relay. I call it relayPin
and potPin or potentiometer pin is A0. and we have a resetPin
pin 3. and startPin, pin 2 these are to pin that is one is used for the start when
you push it the time starts the other one resets and cancels. and then the
relay time because we have two types of relay module HIGH level trigger and LOW
trigger you inside the single quotation you put L or H and this is the maximum
you can set the maximum time for example this is set now for 30 second or thirty
thousand milliseconds put it at millisecond and this is for three
seconds or 3,000 milliseconds. do not change this the duration is a variable
that's holding the duration that you set with potentiometer. potValue is
memorizing the potentiometer between zero and 1024 values rememberTime
is a time that is set at the time that you press the button. realyState is
holding the set of relay if it is on or off. remember that that reset pin must be pin
2 or 3 because I'm interrupted in an Arduino Uno other prints will not
work. for Arduino Wi-Fi all pins are okay for the Arduino mega these are the pin
so I will provide you the link. so these are the interrupt or reset pin that I
used. inside the setup I've set relayPin as an output and I set resetPin as
INPUT_UPLLUP and start an input pull up I use a pull up to eliminate use of
external resistor I have a video explaining that I will also provide the
link for you if you want to watch it I will not spend time on that. and then
this is an important line attach interrupt. and this digital pin to
interrupt reset pin that I mentioned. and this is a function name and
it will check it when the switch is pressed and the low is detected. so in
trouble happened this will interrupt the process of our relay timing. and then we
a initialized a 9600 baud the serial monitor this is a value must
match otherwise you'll not be able to read this text. and then because we are
setting two types of high level trigger and low level trigger I set this here checking it
to turn off the relay at the beginning. so if it's LOW level trigger type, we are setting the
digital Pin HIGH this will turn off the relay and also it prints this text for us.
if relay is not LOW level trigger type and it it is HIGH Level trigger type we just set relay
pin to LOW to turn it OFF. then inside the loop with it using analog read pot
pin potentiometer pin we read the value and we divided by 10, dividing it by 10 well
reduce the sensitivity of potentiometer because constantly the value is changing
a fluctuating so we just disregard the 10 value our first digit on the right
side and the value will be stored. here and then we using the maps we get this
value and we say the value that varies between 0 and 1023 and because we are
divided by 10, I discard the last digit just so I make it 102 that's
pretty close to original value. and then we say map this between minimum and maximum value that
you set in here between minimum and maximum. so we will get the actual
duration in millisecond and then we check here digitalRead startPin if
it's low which means if it is (push button) pressed then we remember the current time using
the millis() we store the value and this variable which is defined above and also
we set the value of really connected or requested so this will be stored above
and then we learned the function which I'm
gonna explain next. it goes and controls the relay. in here this loop
continuously checks if the duration that current time minus the remember time
remember remember time is always smaller because this time is constantly increasing
at the time passes. if it's subtracting this from this and we check it if that
difference between these two is bigger than duration then we set the relate to
zero so this is turning the relay OFF and et the status zero and then running the control
function this is just printing the time duration divided by thousand and
transit as a second for us. and then we wait 200 milliseconds to just go through
the loop. now let me explain the controlRelay. the control is really checks if relay
type is L and status is equal 1 then we set that relay pin to LOW mean
activate. and also we print this text. if the status of relay was ON then we set
it to OFF because that relay triggertype is low. off this means turn off. now if it's not L else
then at 1 we set it to HIGH else we set to LOW. this is this function an important
section of this is the interrupt that I used and this reset means use this
function this is a function name without those opening and closing parenthesis
every time this reset pin is pressed so this is when this runs we set the
duration to 0 that means variable that is defined at the top and then we check
if the relay type is low trigger then we said that alipin high so that high
trigger delay is of F it's not else said that law that really will be often
also we print this text which is when it runs it to will also print relay OFF.
Demonstration
let me now demonstrate. the AC bulb is have connected this is my potentiometer
the two buttons are here this right button is to start the time the relay
and this is for reset or cancelling the activation of light. right now it shows
that my time is set for 9 seconds I can change it that's a maximum I can change
it even for zero second I set it so let's say it is set now for 4 seconds
when I process the light goes on for 4 second also it shows here the status and
if the light is off also shows the release off. now let me change it to 6
seconds now that is 6 seconds. and I will cancel it at the middle it's
running but I can just cancel it. at any moment run it again and cancel it at any
moment now I set the minimum 3 second the maximum for 30 seconds you can see
here when I rotate it to the left the maximum the minimum is 3 seconds when I
rotate this at the maximum it shows 30 seconds. now let's put it around
so let's see if we can set it for 20 seconds let me rotate it so you're
setting it for 20 seconds here it shows now let's start the load is running at
any moment you can cancel let's set it for 3 seconds on that will turn off of
3 seconds. thanks for watching this was how to use Arduino with potentiometer
and relate to control an AC bulb. if you liked this video please thumb up 👍 and
also please subscribe so you can get updates of my upcoming videos
Subtitle typed on August 22, 2020
I did my part to help you learn. Now it is your turn.
You can subscribe to my channel, thumb up 👍 the video and type a comment by saying thank you.
If you don't skip ads it will also help you .
My PayPal Account link is in the description if you want to make cash donation. Thank you