you can control a servo motor using a push button when you press it it goes
180 degrees and comes back to the location or it goes from 180 to 0 and
of code for controlling server Moodle 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
I also have a project that you can control a servo motor using push button
when you press the push button the servo motor starts from 0 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 the link
below it will rotate 360 degrees and you 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 it can go so this is mg 96 X or 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 middle in case we make mistake in terms
of connection and RC or car or airplane so the middle is always five volts and this
is a signal you might see it as a 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
configuration 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 a 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 this 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 (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 then tilde is here five six and nine
10, 11 these are all Pulse Width Modulation enabled pins for example seven and eight
will not work for will not work so keep that in mind.
Code explained
let me now explain the code the code starts from here include servo.h you
don't need to download anything it's part of this Arduino IDE when you get the
software its included and then we get this class here and create an instance
of it and we call it myservo this is our object and then we define servoPin
using define servoPin 3 and we define a pin that his pulse width
modulation enabled in Arduino Uno or the Arduino compatible boards you will see
this sign beside the pin and then we define push-button pin, pin 2 our pin 2
push button is connected to this pin and this is our
initial angle 90 degrees but make sure that if you are setting it set it not
zero or not 180 set at one degree lower one or 179 and then
angle step this is a step where the value will increment from initial value
now it's 10 so it will be 90 was 10 and then hundred hundred 10 20 40 it in
increments of 10 it will increase or decrease but if you wish you can change
it to 5 or even 1 but keep that in mind that it will take a longer time to go
through all those steps which I'm going to show you next if you want to
speed it up and this is a minimum angle and maximum angle you can also have the
option of keeping the servo within certain range let's say from 0 to 90 or
maybe 110 to 180 or what any number but keep that in mind because
this is minimum 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.
and here constant integer type you are defining a type we can put either one or
two if you put one value it will go from 0 280 and comes back to 0 if you make it
2 it goes to the opposite it starts from 180 it goes to 0 and comes back 180
which I'm going to demonstrate this value is holding the push-button value
when it is pushed do not change it inside the set up inside the set up from
here to here this happens only once. Serial.begin() initializes the serial
monitor serial monitor can be accessed by clicking a Tools serial monitor or
pressing control shift + 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 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 typed 3 here so pin 3 will be assigned to the servo and pinMode
pushButtonPin this is pin 2 this was as if i typed 2, we have defined it here we
are defining this pin as an input but I'm using also PULLUP the pull up 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 a separate video explaining that if you do
not use this pullUp or you just use input you need to use a resistor (1k to 100k ohm)
and then serial.print there's just plain text you see the double
quotation so this is our text and then myservo.write(angle) you can put a
value here and the cell will move 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
inside the loop we check the push-button using digital
read push-button pin this is pin 2 so we read the value and
compared it against the value of LOW using this comparison of two equal sign
and when press button is pressed it will be LOW if it is equal LOW then it means
this is true whatever is inside F when it is true then this will happen what
happens is we put one into the push button pushed value so now this will
have the value of 1 or true and then here we check it F push button and
button push this true to do it equally called 1 but that's excess this was very
simple it will work perfectly so if it is true then what we do is we get the
angle which we set above at 90 and this was 10 so 90 plus 10 is now hundred and
it is stored in the angle the same "angle" will be now updated now hundred so later
on in the code the servo will move to 100 degrees and we check if angle is greater
or equal and maximum angle let's say you set at hundred and eighty because it
will increment we will have some point that it goes above hundred and eighty
goes 190 so we want to make sure if it is equal or greater then we have to make
sure that we go backwards meaning angle we put angle step with an negative
now when this value is negative let's say this was 90 and this is negative 10
it will be 80 so now it will go in the reverse so just changes the direction of
rotation of servo and here the type, if type is equal 1 that's 2 equal sign
means compare make sure don't put one equal sign so if it is equal 1 then push button will
be 0 which means the push button is not pressed and this thing will not happen
the whole action will not happen which I'm going to show you and if the value
is smaller than angle again we will change the direction if the type is 2
then equal zero here we check if push button
if pushed that's why we said we updated when you
make it zero it will be false otherwise now it is true then myServo.right angle
the angle which is a hundred or whatever values the server will move and also
this will print it for us on the screen so on the serial monitor and there is
one hundred millisecond delay we give it enough time for the servo to move if you
need fast action reduce this until you see that the servo can move because
a characteristic of each star will be different so this is a delay that we
have between each movement and the loop will continuously take action and doing
the same thing
Demonstration of code
now let me demonstrate it I'm opening the serial monitor here the could have
been loaded to the Arduino Uno board and here if I press the reset button the
code will load again as you can see now initially it is set at 90 so the servo is
at 90 i when i press i release it goes 180 and stops. so this is in type 1 so let
me show you at the moment this is set to type 1 and it goes to 0 and comes back
180 now if I press it again you will see it goes and comes back and here if I go
now it you can see it was at 0 it was at 180 so it went from 180 to 130 up to 0
and then back to 180 so this is when you put in type 1 and here if we change it
to type 2 and upload the code
now when I press it it goes 180 and back to zero hundred eighty and back to zero
from now on every time it goes from zero to 180 and to zero so if the initial
value that you don't like it at ninety put it 179 or put it at one degree it
I've left will help my video in the first algorithm of YouTube if you have
comment our question posted a comment section below of your subscriber of my
channel I always try to answer and reply if you want to get updates of my
upcoming videos make sure to subscribe or control them one by one or just
Subtitle corrected and typed by Ahmad Shamshiri on april 01, 2020 in Ajax, Ontario, Canada
Covid-19 is infecting a lot of people around the world and I am home not going to my work to University.
I did my part to help you learn. Now the least you can do for me is to subscribe and 👍 the video.