How to Control NEMA 17 Stepper Motor with Arduino and A4988 Driver

If you’ve come to this article, you’re probably at the same point as I was before I made my stepper motor move without noise, vibration or jump steps. That’s why in this article, I will provide a sufficient level of depth to know how to control a stepper motor (for example – a NEMA 17 or other stepper motor size) and fixing the problems that may arise.

When a stepper motor is used in a 3D printer, most likely a team of electronics and programming experts makes calculations and put everything together to works perfectly. Instead, when I try to use the same stepper motor in a hobby project (after a normal 8 hours daily job) it’s quite different. Moreover, things get complicated if you have a low budget and just wanted to use the stepper motor to make a robot or rotate a cup holder like in the way I did. All of these situations may give you big headaches.

Let’s start with the beginning and I’ll tell you in a few words from where the whole story started. It is possible to write a long story in this article, but I have a lot to explain to you about stepper motors.

How to Control NEMA 17 Stepper Motor with Arduino and A4988 Driver

How I Start With The NEMA 17 Stepper Motor

First of all, I will comment based on NEMA motors. Like many others, I’ve heard about the NEMA stepper motors and I thought ‘NEMA’ is the brand of the motors. Totally wrong! NEMA is a standard that describes stepper motors. For example, NEMA 17 is a stepper motor with a 1.7 x 1.7-inch faceplate. In other words, NEMA 17 is a size, not a series of stepper motors.

Why a stepper motor?
The stepper motors are used to precisely control movements, whether in cars, robots, 3D printers or CNC machines.

In my project, I need a precise 360-degree rotation of a plastic cup holder. After I did some research on how I can accurately rotate 360 degrees, I came to the conclusion that a stepper motor fits best into my project. I could have used a servo motor with continuous rotation or a motor with encoder, but another reason was the budget. I have a few NEMA 17s in my spare parts cases and I started to use them for various projects.

Both the stepper and the driver are from a printer kit that has never worked. I managed to assemble it, but I did not manage to put it into operation. Problems have arisen since the beginning when I didn’t find some pieces in the package. I had replaced most of the missing parts with all sorts of improvisations without success. Finally, I disassembled the 3D printer kit and I use the NEMA 17 motors in my DIY projects.

A simple tutorial with a few lines of code and some instructions to connect the wires can be useful only for someone who is trying for the first time to control a stepper motor like NEMA 17. But this is just the beginning. The next step is to make an application that uses such an actuator along with other technologies such as remote data transmission via Bluetooth. Why is this aspect important? Using a Bluetooth module such as HC-05 or HC-06 interferes with the stepper motor controller. This may lead to uncontrolled rotations, noise, and vibration of the stepper motor.

The tutorials from the Internet didn’t help me
Okay, now I’m exaggerating a bit. Some of the tutorials that cover the subject of controlling the stepper motor helped me in one way or another. But my system is more complicated and none of the tutorials include the information needed to turn the shaft of a stepper motor while applying torque. Furthermore, the motor is part of a system that includes many servo motors, limit switches, and a Bluetooth connection.

This configuration with a stepper, servo, limit switch, and Bluetooth is quite common, but the tutorials that I found covers only the subject of controlling the stepper motor. After all, I managed to document each control system and the interference between the Bluetooth and the stepper motor. Finally, I managed to control the robot as I wanted it to be.

In other words, even if the NEMA 17 topic exists in a large number of articles and DIY projects, in this article I go from theory to practice and fully describes the steps and mistakes I did in designing, assembling and programming a rotating base for a cup holder with a stepper motor like NEMA 17.

The main part – to simply call it, the rotating base – is the plastic cup holder that is driven by a NEMA 17 and controlled by the A4988 driver. Arduino UNO is the brain of the whole system.

The whole assembly of the stepper motor is a bit more complex than a plastic cup holder. A switch sensor is used to detect the cup in the cup holder, and a pump will extract liquid from a container and fill the plastic cup with juice. The NEMA 17 and the switch limit sensor works together to precisely position the plastic cup holder and identify whether or not there is a plastic cup in the cup holder.

The most important specifications of the stepper motor:

  • Size and version: NEMA 17 42SHD0034-20B
  • Step Angle(degrees):1.8°
  • Phase:2
  • Rated voltage: 12V

Such a motor has a price of about $14. If you are looking for the exact version of NEMA 17 used by me, you will have the surprise to find that there are few places where you can buy one. The reason is that this type of NEMA 17 is used, in the vast majority of cases, in 3D printers. The good part is that what I write in this article can be applied almost entirely to other versions of NEMA 17 and other sizes of stepper motors.

The NEMA 17 stepper motors are found in several dozen models, so be careful with the motor model used in your project. Do not try to feed it with a higher voltage than the specifications.

My NEMA 17 requires 12V to work. My power adapter has a 12V output, which is perfect for what I need. I connected the 12V output from the power adapter to the A4988 driver to power the NEMA 17 motor.

The control of the NEMA 17 motor was not as easy as I had expected. After connecting the wires as in the below diagram, I wrote the Arduino sketch to rotate the stepper motor. The surprise was when I saw the motor shiver spinning, and then a sound when the motor stops. This is the case when you read a tutorial and it seems that everything is easy, but when you start building something, nothing seems to work.

The A4988 Stepper Motor Driver Carrier

The A4988 driver used in this project is disassembled from the same 3D printer.

Few words about this
The good part was that I knew I could control the stepper motor with this driver. The bad part is that it was necessary to make adjustments to control the motor to reach the desired result. Theoretically, I would have to connect the stepper motor to the driver, the driver to the Arduino, and then control the stepper motor without any problems. Things work differently than my expectations. The fact that I use a Bluetooth connection make a change in the driver’s behavior for normal operation.

The A4988 driver is a cheap hardware resource (~ 1EUR / piece) and can control stepper motors powered by a voltage between 8V to 35V.

Until I explain the settings from the driver, I will start with the beginning – with the power supply.

Working with A4988
To work, the A4988 needs a 5V voltage source. All the project is powered by a 12V – 3A power adapter and an adjustable LTC3780 DC buck/boost converter that lowers the voltage from 12V to 6V. The output of the LTC3780 is 6V because I need to power the servo motors of the robotic arm. The same 6V output I connected to Pololu’s S7V7F5 voltage regulator with 5V output. So, in the voltage regulator go 6V and pull out 5V – exactly what I need to power the A4988 driver.

I use the same voltage regulator to power the stepper motor driver, the Bluetooth communication module, and other components that require a power source of 5V.

The A4988 hardware configuration
I’ll start with what you do not have to do. I did it and I did the wrong thing. I burned an A4988 driver by connecting and disconnecting the stepper motor while the driver is powered on. In other words, rewiring anything while it is powered is asking for trouble.

Info: All these steps should be done only if you have experience in electronics, otherwise you do it on your own responsibility.

Current limiting
The driver’s current limitation is the first step and requires applying a mathematical formula. In my case, I have to set the current limit to 1A per phase using the formula provided in the “Current limiting” section of the A4988.

Since I have an 1 amps stepper motor, the formula is:

  1. 1 = VREF x 2.5 (in case of A4988)
  2. VREF= 1 / 2.5
  3. VREF = 0.4V (400mV)

The second step was to find out the Vref location on the breakout board. I search on the Internet and I found the location. It is the center point of the potentiometer.

I connected the ground of the multimeter to GND pin of the driver and the “+” probe of the multimeter to Vref on the driver board. At this step, you have to be careful not to cause a shortage by sliding around with the tip of the screwdriver.

I used a small screwdriver to rotate the screw from low values to higher values. If you try to do it, please make sure you know which direction lowers the current! Start from lower values of the current to higher values. Also, in case you have to do it, my recommendation is to use a plastic screwdriver, otherwise you could short the driver and finish it.

The control
In my case, I skipped the half-step control. Tested by others, the A4988 driver has a large deviation at the half-step position.

The stepper motor used by me requires 200 steps for a full revolution, which means that every step turns the shaft with 1.8 degrees. Full-step means turning the motor shaft to 1.8 degrees for each step. This mode of operation of the motor has only one phase energized at a time. One of the benefits of full-step control is the minimum amount of power from the driver of any of the excitation modes.

The full-step mode may cause the stepper motor to jump from one step position to the next. As a result, you may have torque ripple and vibrations.

Microstepping is another method to control NEMA17. In my project, I used this method. Micro-stepping means to divide the motor’s basic step by up to 256 times. Yes, a full step rotation is first divided by 200 steps and then divided each step by 256 times. This makes the small steps smaller, but do not be happy yet. You’ll see why.

Let’s start with the positive parts. One of the advantages of working in this way is to increase the resolution for the NEMA 17 motor. In other words, the motor spins smoother at low speeds and has fewer vibrations, which is very good for me since I use a plastic structure that amplifies the noise and vibrations of the motor.

In my project, the position of the plastic cup relative to the robot arm is very important. I have left a few millimeters for a possible error in positioning the plastic cup, but in such a complex system where combining an automation part with a robotic arm may occur a lot of errors.

Dealing with a driver without a feedback system (the motor driver does not know the exact angular position of the motor shaft), the driver won’t correct the deviations of the motor. In this case, the solution was to modify the design of the robot to handle several millimeters of deviation for the plastic cup.

Arduino library to control the stepper motor

The library can be used for any of the A4988, DRV8825, DRV8834, DRV8880 drivers and two-pin stepper motors. I do not need all the library functions, everything I use was the stepper motor function for rotating the shaft.

I used states and switch to program all the steps of the robot waiter:

Demo with the plastic cup holder rotated by a NEMA 17 stepper motor



Related Posts

Don't Miss Out!

Get the latest news, tutorials, reviews and more direct to your inbox when you subscribe!