Hi, welcome to an Arduino tutorial from Robojax. my name is Ahmad Shamshiri. I am
presenting this tutorial from Canada. in this tutorial I'm going to show you how
we can control a servo motor using a push button and Arduino when you
push the push button and keep pressing it, it goes between zero and hundred and
eighty degrees when you release it at any moment it will just stop. and you can
sit in between any angle for example between 10 and 150 now if
I keep at rest it goes between 10 and 150 so you can set your
boundary. you can get the code for this by clicking at the link below the video
in the description which will take you to www.robojax.com/learn/arduino
let's get started with this
I will reply to all comments and questions of my channel subscriber make
sure to subscribe and leave the notification on I have different types
of code for controlling servomotor using push buttons because they are required
in many applications
also have a project that you can control a servo motor using push button when you
press the push button the servo motor moves either from 0 to 180 and stops or
moves from 180 to 0 and stops.
also have a project that you can control a servomotor using push button when you
press the push button the servo motor starts from zero to 180 and returns back as
long as you keep the push button pressed when you release a push button the servo
motor will stop.
also I have another project that you can control a servo motor using two push
buttons one push button to move the servo motor to the right and the other
push button will move the servo motor to the left when you release a push button
the servo motor will stop.
also I have a project that you can control 16 servo motors with different
type of codes you can get the code for this by clicking at the link below the
video in the description which will take you to
www.robojax.com/learn/arduino
Servo motor explained
let me briefly explain the servo motors servo motor is a type of motor that the
shaft can be controlled to move only 180 degrees or half a circle
something like that and then you can control precisely where it should be
between 0 and 180 degrees there are also other types of servo motors that for
example there is something called continuous servo which I have (a video) the link
below it will rotate 360 degrees and these are the most common one and also
you might get this type of servo motor which is very heavy with high torque is
the same way
so this is MG996R version and this is the metal gear this is all metal and
this is a plastic gear so this will be much cheaper like three or four or five
times cheaper than this but in general this is the operation and so the servo
motor has three wires the darker wire is the ground the five volts is always at
the metal in case we make mistake in terms of connection and RC or car
airplane so the middle is always five volts and this is a signal you might see
it as black wire and this side red is always red and this side you might see
white but doesn't matter so they're the same figuration is exactly the same
ground five volts and signal
Wiring Explained
now let me explain the wiring. for the wiring I'm using this push button here
the push button is of long pin type with two pins it makes it easy you
can get it from eBay or Amazon I have the link below this video so the two pins
of the push button is connected to the ground and to pin 2 . so my push button
is connected to these two pins my servo which has three wires I've connected
using this extra extension here to make it easy
connect the brown wire to the ground (or GND) of Arduino the middle pin to five volts and
the signal wire orange to pin 2 in this case and if you pay attention here we
have tilde ~ at pin 3 and tilde is here 5 6 & 9 10 11 these
are all Pulse Width Modulation PWM enabled pins for example 7 & 8 will not work 4 will
not work so keep that in m ind
code explained
let me now explain the code we are including the servo header (file) the servo dot
H is part of that like Arduino IDE you don't need to download anything it's
already available but in this line we are using this servo class and we
created an instance of it we call it myServo and in this line defined servo pen
we define pin 3 for servo to be connected the signal line and this is a
pins that has tilde you can choose any other pin that have tilde which means
it's pulse width modulation enabled and we use pushButtonPin pen 2 for push
button pen which we use a across the code and the angle initial value you can
set other value if you wish angleStep is the increment step every time it moves from
one value to other and the increment of 5 if you want it to go faster make it 10
15 whatever you want if you want to make it very slow or precise go to even one
one degree and these two values are minimum and maximum of angle I put this
because sometimes you don't want from 0 to 180 let's say you want from 0 to 90
or you want from 100 to 120 or some some value but if you want to 0 to 180 leave
it as is keep that in mind because this is manual angle this value must be
smaller than this value otherwise this code will not work
minAngle must be smaller than maxAngle or this code will not work.
inside the setup() from here to here this happens only once. Serial.begin()
begin initializes the serial monitor serial monitor can be accessed by
clicking a Tools->serial monitor or pressing ctrl shift and M or on the
right side here you can click and this is the serial monitor will open and will
print this text. at the right side at the lower corner you will see this number
from here you can set the baud rate 9600 baud or 9600 bits per second this if I
close it this is a value it must match otherwise you will not be able to read
the value myservo.attach(servoPin) this as if I type 3 here so pin 3
will be assigned to the servo and pinMode push-button pin
this is pin 2 this is as if I type 2 we have defined it here we are defining
this pin as an input but I'm using also PULLUP the PULLUP job is to eliminate
usage of a resistor when the button is not pressed it always pulls it up or
full set to 5 volts. I have separate video explaining that if you do not use this
PULLUP or you just use input you need to use a resistor
and then serial.print that's just print the text you see that double
quotation so this is our text and then myServo.write( angle) you can put a
value here and the servo moves to that location because this is inside the
setup this is our initial position of the servo if you want to put any other
value or something just put here the servo will be initially at that location
and inside the loop we use while and digitalRead(pushButtonPin and we read the
push-button value and we're checking it every is equal LOW then the push button
is pressed we have to do something here we take the angle and add this if this
was 90 this will be plus 10 it will be hundred so we incremented and then angle
will be there and here we check if angle is smaller than minimum angle smaller
equal or this pipe or two vertical the lines are called pipe character or
angle is greater than or equal maximum which means it's above 180 it means
below zero so we should not allow it immediately we change the direction by
adding this minus and it will be stored here so let's say if this was 90 this is minus
10, 90 plus minus 10 will be 80 so the calculation will be 80 and the servo
which in this line we use myServo.write angle instead of 90 it will go
into 80 so this portion the job of this one is to just keep it within the
boundary and then we use Serial.print move to this is a text you see the
double quote and this is the actual angle that we have will be printed here and
with a space we print the world degree and you see print, print and this half Ln
Ln mean newline so these will be shown side by side and this will create new
line so this will be printed as one line and we give it 100 milliseconds for
the servo to reach to the position you can play with this if you wanted to
speed it up reduce this value but you cannot go very low below certain value
depending on your server because a server will not respond and it will have
no meaning if you go below a certain value and then the loop will continue
take the same action over and over again
Demonstration
now let me demonstrate it. that code have been loaded to my Arduino Uno and
serial monitor is open here initial value was ninety so it's at the middle
now when I press this it will start moving and also it shows on the serial
monitor now it is set between 0 and 180 degrees at any moment right but if I
keep it pressed it goes to the two boundaries of upper and lower and stop
when I release it. if I set this let's say between 80 and 130 let me upload it
now this will go if I press it you will see that it goes it started
from hundred 110 and it goes back to 190 and then those values so is this within
the bound
I would like to thank my Patrons from Patreon.com
you
if you learn something and found if you thought please thumb up 👍 as this will
help my video in the search algorithm of YouTube. if you have comment or question
posted a comment section below. If your subscriber of my channel I always try to
answer a reply if you want to get updates of my upcoming videos make sure
to subscribe or control them one by one
Subtitle Typed and corrected on April 02, 2020 at 00:09 in Ajax, Ontario, Canada
Coronavirus is dominating USA and Canada. Hopefully we get out of this pandemic. God willing.
or just control for example fie