Controlling 3 Stepper Motors with the AccelStepper Library for Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in a previous video I demonstrated how to run steppers without delay functions and without the use of libraries in this video I'll show a much easier way to do this with the Excel stepper Library this library is a convenient library that easily allows you to control multiple steppers position speed and acceleration the documentation on this Library can be at times a bit difficult to follow so I'm going to break it down with a few examples to start open your Arduino IDE and click on tools and then go down to manage libraries search for Excel stepper so in my case I already have it downloaded but if you don't click on install after it's done you should be able to go to the examples and see some of the Excel stepper specific examples for my circuit I'll be using an Arduino Uno three stepper Motors three a4988 motor drivers and three potentiometers for my power source I'm going to use a power supply that I set to 12 volts since I'm using a breadboard I've limited the current to one amp for each of the motor drivers the V motor and ground pin should be connected to a power source it's recommended that you use a capacitor here but for this demonstration I can get away without it the wires of the stepper motor should be connected to the 1A 1B 2A and two B pins the reset and sleep pins can be connected to each other and the enable ms-133 can be disconnected for now for each of the three drivers I've connected to the step pins to pins two four and six on the Arduino and for the Direction pins I've connected them to pins 3 5 and 7. bdd and ground should be connected to the 5 volts and ground on the Arduino along with the potentiometers the center pins of the pots should be connected to a0 through A2 alright so let's take a look at this first example what I'm going to start with is including the Excel stepper library then I'm going to create these six definitions for the pulse and the Direction pins for the Excel stepper object so for the first motor I'm going to have the pulse B pin 2 and the direction B pin 3 and then I'll have it set to 4 5 and 6 and 7 for the other two Motors to create the Excel stepper object I start by saying Excel stepper then my object name and then I have these three arguments so in this situation I'm always going to use one because this is defining that I'm using a stepper motor driver the second argument is for the pulse pin and the third one is for Direction so do this for all three and you can see in this case I just named it stepper one stepper two and stepper three now on the setup I'm going to set the max speed and the acceleration and you have to do that for every stepper motor object so you can see here I have stepper one dot set max speed to 200 and in full step mode this just means it's 200 steps per second then for the acceleration I have stepper one dot set acceleration to 100 which is saying that it's set to 100 steps per second squared and then I just copy this over for stepper 2 and 3. now in the loop I'm starting off with three different commands for stepper one I start by saying run to new position zero and what that means is I want it to take zero steps or position zero then I'm going to move on to run to new position 200 which means I want it to make a full rotation because this is in full step mode so it's going to take 200 steps then I have run to new position 100 which means I'm having it step backwards to the position 100. so if there's any confusion on which direction it's going to move in this instance since the 200 is greater than zero it's going to move counterclockwise but then in this case 100 is less than 200 in that case it's going to move clockwise so it's always dependent on the previous command so let's go ahead and upload this so it's going to make a full rotation go back to the half rotation point and then back to the starting position if I go back to the pin setup for ms1 MS2 and ms3 and I set it to half step so instead of a full rotation for 200 it means now it's going a half of a rotation and 100 is a quarter rotation so run to new position is a blocking function and this means that this command blocks any other command before it can move on so it must run to 200 before it moves on to this command running to 100 and I can demonstrate that by uncommenting this section here let's go ahead and upload this so what I've done is repeated the commands for stepper one stepper two and stepper three and if we take a look at the motors you can see that they don't run simultaneously it's waiting for every single command here to complete before it can move on and once it's at the end of the loop it starts all over again so that previous example is not really that useful because it blocks every other command and it doesn't allow for simultaneous actions to take place so now we're going to use what's called non-blocking functions so in this example we set it up the exact same way with including the library and setting up the pulse and Direction pins as well as setting up the stepper objects in the setup it's also exactly the same with setting the max speed and the acceleration for each of the steppers but what's different now in this setup is I have this command move to and so now I'm saying move to 200 so it's similar in the previous approach but we're doing it in the setup so in this case I have stepper one dot move to 200. stepper 2 dot move to negative 400 and negative means in the other direction and then stepper three dot move to 600. so now I'm going to use the command stepper one dot run stepper 2. run and stepper three dot run if I upload this you can see that the three motors are carrying out their commands simultaneously and the reason why that's happening is that the Run command only does one step at a time it's checking its current position and the distance it has to go and then stepping if that distance has not been reached the issue with this is that once it reaches its desired position or the set position of 200 negative 400 and 600 the motors stop moving so in this case for the sake of this example to show continuous movement I've just created this if statement here so what I'm doing is I'm checking if stepper one dot distance to go is equal to zero then I'm going to say stepper one dot move to the negative position of stepper one so what I'm doing is creating a new move to position and I'm only doing this for stepper one for this example so I'm going to go ahead and upload this so what happens is the three motors move to their initials positions of 200 negative 400 and 600 then because I've only set up this stepper one if statement only stepper one is going to bounce back and forth between 200 and negative 200. so this is the preferred way to move stepper Motors without blocking any other function or command so let's say that I want to move the motors and I don't care about acceleration and I just want a constant speed so just like the other examples I'm going to set it up in the exact same way I'll call the library I'll Define my pins set up my three stepper motor objects and then in this setup I'll use the command stepper one dot set max speed to a thousand it's probably recommended not to go above this depending on your motor and then I have stepper one that's set speed to 50. I've also set it up for stepper 2 and stepper three I've just set the speed to 100 and 500 then in the loop I simply use the Run speed command now if I run this you can see that all three motors are moving simultaneously so this may not be the most exciting example but if you make this set speed Dynamic then you can get some more interesting behaviors and I'll show an example of that okay so now let's say that I want to move the motor to a specific position and I don't care about acceleration I only care about the speed so just like before set up your library Define your pins for each of the motors and then create your three objects for stepper one stepper two and stepper three in the setup I set the max speed for each of the motors to a thousand steps per second and in the loop is where it gets different so for each stepper I set a speed a move to position and then I use the command run speed to position so in this example I'm setting stepper 1 to a speed of 750 and then moving to position 400. I do that for stepper two with 100 and 250 and 600 for stepper three so let's go ahead and upload this so if you take a look at the motors they're moving at different speeds to the desired position there was no acceleration it immediately moved at the set speed to the end position so let's move on to a bit more interesting of an example so what I want to do is use three pots to control the speed of each of the three stepper Motors so to start it's set up the exact same way with the library the pins and then the objects I've also created three variables called speed 1 Speed 2 and speed 3. in the setup I've set max speed to 500 for each of the three motors then in the loop I use the map command and I'll have arguments analog read a0 and then I'm mapping values of 0 and 1023 to negative 500 and 500 since that's my Max Speed I've set here what I'm doing is I'm essentially taking the values from analog read which vary between 0 and 1023 and mapping it to negative 500 and 500. I then put that into set speed and then use the command run speed and then I'll do that for the other two stepper Motors so if I were to upload this you can see that the potentiometers control the speed of each of the motors if I move it all the way to the right it's going to go full speed in One Direction and then if we move it all the way to the left it goes to full speed in the other direction all right in the last example I'm going to use the potentiometers to control the position of each of the stepper Motors so it is set up in the exact same way as the previous example except I've created the three variables position one position two and position three I've set the max speed for each of the motors to 500 and then in the loop I use the same mapping technique so I'm going to read the analog value from the potentiometer map those values between 0 to 1023 to 0 and 200 this time because this is now a position I'm going to set the speed to 500 and then I use the command move to position one then I'll call this by run speed to position then I repeat this for the other Motors so after I upload you can see that as I move the potentiometer the motor position changes but it does look a little bit jittery so let's explore why that's happening so let's print out some values so I'm going to start by setting up the serial monitor and just for the example I'm going to print position one right now I'll open up the serial monitor but it looks like it's bouncing around between 106 and 107 and because of that that's why you're getting that jittery movement the motor is trying to bounce between 107 and 106 or whatever value you have it set at so you can fix this with a few different techniques but that'll be for a later video thanks for watching [Music] foreign
Info
Channel: The Bored Robot
Views: 17,292
Rating: undefined out of 5
Keywords: Theboredrobot, Arduino, stepper, accelstepper, robotics, STEM, 3dprinting
Id: QRCvC5xhJCw
Channel Id: undefined
Length: 11min 48sec (708 seconds)
Published: Sun Aug 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.