Controlling Nema 17 Stepper Motors

The Nema 17 stepper motors are some of the most used on a wide range of applications(3D Printers, Industrial machines, robotic arms, …).  There are lots of manufacturers and several different models of this kind of stepper (with different torque, step angle, voltage, current…). This post presents one way (the easiest and safe one) to control Nema 17 bipolar, 4 wires stepper motor.

The Motor

 

Mechanically, all the Nema 17 motors have the same to profile, screw hole sizes / positions and same shaft diameter. On the other hand, the can have different side profiles(taller / lower), depending on the torque specifications.

Eleetrically, there are several differences:

Connector: The motor may have a connector, which is a standard one (all motors use the same – the one in the pictures above), or may not have. Some models – the cheaper ones – don’t have connectors (the wires come from inside the motor), what make them more difficult to fix in case of wire breaking problems.

Poles: Unipolar and Bipolar. The Unipolars use (in general) 5 of more wires, while the bipolares use only 4 wires (the most common). There are also the hybrid ones (which I’ll not describe).

Wires: The wire colors don’t have any pre-defined meaning. Each manufacturer use the colors they want. But, in general, each wire has a different color, to make easy to manage them.

Step: The most commonly used angles are 0.9 and 1.8 degrees. The bigger the angle the smaller the number of steps on a full turn and, therefore, less smooth.

Voltage: There are steppers with a variety of operating voltages (1.5V, 2.4, 5V, 12V, 24V and, likely, voltages in the middle of them and above). The bigger is the voltage, bigger is the torque and less current is drawn.

Current: The current drawn by the stepper is, generally inverselly proportional to the operation voltage. It’s an important detail, once the power supply must be capable of supplying it.

Holding torque: Unlike the regular motors, the steppers, and particularly the ema 17, are capable of keep torque while static (not turning). That means the can hold some “weight” without any movement. The bigger is the holding torque, the bigger is the weight / force it can hold withot turning.

Speed / RPM: It’s the amount of turns the stepper can do per minute.

 

A4988 Stepper Driver

Although it’s possible to control bipolar stepper motors using H-Bridges, this post will not cover it. This post is about the use of an easy, reliable and good cost/benefit way: The A4988 stepper driver module.

This driver performs all the low level operations related to the stepper control. It “exports” a simple interface which allows you to set: The rotation direction; The precision (steps and micro-steps); Enabling / disabling the motor (to save energy). To do all of this you only need to set digital signals (GND, 3.3V or 5V, depending on the operating voltage of the circuit) to some pins.

To make it easy to explain the module working, let’s say call the negative signal (-) / 0V as LOW. Also, lets call the 3.3V / 5V as HIGH.

  

Right pin set:

  • VMOTOR: Positive pole from the motor power supply. It can handle voltages from 8V to 35V, which must match the motor operating voltage.
  • GND: Wire this pin to the motor power supply negative pole (LOW).
  • 1A and 1B: Wire pair related to the first coil (actually, it doesn’t matter which is the first or the second).
  • 2A and 2B: Wire pair related to the second coil.
  • VDD: Wire to the positive pole (HIGH) from the module power supply (it can be the 3.3V or 5V pins from arduino).
  • GND: Wire this pin to the negative pole (LOW) from the module power supply (it can be the GND pin from arduino)

PS: Both GND are connected (shorted) internally on the module, in a way to keep the same ground referential for both motor and arduino / A4988 power supplies.

Left pin set:

  • ENABLE: This pin is used to enable (obviously) or disable the module, meaning: when you set the pin to HIGH, it turns the module off and the motor is no longer maintaining the holding torque.
  • MS1, MS2 E MS3: Pins to set motor step to full or micro (see table below).
  • RESET e SLEEP: For the goal of this post, the should be connected to each other (shorted).
  • STEP: When this pin is set to HIGH the module performs a full-step or micro step, dependending on MS1-MS3 configuration.
  • DIR: Tells the direction of the turning: set to HIGH for one direction, set to LOW for the opposite one.

Step and Micro-step

The Nema 17 standard operation mode for A4988 is “full step” (steppers with 1.8 degrees step performs 200 steps for each turn). It’s also possible to select 4 different micro-stepping modes, changing the step angle to 1/2, 1/4, 1/8 e 1/16 of a full step(1/2 of 1.8 degrees = 0.9 degres, ….).

To select the step / microstep mode, its necessary to set the pins MS1, MS2 e MS3 according to the table below:

MS1 MS2 MS3 Operation Mode
LOW LOW LOW Full step
HIGH LOW LOW 1/2 step
LOW HIGH LOW 1/4 step
HIGH HIGH LOW 1/8 step
HIGH HIGH HIGH 1/16 step

The procedure for step and micro step is the same:

  • Select the turn direction setting HIGH or LOW on the pin DIR
  • Set a HIGH signal on the pin STEP for 1ms and then set LOW to it. The motor is gonna turn the degree amount of degrees specified by the pins MS1 to MS3.

 

Identifying the Motor poles

As said before, there is no standard for the wires colors on a Nema 17. So, it’s necessary to identify which wire is connected to which coil. There is a simple way to find the correct wire pairs: It’s necessary to get a multimeter, set the mode to “resinstance” or “impedance” and proceed as the images below:

  

 

 

Adjusting the motor current on the A4988

Every different Nema17 motor has its own operation ideal current. The A4988 drivers, on the other hand, are generic, to allow them to be used with a wide range of specifications. So, it’s very important to adjust the current to the specified for your motor. If this adjustment is not done properly, the motor can overheat with an excess of current and burn itself out. Also, if the current is smaller than the specified, the motor can lack of torque or even don’t turn at all.

One way to adjust the current is following the procedures below:

  • Wire the multimeter test leads in series with a wire of one of the coils and select the “current” mode;
  • Set the MS1 to MS3 pins to 0, selecting the Full Step mode;
  • Turn the motor power supply on;
  • DO NOT execute any pulse to the STEP pin (this leaves the motor in “holding torque”);
  • Check the multimeter for the measured current: This value should be 70% of the specified current for the motor (in “holding torque”, the A4988 sends only 70% of the current to the motor). Ex: For a 0.4A motor, the measuerd current should be +/- 0.28A);
  • Turn the adjustment “trimpot” (the little potentiometer on the top of the A4988), using a small screwdriver until the desired value is reached.

 

The measurement should be done using the definitive VMOTOR and VDD voltages. If the voltages are changed, it’s necessary to perform the measurement and adjustment again.

 

Test Circuit

It´’s possible, and not difficult (there are plenty of tutorials on the internet), to assemble a circuit on a breadboard, using the A4988 modules, for testing Nema 17 motors. I used a very simple wiring schematics, widely replicated on the internet, to assemble my first prototype:

  

 

But my idea was to assemble something more permanent, that I could use to test Nema 17 motors very quickly. So, I designed a circuit and called it Nema Tester.

 

Nema Tester

The goal of this circuit is to be able to test Nema 17 motors and adjust A4988 driver modules very quickly and easily.

The wiring schematics for “Nema Tester” is shown below:

Components

Arduino Nano
A4988 - Stepper driver module
5cm x 7cm Double sided universal prototype breadboard
Female Jack 5.5mm x 2.1mm
2x Push Buttons
2x 10KΩ 1/4W Resistors
100µF 16V Eletrolytic Capacitor

The capacitor voltage doesn’t need to be exactly 16V, but it’s necessary to be a least o higher than the motor power supply voltage.

 

2x 2-Way Screw terminal block connector

In my circuit, I used 3-way terminals, just because I didn’t have the 2-way ones.

 

Assembly

Here, some pictures explaining some of the assembly techniques I used.


In the first “line” of the board I streched a wired to be used as the Ground (GND / LOW) of the circuit. All the pins wired to ground are wired to somepoint of this wire.


A good way of soldering the pin headers for the modules (Arduino and A4988) is: inserting the modules in the headers and inserting the headers in the desired place of the board.


The pins on each end of each header should be soldered first (as shown above) then the rest of the pins can be soldered. This procedure make sure the headers stay in the correct position during all the soldering process.


I used wires on the top side to connect the pins. This way the soldering on the bottom side gets easier and cleaner.


Pins soldered on the bottom side of the board.


Basic wiring finished.


NemaTester finished, including push buttons (and their pull-down resistors) to control the motor. Also added a capacitor to remove some possible electrical interference from the power supply.

 

Arduino Code

To control the NemaTester, I developed the code below:

const int dirPin = 2;
const int stepPin = 3;

const int btnDirPin = 7;
const int btnStepPin = 5;

boolean btnDirPress = false;
boolean btnStepPress = false;

boolean motorDirFWD = false;

void setup() {
  pinMode(dirPin,OUTPUT);
  pinMode(stepPin,OUTPUT);
  pinMode(btnDirPin,INPUT);
  pinMode(btnStepPin,INPUT);
}

void checkButtons() {
  if (digitalRead(btnDirPin) == HIGH & btnDirPress == false) {
    // direction button pressed
    btnDirPress = true;
    motorDirFWD = !motorDirFWD;

    digitalWrite(dirPin,motorDirFWD ? HIGH : LOW);
  }

  if (digitalRead(btnDirPin) == LOW & btnDirPress == true) {
    // direction button released
    btnDirPress = false;
  }

  if (digitalRead(btnStepPin) == HIGH & btnStepPress == false) {
    // step button pressed
    btnStepPress = true;
  }

  if (digitalRead(btnStepPin) == LOW & btnStepPress == true) {
    // step button released
    btnStepPress = false;
  }
}

void motorStep() {
  digitalWrite(stepPin,HIGH);
  delay(1);
  digitalWrite(stepPin,LOW);
}

void loop() {
  checkButtons();

  if (btnStepPress) motorStep();    
  delay(1);
}

 

Final Thoughts

I could have added more features to the circuit, as some DIP Switches to set the micro-stepping configuration. Also, to make the process of measuring the current sent to the motor, I could have added some jumpers, so I didn’t need to wire one of the motor wires to the multimeter test leads. But I wanted the circuit to be very simple for motor testing porposes, driver adjustments. Also, I would need a bigger board, which I didn’t have so, I packed only the features I really needed.

 

To See More

 

Author

Fábio Lutz

Bachelor degree in Computer Science by UFRGS / Brazil; 18 years of professional experience on software development (Delphi/Pascal, Java, PHP, Javascript, ...
Sobre o Autor

 

Become our Patron

If you like our content, please consider to become our patron and donate us any amount you like to help us continue our projects and maintain our website.

DIYers Patreon URL: https://www.patreon.com/diyerscombr

Leave a Reply

Your email address will not be published. Required fields are marked *