Working Principle and Driving Methods of Stepper Motors

Working Principle and Driving Methods of Stepper Motors

What is a Stepper Motor?
A stepper motor is a type of motor that converts electrical pulse signals into corresponding angular displacement or linear displacement.
For a stepper motor, each input pulse signal causes the rotor to rotate by a certain angle or take a step forward. The output angular displacement or linear displacement is proportional to the number of input pulses, and the rotational speed is proportional to the pulse frequency. Therefore, stepper motors are also called pulse motors.
Let’s take a look at a simple bipolar motor:

Working Principle and Driving Methods of Stepper Motors

Figure 1. Bipolar Motor
Bipolar stepper motors have four wires and two coils. To make it rotate, current needs to be sent through the coils. Each wire needs to be driven high and low. Below is how to drive current to make the stepper motor rotate.
Working Principle and Driving Methods of Stepper Motors
Figure 2. Bipolar Stepper Motor
To understand why this is done, consider a simple stepper motor with only four steps. In the first stage, it aligns the magnet with the first coil. The next step rotates the magnet by 90 degrees. Sending current in reverse through the first coil will reverse the polarity of the magnet. The opposite coil is connected, but generates an opposite magnetic field relative to the center magnet.
Working Principle and Driving Methods of Stepper Motors
Figure 3. Stepper Motor Rotating in Four Steps
Of course, most stepper motors have more than 4 steps. Your standard stepper motor rotates 200 steps per revolution. This way of rotating the motor is called full stepping. Once you complete full stepping, half stepping is very simple. You can send current through both coils at the same time, which will double the resolution.
Stepper motor drivers can also use microstepping, which adjusts the current through the coils. A typical motor controller can perform 16 microsteps for each full step. Some chips are responsible for modulating the current, but older chips require “tuning” for the stepper motors they drive. Microstepping further divides the entire step into 256 microsteps, turning a typical 200-step motor into a 51200-step motor! Microstepping also reduces motor noise, making it run smoother and more efficiently.
Working Principle and Driving Methods of Stepper Motors
Figure 4. Half Step Between Full Steps 1 and 2
How to Control Current in the Coils
The most common setup to control the current through the windings is to use what’s called an H-bridge. It is a set of four transistors that can pull each wire high or low. You can also use MOSFETs instead of transistors, but the wiring will be a bit different. The diagram shows how to send current in either direction through the H-bridge. You just need to turn on the transistors in the path.
Working Principle and Driving Methods of Stepper Motors
Figure 5. Current Direction in the Coils
You must ensure that the two transistors on the same side do not conduct at the same time. This would short the circuit by providing a low-resistance path from power to ground. You should also note that transistors may take some time to switch from on to off. Rapidly switching current through the coils is not recommended unless you know what you’re doing.
Working Principle and Driving Methods of Stepper Motors
Figure 6. You Must Ensure That the Two Transistors on the Same Side Do Not Conduct at the Same Time
This is still not the full picture. The rotating motor will generate voltage. To protect the transistors, it is best to place diodes.
Working Principle and Driving Methods of Stepper Motors
Figure 7. Diodes for Protecting Transistors
This will prevent the motor from generating high voltage, which could damage the transistors or even the driver. If the voltage driving the stepper motor is higher than the voltage output from the MCU, another transistor will need to be added to control the PNP transistor.
Working Principle and Driving Methods of Stepper Motors
Figure 8. Using Another Transistor to Control PNP Transistor
When you turn on the additional NPN transistor, it will allow current to flow from the base of the PNP transistor (pin 1), thus turning it on. Now all that’s needed is a current limiting resistor on the bases of all the NPN transistors.
Working Principle and Driving Methods of Stepper Motors
Figure 9. Current Limiting Resistor on the Base of NPN Transistor
That’s it! The H-bridge will control the current through one of the windings. Since there are two windings, we need to double this circuit.
Working Principle and Driving Methods of Stepper Motors
Figure 10. Dual H-Bridge Driving Stepper Motor
Now you can calculate the components needed. Using a dual H-bridge is not the only way to drive a stepper motor. You can also purchase stepper motor drivers that will have a built-in dual H-bridge (although drivers usually use MOSFETs and other tricks). If you want to reduce the BOM count (sometimes gaining more features), I suggest you look at stepper motor drivers. You need to check the datasheet to understand the features provided by the chip. Some chips only provide transistors and diodes, while others fully control the current through the coils.

Microstepping

Working Principle and Driving Methods of Stepper Motors
Figure 11. Pulse Width Modulation Signal
Microstepping includes sending pulse width modulation signals to the transistors. This is a simple way to control the current in the motor coils. Pre-selected PWM values are placed in a sine lookup table. Typically, a PWM frequency of 20-40kHz is chosen. Any frequency below 20kHz can be heard by the human ear. The frequency is kept below 40kHz to improve efficiency and reduce power loss in the transistors. When the PWM signal is high, current flows through the transistors. When the PWM signal is low, current flows through the diodes. This is a very rough implementation of microstepping, but it gives a general idea of how it works. Motor drivers using MOSFETs can control the speed of current decay or attenuation in the motor. The current waveform of the driver looks like this:
Working Principle and Driving Methods of Stepper Motors
Figure 12. Current Through MOSFET Motor Driver
It is necessary to manually optimize the fast decay and slow decay cycles for the motor being driven. Some new chips automatically adjust the decay cycle based on the current they sense, but older chips may require optimization (or tuning).
Stepper Motor Driving Example

Example: Using Control BoardArduino Mega to Control Stepper Motor DriverTMC5130-EVAL to Drive the Stepper Motor.

Working Principle and Driving Methods of Stepper Motors

Figure 13. Using Arduino Mega to Control Stepper Motor Driver TMC5130-EVAL

Controller: Arduino Mega 2560 is a microcontroller board based on the ATmega2560. It has 54 digital input/output pins (15 of which can be used as PWM outputs), 16 analog inputs, 4 UARTs (hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; just connect it to a computer with a USB cable or power it with an AC to DC adapter or battery to get started.

Stepper Motor Driver Board: TMC5130 is a fully integrated stepper motor driver and controller system that allows remote control of stepper motors from any microcontroller. It implements all real-time critical tasks in hardware. Once configured, the motor can be driven by giving a target position, commanding a return sequence, or specifying a target speed. Benefits of using TMC5130 include: ease of use, motor precision with 256 microsteps, low motor noise (no noise hidden chopper), sensorless stall detection (stallGuard2), no step loss, dcStep and coolStep, high efficiency with UART or SPI control interface, high voltage range, small form factor, and low component count.

1. Ensure Arduino Mega and TMC5130-EVAL Have Voltage Matching

If the Arduino is a 5V control board, then a resistor on the TMC5130-EVAL must be relocated from position R3 to R8. This sets the logic level of the TMC5130 to +5V.

2. Wiring
Working Principle and Driving Methods of Stepper Motors
Figure 14. TMC5130 Connected to Arduino Mega 2560 (Image Source: Trinamic)

The cable colors in the above image

+5V – > Red

GND – > Blue

SDO – > Yellow

SDI – > Orange

SCK – > White

CSN – > Gray

DRV_ENN – > Black

CLK16 – > Green

Working Principle and Driving Methods of Stepper Motors
Figure 15. Pin Corresponding Signals (Image Source: Trinamic)
Pin corresponding signals. The configuration is recorded in the comments section of the Arduino code.
ARDUINO CODE
The Arduino code below does not require any additional libraries. The SPI library is included with the Arduino IDE. This program initializes the TMC5130 and performs a simple move-to-position cycle. It will rotate a 200 full-step motor 10 turns in one direction and 10 turns in the other direction, depending on the wiring of the stepper motor. Please refer to the TMC5130 datasheet or TMCL IDE as a reference for different registers.

Scroll up to view the complete code

#include <SPI.h>

#include “TMC5130_registers.h”

/* The trinamic TMC5130 motor controller and driver operates through an

* SPI interface. Each datagram is sent to the device as an address byte

* followed by 4 data bytes. This is 40 bits (8 bit address and 32 bit word).

* Each register is specified by a one byte (MSB) address: 0 for read, 1 for

* write. The MSB is transmitted first on the rising edge of SCK.

*

* Arduino Pins Eval Board Pins

* 51 MOSI 32 SPI1_SDI

* 50 MISO 33 SPI1_SDO

* 52 SCK 31 SPI1_SCK

* 25 CS 30 SPI1_CSN

* 17 DIO 8 DIO0 (DRV_ENN)

* 11 DIO 23 CLK16

* GND 2 GND

* +5V 5 +5V

*/

int chipCS = 25;

const byte CLOCKOUT = 11;

// const byte CLOCKOUT = 9; –> Uncomment for UNO, Duemilanove, etc…

int enable = 17;

void setup() {

// put your setup code here, to run once:

pinMode(chipCS,OUTPUT);

pinMode(CLOCKOUT,OUTPUT);

pinMode(enable, OUTPUT);

digitalWrite(chipCS,HIGH);

digitalWrite(enable,LOW);

//set up Timer1

TCCR1A = bit (COM1A0); //toggle OC1A on Compare Match

TCCR1B = bit (WGM12) | bit (CS10); //CTC, no prescaling

OCR1A = 0; //output every cycle

SPI.setBitOrder(MSBFIRST);

SPI.setClockDivider(SPI_CLOCK_DIV8);

SPI.setDataMode(SPI_MODE3);

SPI.begin();

Serial.begin(9600);

sendData(0x80,0x00000000); //GCONF

sendData(0xEC,0x000101D5); //CHOPCONF: TOFF=5, HSTRT=5, HEND=3, TBL=2, CHM=0 (spreadcycle)

sendData(0x90,0x00070603); //IHOLD_IRUN: IHOLD=3, IRUN=10 (max.current), IHOLDDELAY=6

sendData(0x91,0x0000000A); //TPOWERDOWN=10

sendData(0xF0,0x00000000); // PWMCONF

//sendData(0xF0,0x000401C8); //PWM_CONF: AUTO=1, 2/1024 Fclk, Switch amp limit=200, grad=1

sendData(0xA4,0x000003E8); //A1=1000

sendData(0xA5,0x000186A0); //V1=100000

sendData(0xA6,0x0000C350); //AMAX=50000

sendData(0xA7,0x000186A0); //VMAX=100000

sendData(0xAA,0x00000578); //D1=1400

sendData(0xAB,0x0000000A); //VSTOP=10

sendData(0xA0,0x00000000); //RAMPMODE=0

sendData(0xA1,0x00000000); //XACTUAL=0

sendData(0xAD,0x00000000); //XTARGET=0

}

void loop()

{

// put your main code here, to run repeatedly:

sendData(0xAD,0x0007D000); //XTARGET=512000 | 10 revolutions with micro step = 256

delay(20000);

sendData(0x21,0x00000000);

sendData(0xAD,0x00000000); //XTARGET=0

delay(20000);

sendData(0x21,0x00000000);

}

void sendData(unsigned long address, unsigned long datagram)

{

//TMC5130 takes 40 bit data: 8 address and 32 data

delay(100);

uint8_t stat;

unsigned long i_datagram;

digitalWrite(chipCS,LOW);

delayMicroseconds(10);

stat = SPI.transfer(address);

i_datagram |= SPI.transfer((datagram >> 24) & 0xff);

i_datagram <<= 8;

i_datagram |= SPI.transfer((datagram >> 16) & 0xff);

i_datagram <<= 8;

i_datagram |= SPI.transfer((datagram >> 8) & 0xff);

i_datagram <<= 8;

i_datagram |= SPI.transfer((datagram) & 0xff);

digitalWrite(chipCS,HIGH);

Serial.print(“Received: “);

PrintHex40(stat, i_datagram);

Serial.print(“\n”);

Serial.print(” from register: “);

Serial.println(address,HEX);

}

void PrintHex40(uint8_t stat, uint32_t data) // prints 40-bit data in hex with leading zeroes

{

char tmp[16];

uint16_t LSB = data & 0xffff;

uint16_t MSB = data >> 16;

sprintf(tmp, “0x%.2X%.4X%.4X”, stat, MSB, LSB);

Serial.print(tmp);

}

Code Source: Trinamic Blog (For questions about the code, you can learn more on the Trinamic blog)

Summary

I hope this article can help you understand the working principle of stepper motors and how to drive them. The technology for driving stepper motors is already mature, and effectively utilizing existing hardware and code can yield significant results.

Leave a Comment