Stepper Motors and Arduino - The Ultimate Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello dan here from howtomechatronics.com and in this tutorial we will learn everything we need to know about controlling stepper motors with arduino we will cover how to control a nema 17 stepper motor in combination with a 4988 a drv8825 and a tmc2208 stepper driver this combination of stepper motors and drivers is used in so many applications where position control is needed such as 3d printers cnc machines robotics automation machines and so on i have already used it myself in many of my arduino projects like my diy camera slider the 3d wire vending machine my diy foam cutting machine the laser engraver the pen plotter the scara robot arm and several other projects i will explain in details how they work how to connect them how to set the current limit of the drivers and how to program them with or without an arduino library also i will show you how we can easily control multiple stepper motors using an arduino cnc shield for any type of arduino project so we got quite a lot to cover in this tutorial you can jump to any section of this video using the timestamps in the progress bar or in the video description before we start let's take a look at this video sponsor altium who offer by far the most trusted pcb design solution on the market ultim designer represents decades of innovation and development dedicated to creating a truly unified design environment it comes with all the functions you would ever need when it comes to not only designing a simple and quick pcb but also very complex and demanding one there's also altium 365 which is the electronics product design platform that unites pcb design mcat data management and teamwork you can design share and manufacture all in the same space with nothing extra to install or configure you can connect to the platform directly from altium designer without changing how you already design electronics and what's also great is that you can try all of this for free by clicking and following the link in the video description alright i will start with briefly explaining what is stepper motor and how it works as it will help us better understand everything else in this tutorial a stepper motor is a unique type of brushless dc motor which position can be precisely controlled even without a feedback the working principle of a stepper motor is based of course on magnetic fields it has two main components a stator and a rotor the rotor is usually a permanent magnet and it's surrounded by some coils on the stator when we energize or let current flow through the coils particular magnetic fields are generated in the stator that either attract or repel the rotor by activating the coils step by step one after another in a particular order we can achieve continuous motion of the rotor but also we can make it stop at any position so that's why these motors are called stepper motors they move in discrete steps by increasing the number of magnetic poles on the rotor we can increase the number of possible stopping positions thus increase the resolution or the precision of the motor please note that this is just a basic explanation and you can find more details in my house stepper motors work video a typical stepper motor a nema 17 for example has 50 stopping points or steps on the rotor on the other hand the stator can have several coils organized in two phases which provide four different magnetic field orientations or positions so the 50 steps of the rotor multiplied by four different magnetic field orientations make total of 200 steps for completing a full rotation or if we divide 360 degrees by 200 steps that's a resolution of 1.8 degrees per step i mentioned that the stator coils are organized in two phases and we can also notice that if we take a look at the number of wires of the stepper motor it has four wires two for each phase the four different magnetic field orientations are possible as we can let current flow through the phases in both directions there are also stepper motors with five six or even eight wires but they still work on two phases or we control them with just four terminals the thing with them is that they can provide different performance characteristics like more torque or more speed depending on how we connect these wires on the four control terminals nevertheless with this brief explanation now we understand that for driving a stepper motor we cannot just connect power to it as nothing will happen instead we have to energize the two motor phases in both directions and activate or send pulses to them in a particular order in a timely sequence so that's why we need drivers for controlling stepper motors there are many types and sizes of drivers corresponding to many types and sizes of stepper motors however the basic working principle of all of them is that they have two h bridges that allow energizing the motor phases in both directions of course they have many other components and other functions like microstepping current limiting and so on that enable us to easily control stepper motors which is of course the whole purpose of them okay so now let's take a look at the first example for this tutorial how to control a nema 17 stepper motor with an a4988 stepper driver the nima 17 is the most popular stepper motor among makers as it offers great performances and it's affordable at the same time it can be also found in almost any desktop 3d printer and laser engraver generally the nema 17 stepper motor has 200 steps or 1.8 degrees per step resolution but there are also models with 400 steps or 0.9 degrees per step resolution we should note here that the designation nema 17 actually describes the size of the motor in terms of the front faceplate size the number stands for the size of the face plate in inches when divided by 10 or in this case that would be 17 divided by 10 equals 1.9 inches face plate or 2.3 inches face plate in case of nema 23 so the faceplate is fixed size but the length of the nema 17 steppers can vary from 20 to 60 millimeters and with that the power requirement of the motor also varies the power requirement is usually defined by how much current the motor is allowed to draw and the range for these nema 17 stepper motors is from 0.3 up to 2.5 amps now according to the current rating of the stepper motor we need to choose a suitable driver which can handle that amount of current the most popular driver for controlling nema 17 stepper motors is the a4988 stepper driver the a4988 has a maximum current rating of 2 amps per coil but that's actually a peak rating it is recommended to keep the current to around 1 amp but of course it is also possible to go up to 2 amps if good cooling is provided to the ic a great feature the a4988 stepper driver has actually all other drivers have is the current limiting with this we can easily set how much current the motor will draw no matter the motor rating for example we can connect even a 2.5 amps rated stepper motor but we can limit the current of the driver to 1.5 amps so although the motor won't work at its maximum capacity we would still be able to use it on the other hand if the motor is rated lower than the set current limit of the driver the motor would overheat of course it's always recommended to try to match the current rating of the motor with the current rating of the driver alright so now let's see how to connect the a4988 driver with the stepper motor and the arduino controller at the top right corner we have the v-mod and ground pins and here we connect the power supply for the motor which can range from 8 to 36 volts here it is also recommended to use the decoupling capacitor across these two pins in order to protect the board from voltage spikes we should use large electrolytic capacitor with at least 47 microfarads capacity next are the four pins where we connect the stepper motor one phase of the motor goes on one a and one b pins and the other phase go on two a and two b pins sometimes it can be a bit difficult to recognize which two wires of the motor would make a face but there are several ways to identify them the simplest way is to rotate the shaft of the stepper motor by hand and connect two wires to each other if you connect two wires that make a face the rotation of the shaft would be a bit more difficult another way is to use a multimeter and check for continuity between the two wires if you connect two wires that make a face you will have a short circuit and the multimeter will start beeping once we find the face we can connect it to any position of the two positions of the driver the order doesn't matter next we have the ic or the logic power supply pins vdd and ground which can be from 3 to 5 volts on the other side we have the step and the direction pins which can be connected to any pin of the arduino board with the direction pin we select the rotation direction of the motor and with the step pin we control the steps of the motor with each pulse we send to the step pin the motor will advance one step in the selected direction right above these pins we have the slip and the reset pins which are used for as their name suggests putting the driver to sleep mode or resetting it we should note that both of these pins are active low the slip pin by default is high state but the reset pin is floating that means in order to have the driver enabled the easiest way is to just connect these two pins to each other assuming that we won't use these pins functions the enable pin is also active low so unless we pull it high the driver will be enabled the next three pins ms-1 2 and 3 are for selecting the step resolution of the motor we already said that the step resolution depends on the construction of the motor which is usually 200 steps per revolution for a nema 17 stepper motor however all stepper drivers have this feature called microstepping which allows driving the motor at high resolutions this is achieved by energizing the coils at an intermediate current levels which produce intermediate step locations for example if we select quarter step resolution the 200 steps of the motor will be 200 multiplied by 4 equals 800 microsteps per revolution the driver will use four different current levels on the coils to achieve this the a4988 driver has a maximum resolution of 16 micro steps which would make a 200 steps nema 17 motor has 3200 steps per revolution or that's 0.11 to 5 degrees per step that's quite impressive precision and that's why this type of stepper motors and drivers are used in so many applications actually there are stepper drivers that can have up to 256 microsteps or that's a whopping 51 200 steps per revolution nevertheless these three pins have pull down resistors so if we leave them disconnected the driver will work in full step mode for selecting a different stepping resolution we need to connect 5 volts to the appropriate pins according to this table alright so now that we know how to connect the stepper motor and the driver to the arduino board we can move on with explaining how to program or code the arduino for controlling the stepper however before we do that or before we power the motor there is one more important thing that we need to do and that's to adjust the current limit of the driver there's a small trimmer potentiometer on the a4988 driver through which we can adjust the current limit by rotating the potentiometer clockwise the current limit rises and vice versa there are two methods which can be used for determining the actual value of the current limit the first method involves measuring the reference voltage across the potentiometer itself and ground we can measure this reference voltage using a multimeter and use that value in the following formula to calculate the current limit of the driver the current limit equals v reference divided by a times rcs the rcs is the current sensing resistance or the values of the current sensing resistors located right next to the chip depending on the manufacturer these values are usually 0.05 0.1 or 0.2 ohms so we need to take a closer look at the value of these resistors in order to accurately calculate the current limit with this method as an example if we measure a reference voltage of 0.7 volts and we have 0.1 ohms resistors the current limit would be 0.875 amps or if we want to limit the current to let's say 1 amp we should adjust the reference voltage to 0.5 volts the second method for setting the current limit is by directly measuring the current to the coils for that purpose we need to connect the stepper motor and the driver as explained previously we can skip the controller connection and instead connect the 5 volts to the direction and the step pin so that the motor stays active and holds one position dms pins should be left disconnected so the driver would work in full step mode then we can disconnect one line or coil from the motor and connect it in series with an ammeter in this way once we power the driver with both the logic voltage the 5 volts and the power for the motor 12 volts in my case we can directly read how much current is running through the coils though we should note here that when the driver works in full state mode the current in the coils can reach only 70 percent of the actual current limit i tried both of these methods for setting up the current limit of the driver and they gave me approximately the same results nevertheless now we can move on with programming the arduino or take a look at several example codes for controlling a stepper motor with an arduino board let's start with a very basic example code of how to control a stepper motor without using a library here all we have to do is define to which pin number the step and the direction pins are connected and define them as outputs in the loop first we set the rotation direction or the motor by making the direction pin either high or low then using a for loop we send 200 pulses to the step pin which will make the motor rotate a full cycle considering that it works in full step mode the pulses here are generated simply by toiling the state of the step pin high to low with some time delay between them this time delay actually defines the speed of rotation if we lower it the speed of rotation will increase as the steps will occur faster and vice versa then we change the rotation direction and using another for loop we send 400 pulses which would make the motor rotate to full cycles however if we change the microstepping mode of the driver let's say to a quarter step which would make the motor have 800 steps now the first loop will make the motor rotate only 90 degrees and the second loop only half rotation here is another simple example controlling the stepper motor speed using a potentiometer for that purpose we just have to connect the potentiometer to the arduino and read its value using the analog read function we can then map or convert the potentiometer values which are from 0 to 123 to values suitable for being a delay time in microseconds for the step pulses i found the minimum value for the delay time between the steps to be around 300 microseconds by going lower than that the stepper motor started skipping steps overall controlling stepper motors with this method is easy and it works but only if the required control is simple as shown in the examples in case we need more complex control the best way is to use an arduino library the most popular library for controlling stepper motors with arduino is the excel stepper library by mike mccooley it's an extremely versatile library featuring speed acceleration and deceleration control setting target positions controlling multiple stepper motors simultaneously and so on the library has also a great documentation explaining how each function works i have already used this library for several of my arduino projects for controlling the motion of my diy camera slider the 3d wire vending machine the scara robot arm and few others in case you are interested there are details and code explanation for each project on the website now let's take a look at few example codes using this library the first example will be controlling the speed of the motor using a potentiometer so here first we need to include the excel stepper library of course before we do that we need to install the library and we can do that from the arduino id library manager we just have to search excel stepper and the library will show up and we can install it then we need to create an instance of the excel stepper class for our motor the first parameter here is the type of driver and in this case for driver with two control pins this value is one and the other two parameters are the pin numbers to which our driver is connected to the arduino if we have multiple stepper motors we need to define each of them like this and we can name them however we want in this case i named my motor stepper one in the setup section we just have to set the maximum speed of the motor which is defined as steps per second this value can go up to 4000 but in the documentation of the library it is stated that the speed values of more than 1000 steps per second might be unreliable in the loop section using the set speeds function we set the current speed of the motor in this case that's the analog input from the potentiometer which is from 0 to 123. in order the motor to move and implement that constant speed we need to call the run speed function each interval a negative value here or simply including a minus sign before the value would make the stepper motor rotate in the opposite direction here's another example controlling two stepper motors with implementing acceleration and deceleration using the excel stepper library so we need to define the two stepper motors and in the setup using the set acceleration function set the acceleration value for the motors using the set current position function we set the position of the motors to be at zero steps in the loop section we start with the move to function through which we tell the motor to what position to go or how many steps it should move in case of quarter step resolution 800 steps would mean one full rotation then the run to position function moves the motor to that position while implementing acceleration and deceleration however this is a blocking function so the code execution will stay there until the stepper motor reaches that position with the same method we move the motor 1 600 steps or two full rotation with quarter step resolution if we don't want our code to be blocked until the motor reached that target position instead of using run to position function we should use the run function the run function also implements acceleration and deceleration to achieve the target position but it just makes one step per call therefore we need to cool it as frequently as possible for that reason here we put the run function for both motors in the while loop which is executed until both steppers reach position zero we previously set the two motors to go to position zero with the move to function here we could also add more coat in that while loop and do other stuff along running the motor actually there are many methods of running the motors and doing other stuff too i recommend going to the nicely described documentation of the library so you can understand how each function works and implement them according to your needs nevertheless i would like to show you one more example using the excel stepper library and that's controlling multiple stepper motors in a coordinated fashion this means that we can set target positions for each stepper motor and they can reach their positions all at the same time no matter the different distance they need to travel this can be easily done using the multi-stepper class that comes with the excel stepper library here we also need to include the multi-stepper class and create an instance of it then we need to define an array type long which will be used for storing the target positions for our motors in the setup section we need to define the maximum speed values of the steppers and add the steppers to the previously created multi-stepper instance which in my case i named it stepper's control in the loop section we start by storing the target position values in the array that we previously created i set the first stepper to move one rotation the second two rotations and the third one three rotations then we can assign this array to the move to function which will calculate the required speeds for all motors to arrive at those locations at the same time then we just have to call the run speed to position function which will move the motors to their position though we should note that this function blocks the code until the steppers reach their target position we should use the run function instead if we don't want to block the code we should also note that the multi-stepper class doesn't support acceleration and deceleration nevertheless if you want to learn more from more advanced examples you can check my arduino project that i already mentioned all the details and the codes for them are on the website still talking about controlling multiple stepper motors it's worth mentioning and taking a look at the arduino cnc shield the main purpose of the arduino cnc shield is for controlling two or three axis cnc machines but it is actually a great option for controlling any type of project where we need to control multiple stepper motors as it's compact and provides easy connections for the drivers and the motors this shield goes on top of an arduino uno board and we can control up to 4 individual stepper motors and have all the remaining arduino pins available for use i use this combination of an arduino uno board and a cnc shield for controlling the four stepper motors for my four axis cara robot arm and they work great okay so now let's move on and see how we can control stepper motors using the other drivers that i mentioned at the beginning the drv8825 and the tmc2208 actually everything we explained so far about controlling stepper motors with the a4988 stepper driver applies for these two drivers as well the working principle the connections and the coding are almost the same for all of these drivers the difference between them is in their technical characteristics and now we will take a look at them and compare them the drb8825 is a stepper driver by texas instruments which can be used as direct replacement for the allegro a4988 driver as their connections are the same the three key differences between them are that the drv8820 can deliver more current than the a4988 without additional cooling 1.5 amps versus 1 amp it has higher maximum supply voltage 45 volts versus 35 volts and it offers higher microstepping resolution 32 versus 16 microsteps of course they also have some other minor differences for example the current limit potentiometer has different location and the relationship between the current limit setting and their reference pin voltage is different the drb8825 doesn't need logic power supply and that pin location is used as fault output however it is safe to connect the full pin directly to 5 volts so that's why the drv8825 can be used as a direct replacements in systems designed for the a4988 driver it's worth noting though that when replacing the a4988 driver with an drv8825 it is very important to make sure the orientation of the driver is correct i already mentioned that their potentiometers are at different locations on the a4988 it's below the chip and on the drv8825 is above the chip and that sometimes causes confusion and the driver can be easily placed on the wrong side so please be careful with this for setting the current limit we can measure the reference voltage with one probe on the ground and the other probe on the potentiometer itself the formula for calculating the current limit of the drv8825 stepper driver is as follow the current limit equals the reference voltage times 2. as for selecting the microstepping resolution we can use the following table overall the drv8825 is a better stepper driver than the a4988 as it offers higher current and voltage ratings and higher microstepping results which eventually results in smoother and quieter operation of the stepper motor speaking of smoother and quieter operation let's take a look at the tmc 2208 stepper driver the tmc 2208 chip is made by trinemic a german-based company specialized in motion control electronics the tmc2208 is a silent stepper motor driver which can be used as a direct replacement in systems designed for the a49 or the drv8825 drivers it is widely used in desktop 3d printers laser engravers scanners and so on what sets this driver apart from the two other is its integrated interpolation unit which provides 256 subdivisions or microsteps this allows for almost perfect sinusoidal control which is generated internally within the jeep this means that the driver will output 256 micro steps to the stepper motor no matter what microstep resolution we have selected through the two ms pins 2 4 8 or 16 microsteps this provides smoother operation and reduces the burden of the microcontroller significantly this feature of the driver in combination with its noiseless current control still chop 2 technology provide that ultra silent control of the stepper motors here's a comparison of the noise levels between the 3 drivers [Music] the tmc 2208 drives the stepper motors completely silently which is really impressive the current rating of the tmc tmc2208 is slightly higher than the a4988 driver or it's 1.2 amps with 2 amps peak current for setting the current limit of the driver again we use the same method as explained for the other drivers we need to measure the reference voltage with one probe on the ground and the other on the hole right next to the enable pin the formula for calculating the current limit is as follow the current limit equals the reference voltage times 0.71 although it can be used as a direct replacement the tmc2208 driver has a slightly different pin out compared to the a4988 driver here we only have two pins for selecting the microsteps resolution and for enabling the driver we need to connect the enable queen to ground in terms of coding it's the same as the two other drivers the tmc2208 driver has also some other more advanced features compared to the two other drivers it provides more tuning and control options overall the tmc 2208 is a better driver than the a4988 and the drv8825 but that's normal as it comes with a higher price tag though if you don't need these extra features and the noise levels doesn't concern you the two other drivers are just great choice so that would be all for this tutorial if you are interested in learning how to control bigger stepper motors like the nema 23 or the nema 34 i will have a dedicated tutorial for that too i hope you enjoyed this video and learned something new don't forget to subscribe and for more tutorials and projects visit howtomechatronics.com
Info
Channel: How To Mechatronics
Views: 962,866
Rating: undefined out of 5
Keywords: Arduino, Stepper Motor, How It Works, A4988, DRV8825, TMC2208, Tutorial, Steppers
Id: 7spK_BkMJys
Channel Id: undefined
Length: 29min 57sec (1797 seconds)
Published: Sun May 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.