Arduino Tutorial 35: Understanding How to Use a Stepper Motor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number 35 in our legendary new and improved series of tutorials on the Arduino microcontroller I will need you to pour yourself a large mug of iced coffee and I will need you to get out more the Lego starter kit the super starter kit if you don't have one check out the description below the link will take you to a kit on Amazon for $35 you'll get all the hardware that we are using in this entire series of Arduino tutorials and seriously it does help a little bit if we are working with the identical hardware all right what are we going to learn today we are going to learn about stepper motors this is a monster you don't have one of these in your kits this is like a mini monster this is like Papa Bear mama bear and then on the on the arduino kits you kind of have baby bear but the thing is the one that comes with the kit is suitable for learning on and so will be able to learn on this one and then if you know how to use this one you can come in and you can use the bigger ones but let's talk a little bit about stepper motors Before we jump in there's some very important things to know about stepper motors one of the really good things about stepper motors is they allow for very precise control you don't just hit it with a voltage and let the thing spin the way you turn it is you kind of click it like one step at a time alright and different stepper motors have different step sizes but a step size can be a fraction of a degree like a tenth of a degree and so one of the characteristics of a stepper motor is you have very precise control of position the second characteristic of a stepper motor is at low rpms you have very large torque and so you can move things very precisely and you can move them with a large amount of torque now we have done motion in some of our earlier lessons we learned how to use a servo but one of the things about a servo is you don't have that precise control position most of them don't have that much torque and most of them have limited ranges of motion 0 to 180 the advantage of a stepper motor is it's really easy to use the disadvantages can't make it go all the way around for most of them limited range and not that positionally accurate and not that strong so if you need something like that you want to move to a stepper motor these are the disadvantages of a stepper motor they're kind of tricky to use they draw a whole lot of current and so you cannot power a stepper motor off of the Arduino even for these small ones you cannot power them off of the Arduino you need an external power supply and luckily you've got one here in the in the super starter kit the thing also to know is is that it's very tricky to get these things working and so the parameters that one stepper motor uses are not the same as the parameters another stepper motor you see you actually got to open up the instructions and read and look at the datasheet the other thing that you have to keep in mind that's very important on on these things is is that you've got to use a stepper motor driver that matches the stepper motor that you're using because like this is a little stubborn motor driver made for small stepper motors if I tried to run this one off of this then I could run into trouble I could burn out this driver or cause problems with the motor similarly you've got to make sure that your power supply will generate enough current to run the stepper motor driver to run the stepper motor and so the nice thing is with these parts and the e Lego kit they're all matched up this has enough current this can drive this and it will all work out fine but just be aware of that be aware of that that you've got to be a little bit more mindful and not just going and randomly plug stepper motors into stepper motor drivers now these big ones like this one this this this one there's a part called an easy driver and the easy driver will drive these this would be something that would be used like on a 3d printer so applications of separate motors would be things like a CNC machine where you've got to move a cutting tool around that's kind of might be large and heavy so you've got to have a lot of torque and then you're doing precision works you've got to have a lot of torque in high precision and so 3d printers and CNC machines laser engravers things like that use stepper motors like this but several learners like this size will run off of something called an easy driver and then they've got something called the Big Easy or the Big Easy driver that can drive these bigger ones but just make sure that your driver is designed for the stepper motor that you're going to use alright enough of this nonsense let's move on what you will need for today's project is you will need the power supply out of the e Lego kit you will need a switch you will need your stepper motor driver you will need your stepper motor you will need the Arduino you'll need a 9-volt battery my 9-volt battery that came with the kit I ran it out so I had to get another 9-volt battery and then you need the little clip that allows you to plug the 9-volt battery into the power supply and lucky for you it's your lucky day that that clip itself is also included in the in the starter kit and then you need this this ribbon cable so let's jump right in and get going on this project and one of the first things that I want to tell you is and this is not just something you're learning for this project this is like one of the number one reasons that I find that more complicated projects that people are working on don't work when you have a lot of different components you have to make sure that they all share a common ground and so like if you look over here Arduino has a ground and you're gonna start plugging things into Arduino you've got to make sure that the Arduino ground is connected to the stuff that you're using over here now let me maybe start with showing you how to plug in this power supply into the board you could plug it in this way or you could turn it around and plug it in over here but what you have to see is is that do you see how these these power rails and ground rails are marked like this one is Plus this one is minus this one is plus this one is minus make sure that you put it in in a way that that matches the direction that it's plugged in so here in this direction this is Plus and that goes into Plus this is minus that goes into minus this is Plus that goes into Plus this is minus that goes into minus because if those labels don't match these labels it would be very easy for you to plug something in wrong and then you see that it just plugs in very nicely there and you've got kind of like plug plug plug and plug in that all plugs in now the first thing I want to do because it's the easiest thing to forget is to plug a ground wire over to Arduino so everyone agrees where ground is so notice I'm putting it in this minus row the minus row is coming over to GND on the Arduino and so now everybody agrees where a ground is and that's very very important okay what else do I need to do now well I guess that I need to begin by hooking the stepper motor up to so we're gonna hook the stepper motor up to the stepper driver if you notice there's only one direction that this will plug in you've got those notches that go in these grooves and so there's only one direction that this will plug in where the little raised things go in those little notches and so that is plugged in to the stepper motor now we need to hook the stepper motor driver up to the Arduino and that's where this is going to come in now the key thing is that you need to get these in the right direction and what you need is you need I in on the motor driver to go to pen 8i and to depend on in3 to pin 10 and I in for to pin 11 okay and I like to do that by using this ribbon cable and then I'm gonna start with the outside one which for me is black your colors might be different but you've just got to make sure the right one goes to the right one so my first one is going to be the black into iron one then I'm going to put for me the next wire is white into I into for me the next one is gray into I and three and then purple into I in for there okay so let me see if I can show you that see see I got four wires black white gray purple and it goes into respectively I and one I into I n 3i and for now coming over here remember it starts with it all starts with don't come out of there okay it all starts with black and where did we say that black needs to go it's going to go into pin 8 and then white the next one is going to go into pen 9 so I have pin 8 10 9 and then the next one is gray is going to be pin 10 and then the last one is purple which is pin 11 and then that for me we'll put things what is this one we had a we had an arrow okay black into pin 8 and I had grabbed the wrong wire so let's look at that black indepen white into pin 9 gray into pin 10 purple into pen 11 and then we still have our common ground wire so that is looking good now coming back over to our driver coming back over to our driver you can see that we've got to power the driver here and we have five volts that it wants well it says it'll take between five and twelve all right but the negative is the outside one and so let's look very carefully here you see this first pin is negative and so I'm going to have to put another wire which is going to be my purple wire or my blue wire on that outside one the blue one on the outside one where do I need to take the blue one on the other end on the other and the blue one needs to go to the negative which is that ground rail okay the ground rail and now what am I going to need I'm going to then need the second outside one okay and that is going to be my next wire which is my green wire and I need to take that to five volts this is where it gets really important this green wire is 5 volts not the five volts on the Arduino because you cannot power this whole thing off of the Arduino it has got to come from the power supply so I put it on the power supply rail and I'm really trying to do this where you can see it okay so you see the plus the power supply rail you see that green to the plus to the plus five so now let's see what we have here okay so now let's kind of plug this thing in so I'm going to plug my battery in plug my battery in and then I'm going to plug my battery into my power supply and what happens absolutely nothing well realize that the power supply has a push button and if I push the button look at that happy little green light comes on and then look at this I've got happy little I've got happy little lights coming on here and your lights might not on here if you haven't got the thing I programmed up yet so don't worry about that but you should see a light there okay so I have got let's think about this I've got the motor connected to the driver and I apologize that this ribbon just will not get out of the way but the motor is connected to the driver the driver is powered through the power supply through this rail these rails power supply is turned on and then I have got pins 8 9 10 and 11 hooked up right pins 8 9 10 and 11 hooked up so let's come over here I actually think we are probably ready to start coding now this is plugged in for pins power coming over to power and guys make sure that you're using this one that's labeled 5 volts where the jumper is on 5 volts I think this thing comes where this other rail is at 3.3 volts make sure that you're hooked up to the 5 volts and so now let's get all this stuff up here and it is really annoying to get all this stuff where you can see it okay now we are ready to start coding so let me come over here to a coding view like this so you can see me and you can see what I'm typing so what do we need to do in this project the first thing that we need to do is we're gonna run the stepper motor so we need to load a library and I think we talked about this on the servo lessons how to load a library that it is the pound include and in this case which library will you want to open your you want to open your triangular bracket and you're gonna say step eh you're gonna load that and if you installed the software with me in Lesson number one you will already have this libraries all you got to do is load it remember on and include it is one of the only steps that you don't put a semicolon on the end of it strangely enough now what do we want to do we've included that now we want to we've got to tell it a parameter of our stepper motor and that's for a particular stepper motor how many steps for that motor make up a revolution and for this stepper motor which is the twenty eight byj - 48 by Ross a RoHS that number is 2048 so I'm going to say steps per revolution steps per revolution is equal to 2048 you've got to get that off the spec sheet for the motor that you're using it's not always going to be 2048 and 2048 in my experience is a little bit of an unusual one to use okay now what we also need to do is we need to create the stepper okay so we're gonna use the command stepper from the stepper library and then I'm gonna call it I'm gonna call it my stepper that's my stepper motor my stepper and then I need to tell it for this motor what are the steps per revolution steps per revolution which we set to 2048 okay and now I've got to give it the pin sequence and this is going to be eight comma 10 comma nine comma 11 and the reason that is true the reason that that is true is because that is what works for this stepper motor with this driver connected to the pins the way that I connected them that is not always going to be true if you're using a different driver or you're using a diff motor you're gonna have to go in and you're gonna have to figure out what that sequence is I can't tell you a magic formula that will always work for everything all right now we come to the void setup it's usually a good idea to do a serial dot begin in case we need to do some troubleshooting and printing some stuff out that looks good then what we are going to need to do is we're going to need to tell the stepper motor how fast we want to go so I'm going to say my stepper dot set speed and let's go note speed well if I'm gonna use mote speed I better come up here and defined it right and oh I forgot to put it here I'm sorry I hope you guys were yelling at me at that okay and then I need an int and mote speed is going to be equal to I am going to say 10 rpm and understand that these stepper motors you can't run real fast and so this 110 is a pretty good clip for it so then I come down here to mote speed now it knows what mote speed is and so that looks pretty good I've got that set alright what I need to do in the void loop well I am going to do a I'm gonna tell the thing to move I'm gonna now move it and so what I'm going to do is I am going to say my stepper dot step I'm gonna tell the stepper motor to step how many steps is it going to make whatever number I put in here if I put one it's going to do one step which would be about a tenth of a degree because if if for this motor 2048 steps make one revolution then one step would be about two tenths of a degree so I want it to go one full revolution so I would just put in your steps per revolution this is just for demonstration you could put in here whatever you wanted okay and then what I'm going to do is I'm going to delay by delay time well I better go set that up up there give that a value so I'm going to say int delay time is going to be equal to 500 milliseconds and then that's going to make one revolution in the positive direction I'm going to want to make one revolution in the negative direction so my stepper dot step and I want to go - steps per revolution [Music] revolution and that will make it run backwards because I put the negative in there and then what do I want to do I want to delay by delay time and then it's going to loop so it's going to make one revolution in the positive direction one revolution in the negative direction theoretically this is really a lot of complicated stuff here so it makes me somewhat nervous all this stuff whether it is going to run or not and so then let me also just so we can see get a little piece of our pookie and put it on there so we will have something to look at like such nice piece of pookie which will stick anything to anything and then i have a nice little arrow and we will put the arrow on like that okay so now let's download this hold-your-breath please download it and look look giddyup giddyup look at that all the way around once change direction went back words did you see that we got a stepper motor runnin here man we have got a stepper motor running look at that and look it looks like it's really making one revolution okay why is it making one revolution correctly because we knew that this magic number should be 2048 Wow look at that look at that all right so let's play around with some of these parameters don't change this parameter because that's particulars but what if we came down I'll tell you what I'm going to do I'm going to time this and see if we are really at about 10 rpm so I'm going to time it so when it stops I'm going to start my timer okay I started it how long does it take to go around look at that six seconds it took six seconds so 60 divided by six is ten it is going ten rpm so everything is behaving beautifully beautifully like what we expected what if we came down here and instead of going steps per revolution what if we went steps per revolution divided by two saying that we want to go half a revolution now let's see what happens made-to-order half a revolution now let's see let's go back to one revolution and let's go like a let's go a motor speed of two let's go a motor speed of three revolutions per minute and then I will time that and see how it works okay okay look at that it's gonna go all the way around slower because we slowed down the rpms look at that okay and it is going to stop at 20 20 seconds which would be three rotations per minute alright guys we have got our stepper motor running and now it's time for me to give your you your homework assignment your homework assignment is to incorporate this little button into the project and this is what I want I want the motor to run forward not one revolution but to just run forward continuously forever run forward continuously but if you press the button when you press the button it runs backwards and you press it again it runs forward so this button is going to toggle you from clockwise to counterclockwise and every time you press the button it switches direction I will give you a hint if you are not sure how to do that we played around with those buttons if I can find my mouse we played around with those buttons in lessons I believe the last lesson we talked about it lesson 33 and then also we talked about the buttons it looks like in lesson 27 in lesson 28 but try to do it on your own okay try to do it on your own and then when you come back I want you to pause the video when you come back I'll do it and you can do it along with me but man you're never gonna learn really learn how to do something if you're just following what I'm doing I have taught you enough in these lessons to do this on your own I want you to go away and do it on your own and then when you come back you can see how I did it make it work though without watching me leave me a comment down below that you were able to make it work without watching me and then tell me you did it the way I did it you did it a better way or you did it a harder more complicated way okay you guys go pause go do it and then come back some of you didn't pause did you you're just going to continue to copy forever aren't you you're just gonna copy me for it all right well anyway nonetheless we are going to move ahead so what we are going to do is we are going to come over here and we are going to get this button hooked up okay we're going to get the button hooked up and I'm going to use the hack that I talked about I believe in lesson number 34 which allows us to do this without an external pull-up resistor and so I am going to come to the left pin of the button and I'm going to come to pin 2 on the Arduino alright and to know I think I just unplugged my stepper motor and so I'm going to be very careful that green goes to 5 volts green goes to 5 volts and blue goes to ground ok yeah and it's running again so accidentally pulled that thing out as I was hooking up the button so one leg of the button goes to pin 2 on the Arduino and then the other leg of the button is going to go to ground and this is this common ground right we got all of our ground sit together so I put it on the ground rail so now the button is hooked up now we've got a code it where the button actually does something so let's come over here and what I'm going to do is all that code up there was for the stepper motor and so I'm gonna come down to a couple of you know just leave some space there and then if I'm gonna use a button I've got to have a button pin so I'm gonna say int button pin and where do we hit connect the button we connected it to pin 2 ok now remember how I'm gonna make the motor run in two different directions so I've got to keep track I've got to have a variable that's telling me whether it's going in the pause direction or the negative direction and so I'm going to have a variable called motor direction and in my mind mentally plus one means it's going in the clockwise direction and minus one means it's going in the counterclockwise direction so I will start it out going clockwise but this variable will keep track of which direction the motor is going got to keep track of that it's kind of like dropping a bread crumb that helps you get out of the forest you've got to remember which direction your motor is going now also I've got to read the button Val but for this assignment you were to make it toggle so just press release it changes direction press release it changes direction press release it's toggling between two states and therefore I need to have a button Val new and what that is that is where you are presently that's whether the button is pressed or not pressed that's the present state of the button and then I'm also going to need to know on the last time through the loop where was the button so that would be button Val old because if we were up and went down that's when we want to change direction so we've got to keep track of where the button was last time and where the button is this time and since the first time through the first time through I can read the new value of the button but there won't be an old value so I need to give it something so I'll just put in to get the thing started say that the old position of the button was was a1 which is basically not pressed and that will kick start the thing without having any errors okay now we have got to all right we set the stepper up already so now in the serial Dopp again I've got to do that little hack on that pin where I can make that that button work without an external without an external pull-up resistor and if you remember and this thing is kind of falling over there if you remember the way you do that is you set the pen mode of your button but contend to input so you tell it it's gonna be an input so you can do a digital read from it and then this is the crazy thing that I think I talked about in the previous lesson which was and this is this is hard to see this the previous lesson was lesson number 34 I showed you how to do this little hack you do a digital right to button and you make it high that's kind of crazy you said that it was gonna be an input that but then you wrote to it high well when you write to it high it puts five volts on it and when you do the pen mode it puts that pull-up resistor in there so you kind of end up with what you want if you do this if you understand it go back to that go back to that last lesson all right now we have got motor speed we already did that we turn the serial port on we've got that all set up okay let's come down to our void loop in our void loop what is the first thing you want to do what you got to figure out where the button is so how do I do that I need to do a digit toll digit all read of button and write and where do I put that well this is the fresh new value so we put this button Val new equal so this is the fresh new read of where the button is it's the fresh new read of where the button is okay so now if okay if button Val old equal equal once so last time around the previous loop the button wasn't pressed it was up okay and if button Val new equal equal 0 this represents a transition last time nobody was pressing the button now the button is pressed that means the button had has been pressed in that case you want to change directions now you'd say well why not just say if it's zero because what if I pressed it and held it down every time it's zero if if if you said switch directions every time it's zero you're going to press the button it's going to read read read read it's going to be reading zero zero zero zero and it's gonna go like this okay so the only time you change direction is if you were up last time in this time down and then you switch directions I hope that makes sense so what do you do if button old Val equal equal one and button Val new equal equal 0 that means the buttons been pressed you want to change motor direction so motor direction is equal to motor Direction times minus one and I'm going to put parentheses just because I can my math teacher I like parentheses motor direction that is crazy yeah that's good motor direction is motor direction times minus one that is proper just like that okay motor direction is motor direction times minus one you know what's happening over here our battery is running down and so what I am going to do is I am going to unplug the battery and I have one of those wall warts that's ten volts and has the right plug on it so we are going to go with that because our battery will running down quickly these things draw a lot of current and so they will chew your battery up okay so that's working like a charm so remember positive one means you're going in the positive direction negative one on the letter Direction means you're going into the negative direction so how can I switch I can take motor direction and multiply by negative one which means if I was going this way and I multiply by negative one I'm gonna be at negative one going the other way then negative one times negative one is going to be positive one so negative one times motor direction is going to be a clever little way to toggle this thing okay so motor direction is motor direction times negative one now I have the direction right now what do I do just my stepper dot step remember that command what do I do well I just go one step motor direction times one step so if motor direction is one I'm going to go one step in the positive direction if motor direction is negative one I'm going to go one step in the negative direction so she's gonna be clickity clickity clickity clickity clickity clickity all the way around okay and then when I press that button I'm gonna multiply multiply M motor direction by negative one and it's gonna switch direction does this make sense oh my goodness I hope this works hold your breath please Oh boom at least it's working at least the thing is going okay let's come up to the full view here alright so at least the motor is spinning okay and I got to make sure that I don't pull out any of these wires alright do you see the little switch the moment of truth the moment of truth okay the moment of truth I press the button oh look at that giddyup you see that I changed direction let's try it again and put back to the positive oh I pressed it twice okay oh you know what I forgot to do you know why this isn't working I forgot a very important step I forgot a very important step did you guys see what I forgot what do I need to do my button Val old is equal to button Bao new all right so this is what I forgot to do after you get to the bottom of the loop that new button value it's still now it's old it goes into old and then you come up here and you get a new button Val new and if you don't do this it doesn't work the second time you press the button I about had a heart attack I thought maybe I hadn't pressed it good but I didn't have this command in here so let's download that okay now it's going that way now watch I'll press it one time the change direction it changed direction again now if I press the button and hold it it'll change direction one time okay and when I let it up it doesn't change direction because we're looking for that one to 0 transition right ok I can press it fast present slow it works every time okay guys leave me a comment down below was anybody able to do this project on their own were any of you able to do this project on your own because I gave you everything if you're following these lessons along you should have been able to figure it out even though I did something really clever on the toggle here in the way that I just made motor direction is motor direction times negative one if you had watched lesson number 13 for was it was it lesson number 34 I keep forgetting what lesson yeah if you'd watch lesson number 34 you could have kind of figured out how to do this but leave me a comment was anybody able to figure out how this thing worked you guys think about giving me a thumbs up on this video did any body was any body able to do this on their own was anyone out there able to do this on their own or did everybody just copy me okay did everybody just copy me leave a comment let me know think about subscribing to the channel think about sharing this with other people this is Palmer quarter from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 94,293
Rating: 4.9532871 out of 5
Keywords: Stepper Motor, Driver, Arduino, Toggle
Id: CEz1EeDlpbs
Channel Id: undefined
Length: 40min 9sec (2409 seconds)
Published: Tue Nov 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.