In this tutorial we will learn everything we need to know about controlling stepper motors with Arduino. We will cover how to control a NEMA17 stepper motor in combination with a A4988, a DRV8825 and a TMC2208 stepper driver. This combination of stepper motors and drivers is used in countless applications where position control is needed, such as 3D Printers, CNC Machines, Robotics, Automation machines and so on.
I will explain in details how they work, how to connect stepper motors with Arduino, 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 watch the following video or read the written tutorial below which also includes all example codes and wiring diagrams.
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 any feedback. The working principle of a stepper motor is based 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 continues motion of 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 How Stepper Motors Work tutorial. A typical stepper motor, a NEMA17 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 the 4 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 number of wires of a stepper motor. It has 4 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 also stepper motors with 5, 6 or even 8 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 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 the 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 functions like micro stepping, current limiting, and so on that enable us to easily control the stepper motors, which is the whole purpose of them.
All right, now we can take a look at the first example for this tutorial, how to control a NEMA 17 stepper motor with an A4988 stepper driver. All right, now we can take a look at the first example for this tutorial, how to control a NEMA 17 stepper motor with an A4988 stepper drive. The NEMA17 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 NEMA17 stepper motor has 200 steps, or 1.8 degrees per step resolution, but there are also models with 400 steps and 0.9 degrees per step resolution. We should note here that the designation NEMA17 actually describes just the size of the motor in terms of the front faceplate size.
The number stands for the size of faceplate in inches when divided by 10, or in this case that would be 17 divided by 10 equals 1.7 inches faceplate, or 2.3 inches faceplate in case of NEMA23. So, the faceplate size is fixed, but the length of the NEMA17 steppers can vary from 20mm to 60mm, 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 NEMA17 steppers motors is from 0.3A up to 2.5A.
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 controlling for NEMA17 stepper motors is the A4988 stepper motor driver. The A4988 has a maximum current rating of 2A per coil, but that’s actually a peak rating. It is recommended to keep the current to around 1A, but of course, it is also possible to go up to 2A of good cooling is provided to the IC. A great feature the A4988 stepper driver has, actually all other drives 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.5A rated stepper motor, but we will limit the current of the driver to 1.5A. 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 on 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.
All right, so now let’s see how to connect the A4988 driver with the stepper motor and the Arduino controller.