Hi, welcome to Arjuna tutorial by Robojax. in this video we are going to
learn how we can control this 28BYJ-48 stepper motors with three
push button. presses button motor rotate to the right, stop or to the left or maybe
directly to the right or just stop also to announce that I have launched my
Patreon account if you want to support me and get access to a behind scene and
extra material you can visit my Patreon accounts from the link below the video
in the description. You can get the code for this video by clicking on the link below
the video in the description which will take you
www.robojax.com/learn/arduino let's get further than this
My Name is Ahmad Shamshiri and I am from Canada
this push-button will control that clockwise and this will be for
counterclockwise and the middle one to stop the motor when you press this
button motor rotates to the right stop to the left or maybe directly to the
right or just stop I've seen a lot of requests and regards to controlling a
stepper motor using three push buttons so I spent some time and wrote this code
that you can press one button and it rotates in one direction the other
button it makes it rotate in the other direction and you can also stop it or at
the middle of rotation you can directly change the rotation on the other way
it's not just using these push buttons to control the motor but actually you
will have a lot of other applications for example you can have a sensor that
can detect an obstacle for example ultrasonic sensor or infrared sensor a
distance sensor or other some other type of switch and once you detect the
position or the distance instead of this push button you can modify this code
very easily to control this motor by those means this makes it very easy to
use. this could be wheel of a smart car or maybe a robotic arm that the detects an obstacle
on a stop or changes a direction. whether if you're studying mechatronics
electrical and engineering hobbyist researcher or high school students I
have an Arduino course that will teach you from ground up or do you know
why are doing a course at udemy is arranged such that all the material or a
structure from absolute beginner to advanced including all the library codes
schematic diagram an explanation step-by-step for all the projects are
available to you in no time the knowledge that you gain from my course
in Arduino you will be able to implement it and build cool robotic or
mechatronic projects that is beneficent for you and people around you
the link for my course on udemy is below the video in the description you can
visit it now
Wiring Explained
If you need wiring diagram for this projects, you can get it by signing to my Arduino course on Udemy.com
the link to my Arduino course is below this video in the description.
Here is have you connect to Arduino if you power this motor from Arduino
because it needs a little power very little power it works so you
connect this pin on the left side to the ground of Arduino that's wire I've
inserted and the second pin will be connected to VCC or to five volts in
here so ground that is the five volts on Arduino this is the jumper you leave the
jumper as is now this motor can be powered separately in case you want to
save let's say you're running Arduino and this device with a small
battery and this one with a larger battery or maybe this is with a small
power adapter and this is with a larger power adapter you can use different more
or even for that reason they put a jumper here so if you feel using
external power to power the smaller remove this and now the power to the
motor is disconnected but this power from Arduino will feed the IC so you
will connect your external power in here suppose this is your external power once
you connected the power goes to the motor but if you put this jumper and use
it with or dinner still it's fine for more do we know the power comes and
passes through this jumper and goes to the motor and this is the pin. the pin on
this side you can see the path here the path is coming here and it goes to the
first pin on the side if you don't hear the first pin the red the red is the
power pin for the motor. here is the wiring on the actual board the motor
comes with a connector I've just inserted it here and you don't have to
worry about it because you cannot insert this incorrectly
and in here the board have been labeled from IN1 to IN4 and we are not
using 5 6 & 7 because there's a 7 input
and here IN1 is connected to 10 and IN2 gray have been connected to 11 and
then purple from IN3 is connected to 12 and blue from IN4 have been connected to
13 the last one here from this module is a ground it has been connected to the
ground of Arduino. when you're using external battery make sure to connect
your battery this is my wire for the battery connect your battery to the
ground because you must have also a common ground and connect and connect
the 5 volts from your battery by removing by removing this pin and
connecting it to the last 10 if you're not using external power and you want to
connect this directly to Arduino connected in the second pair on this
side and then put this jumper here if you do not put this jumper it do not
work this is supplying power to the motor
from that 5 volt so you must put it here so this goes to the ground and this goes
now to 5 volts off Arduino. and here the push buttons have been arranged exactly the
same way and the same colors that I've shown you on the wiring diagram and I've
inserted it in such a way that this pin is connected to this blue line
which I've used it as a ground using this black wire I've connected it to a
ground of Arduino at this point and this push button switch is also connected to
ground this is also connected to the ground the left wire of this push button
this one is connected using white wire to pin 2
and the second one have been connected using gray wire to pin three and the
third one using a purple wire the third one from the left side have been
collected using purple wire to pin 4 of Arduino. I've spent almost one and a
half day working on the library and testing this module studying about the
material preparing writing code and testing it and recording and uploading
so you can easily use this code and learn about this the least that you can
do for me is to click on the subscribe button and leave the notification on
it's really appreciated thank you.
Code Explained
let me now explain the code this portion of the code is exactly the same as the
original code for this stepper motor I've defined which CW clockwise for pin
2 and switch stop for pantry and switch CCW counterclockwise for pin 4 these 3
pins are used to control the motor they are exactly the same as before we have polar
step zero and direction status 3 this 3 means stop initially we just put this
value but these variables do not change them they are used within the code and
so the setup this is exactly the same as before we are defining 4 pins as an
output and these are new in this section switch CW which is pin 2 3 & 4 we are
defining them as input and here I use pull up and pull up I have explained
this input pull up the pull up eliminates the usage of a resistor for
the switch so this is an input with a pull up when there is no connection is
always pull up which means keeps the pin at HIGH for the 3 pin. and inside
the loop we check each pin using digital read switch CCW we check the
counterclockwise switch and we see if it is low when the push button is pressed the
value goes LOW and when you release a button it will be HIGH so we check if it
is pressed then we set the direction status as 1 and we check CW if it is LOW
if the other button is pressed we set the direction as 2 so 1 is for one
direction 2 is for the other direction and 3 is stop and we check ever stops
which is equal LOW then we set a status equal 3 so this is done with set the
status and we check if the directional status is equal 1 then we increment
the steps we go up to 8 and then driveStepper(poleStep) this is the exact
code that was used in initial introduction
driveStepepr() function is explained at the end. Stay tuned.
and then if the the direction is equal 2 we do decrement so this is increment
decrement the rest of the code is exactly the same as before and we wait
one millisecond at the end of the code and the loop continuously checking the
switches or running the motor. now derive stepper is another function that
receives an integer and here is that and we receive an integer and we call it C
here when it arrives and then we use digitalWrite pen 1 and then we pole1
c, c means whatever the value is between 0 to 7 so we pass 0 or 1 or 2 so
this zero at pole 1 means for one of these values 0 means here 1 min here 2 &
3 & 4 & 2 means pull 2 it will pick up some value from here 3 but it will pick
up something from here and for something from this array and in the same time we
have to send command to all 4 pin in order to control the motors every time
he sent one value all of these will be executed immediately
Running code Demonstration
in the demonstration I've connected it using Arduino power there is no external
power it's directly connected the motor is now stopped if I press STOP (middle) nothing
happens I'm pressing the the right one which is clockwise so the motor
rotate clockwise and then if I want to stop it just stop or counterclockwise
now this rotates counterclockwise or just stop clockwise counter clockwise
you can change the code so we can it can be used for other purposes for example
instead of this push button you have some kind of sensor ultrasonic sensor or
maybe for a sensor or laser sensor that detects some distance and when the
object is close you can stop the motor if this is connected to the wheels or
maybe a robotic arm or any other application that you have the function
of these buttons can be used to control this motor using other means instead of
these push buttons if you are interested to control the stepper motor using
keyboard of your computers, I will try it now and if I press some other one and enter
stop J enter continuously controlling it so you can see also the lights here
because the speed is so fast you do not see the sequence of the light stop left
stuff you can also go from right to left to watch that video look at the link in
the description below this video thanks for watching us was how to control this
stepper motor using 3 push buttons and if you liked this video and learned
something please thumb up if you have comment or question posted in the
comment section below I always try to answer and reply and if you have not
subscribed please do so you can get updates of my upcoming videos
I would like to thank the following Patreon supporters of mine.
Subtitle Corrected and Typed on April 23, 2020 in Ajax, Ontario, Canada
I did my part to help you. Now it is your turn. you can do it in many ways.
First thumb UP the video. Subscribe and leave the bell ON so you get updates. Third, just type simple works in the comment. Say thank you