Skip to main content

Microcontroller Interfaces: Stepper Motors

Typical stepper motor formatsTypical stepper motor formats. Left: A NEMA 17, high-precision, hybrid bipolar 2-phase motor. This one has a full-step resolution of 200 steps per revolution, corresponding to a step rotation angle of 1.8°. Right: A cheap stacked-coil, permanent-magnet unipolar 4-phase motor with a 64:1 gearbox. The gearbox gives it an output shaft resolution of 2048 steps per revolution (!).

What is a stepper motor?

Like a conventional PMDC motor, brushed or brushless, the stepper motor converts electrical energy to the rotational motion of a shaft. Unlike those other types, the stepper rotates in precise steps and can hold position. The latter feature makes them ideal for driving robotic arm joints, CNC milling machines, and computer printers – both conventional and 3D. In terms of position control, it seems as if a stepper motor offers an alternative to the widely used ‘servo’. Things are never that simple….

Steppers versus Servos

At this stage I should clarify what is meant by the term ‘servo’: I take it to mean ‘servomechanism’, which consists of a servomotor with a gearbox, and an angular position feedback sensor plus associated electronics. A servomotor can be a cheap brushed PMDC type as in an RC servo, a precision brushless DC motor, or even an AC synchronous motor in a large industrial application. For the purposes of comparison, I’ll consider a small ‘domestic’ stepper against an RC servo.

There are plenty of pros and cons to weigh up when considering which technology to use in a particular project. Here are a few, starting with the ability to rotate the output shaft to a particular angle, and hold it there.

Positional Control

Servo: requires closed-loop position-feedback control electronics to drive the motor. The popular and cheap RC Servos contain all these components ready-packaged and are widely available off the shelf. Of course, their performance is not that great, and they only work over half a revolution (180°) of the output shaft. But they do work to an absolute position. Just give them a 50Hz PWM signal with a ‘Mark’ pulse length between 1ms and 2ms and the shaft will move to the corresponding angle. So, for example, 1.5ms takes it to the mid-point (90°) of travel.

Stepper: can make do with some very simple drive electronics and requires no feedback control, although at least four ‘signal’ connections are needed against the servo’s one. Despite the extra wires, the stepper cannot take you to an absolute position; only relative movement is possible.

Holding Position

Servo: can get to a position quickly, but the nature of the feedback control system means that it’s not too good holding it, if there is a significant load torque. The difference or error between the desired and the actual position, drives the motor. The motor stops when the error is zero, but any load torque on the servo arm will then try to move it back. If the torque is sufficiently large to move the arm and force the feedback sensor to generate a non-zero error signal, the motor will briefly start up again before cutting out when the arm has been returned to its original position. The loud ‘buzzing’ noise from the servo when this cycle constantly repeats is not only annoying, it also warns of mechanical jitter and imminent failure unless the load is reduced. Remember, the servomechanism’s motor is just a tiny brushed PMDC type, which takes high currents when ‘stalled’, and may burn out. Or strip the nylon gears. Servos required to move a heavy load are best used with the output shaft vertical so that gravity plays no part in their operation.

Stepper: On the other hand, steppers are designed to hold position. Turn the shaft of an unpowered stepper motor by hand and it will feel rough. Turning it slowly will reveal that the rotor is in fact moving evenly in steps between ‘detent’ positions. The permanent magnet rotor is rotated by the interaction its magnetic field with that of the electromagnets (coils) when the latter are energised in sequence. Freeze the sequencing, and the rotor is held at a position depending on the pattern of coils left energised. What you have now is a ‘stalled’ PMDC motor with heavy current flowing through those energised coils providing a powerful ‘holding’ torque resisting any external load applied to the shaft. That results in a standard feature of all types of stepper motor: maximum current flow when nothing is moving. Steppers and their electronic drivers get hot when the rotor is stationary.

Modes of Operation

Bipolar

A stepper motor will have a minimum of four connections: one to each end of two stator windings, A and B, (or ‘phases’) arranged in four coils at 90° to each other around a permanent magnet rotor. This is described as a 2-phase 4-wire motor (Fig.1). Notice that the coils in a phase are physically opposite each other and are wound to generate opposite magnetic polarities when energised. To reverse the polarity from say N-S to S-N, just reverse the direction of the current through the phase. This is known as bipolar operation. Driving the rotor around is accomplished by turning phase currents on and off, or by reversing their direction, in a strict sequence.

Wave-Mode (Fig.1a)

Only one phase is switched on at a time: A → B → A\ → B\ → A, where A\ and B\ signify current reversal. This sequence uses less current, but provides less torque, both dynamic and holding.

Full-Step Mode (Fig.1b)

This time both phases are used simultaneously, taking more current, but delivering more torque. Notice how two adjacent North stator poles hold the rotor’s South pole aligned half-way between them. The sequence becomes: AB → BA\ → A\B\ → AB\ → AB.

Half-Step Mode (Fig.1c)

Notionally, this simple motor will only take four steps to complete one revolution, but by combining sequences [a] and [b], we can double the resolution to eight. Unsurprisingly, we now have an eight-step sequence: A → AB → B → BA\ → A\ → A\B\ → B\ → AB\ → A. Torque is uneven with alternate steps having one or two phases active at the same time.

Unipolar

The bipolar motors need driver electronics that can reverse the current flow through each phase when required. By adding an extra connection between the coils of each phase and taking it to the positive supply rail, we can eliminate the need for a current reversing circuit: four simple power transistor drivers will suffice (Fig.2).

Wave-Mode (Fig.2a)

As before, only one phase is switched on at a time. Unfortunately, the simplified driver means that only one coil in each phase is on with each step: A → B → A\ → B\ → A.  With only one coil active, torque is lower.

Full-Step Mode (Fig.2b)

Like the bipolar case, but with only one coil per phase active: AB → BA\ → A\B\ → AB\ → AB.

Half-Step Mode (Fig.2c)

Once again, combining sequences [a] and [b] doubles the resolution to eight with the same basic eight-step sequence: A → AB → B → BA\ → A\ → A\B\ → B\ → AB\ → A. Torque is still uneven.

Practical Motors

The motor design shown in Figs. 1 and 2 is not a practical proposition. For a start, it only allows a maximum of eight steps per revolution corresponding to a step-angle of 45°. A high-precision application is likely to need sub-degree resolution, both for accurate positioning and smooth vibration-free movement.

NEMA-17 Hybrid Stepper Motor

The motor on the left in the heading picture should look familiar: it’s widely used for precision movement in robotics, computer printers and more recently, 3D printers. It’s relatively cheap and so widely available it seems amazing that a single motor type could cover so many applications. NEMA- 17 actually refers to a standardized frame size established by the US National Electrical Manufacturers Association. So, all that NEMA-17 tells you about a particular motor is that it has a 1.7 x 1.7in square mounting faceplate: it says nothing about electrical or mechanical characteristics. The device shown here has the part number 42HS34-1334 and features a current/phase of 1.33A, a holding torque of 2.6kg.cm and step angle of 1.8° (Fig.3).

Taking a look at the NEMA-17 motor with one end-cap removed reveals that its stator looks similar in format to the theoretical model, apart from having eight coils instead of four. The rotor is of a completely different design. The permanent magnet is now mounted axially, and the rotor split into North and South discs. These discs are toothed and look like gear wheels which appear to line-up, but not mesh with, similar teeth on the stator cores. The toothed rotors account for the this type of motor being called a hybrid, because it combines the features of a permanent magnet stepper with those of a Reluctance Motor. The gap between rotor and stator teeth must be very small and precise, hence the necessity for precise overall mechanical construction. The reluctance effect significantly increases the number of steps per revolution according to the formula:

Steps/Rev = 2 x (no. of rotor teeth) x (no. of phases)

The motor in Fig.3 has a 50-tooth rotor and two phases giving a steps per revolution of 200 and a step angle of 1.8°. Now let’s look at the interface for a Raspberry Pi Pico microcontroller (212-2162) shown in Fig.4.

Having a four-wire bipolar format, this motor requires an interface circuit that can reverse the current flow through either or both phases in response to digital I/O signals from the Pico. Such a circuit is the ubiquitous ‘H-Bridge’ driver, frequently used to control the rotational direction of brushed PMDC motors. For phase currents in the region of 1A, the DIP-packaged STM L293D device (714-0622) is ideal as it contains two driver circuits. The H-bridge circuits will enable the Pico software to control the motor stepping-speed, direction and selection of stepping mode - Wave, Full and Half - as described above.

Advanced Features

You might have noticed that the interface in my test rig of Fig.4 does not feature the L293D. Instead, I’ve used a small module - the red PCB - based on a specialist stepper driver chip, the Allegro A4998. Apart from being inexpensive and widely available, this device unlocks a number of software simplifications and performance improvements:

  1. Constant-current drive. A stepper motor’s torque drops with increasing rotational speed assuming a constant-voltage from the basic H-bridge circuit. This effect, caused by falling phase current can be partially mitigated by the signal from a current feedback sensor (a small-value resistor) in each phase-driver circuit which, via further circuitry leads to the supply voltage being increased.
  2. Microstepping. Being able to control the phase current in this way enables a very useful feature: a further reduction in step size. Half-stepping is achieved by turning on adjacent coils, so the rotor ends up half-way between. The phase currents are equal yielding an equal ‘pull’ from each coil. But what if they could be made unequal so that the rotor was held nearer to one coil than the other? As you might expect, this opens up the prospect of ‘quarter-stepping’. In fact, the A4998 chip can be set up to provide 1/4, 1/8, even 1/16-stepping. In other words, this motor can be operated at 3200 steps per revolution.
  3. Reduced load on the MCU. All the sequencing of coil currents is handled by the A4998, so the MCU driver software just supplies a step signal, a direction signal and optionally three GPIO lines to select the step mode.

I used a module ‘from the Internet’, but if you’re using an MCU board with MikroBUS sockets, then the Stepper 2 Click module (136-0748) performs the same function. The ‘cheap’ A4998 module is plugged into a breakout board (also widely available) which allows the three step-mode select signals to be set by a DIP-switch. The hardware setup in Fig.4 includes a potentiometer which provides position information to the demo software in the form of an analogue voltage digitised by the Pico convertor, ADC0. Turning the knob should produce a corresponding movement of the stepper shaft, in a similar way to the servo demo.

An aside on the Pico ADC

Note the Pico pins to which the potentiometer is connected. These are different to those used in the servo demo. The breadboard setup for the latter has the ends of the pot connected between one of the Gnd pins and the 3V3(OUT), and it worked just fine. The same connection for the stepper demo resulted in very jittery motor operation – without even touching the pot. The reason was excessive noise on the two pins. But why was the servo unaffected? Because its internal feedback control system features a ‘deadband’ which means the servomotor will only respond to a change in the feedback signal over a certain threshold. It’s to stop noise on that feedback signal causing the motor to turn on and off randomly. The stepper demo initially featured no such deadband arrangement and the noise was causing rather more than just a one-step jitter. Reconnecting the pot to use analogue ground (AGND) and ADC_VREF reduced the random movement considerably, but still left that one-step jitter when the motor was supposed to be stationary. I then introduced a software deadband to ignore the last bit of noise. The Pico ADC is supposed to have a 12-bit resolution, but it’s generally accepted that the internal reference voltage (ADC_VREF) is not stable enough leaving you with only nine usable bits. For high precision work an external reference is needed.  The demo program for the Pico created with the Arduino IDE can be downloaded from the Downloads section below.

28BYJ-48 Permanent Magnet Stepper Motor

Now let’s move on from the precision, NEMA-format motors to the other end of the scale: small, cheap devices for applications where something needs to be moved from one position to another, but with no need for precision. For example: the air-flow directing slats on a ceiling mounted air-conditioning unit. This little unipolar 5-wire unit features a radial permanent magnet rotor placed inside two ‘stacked’ coils just like the moving core in a solenoid. In order to get rotary motion, the magnetic field from the coils is turned through 90° using soft iron plates on the sides of each coil. ‘Fingers’ from each plate line the coils surrounding the rotor. The construction is rather basic, and this limits the performance to 32 steps/rev. A 64:1 ratio gearbox increases this to 2048 on the output shaft. Contrast the beautifully machined aluminium blocks of the NEMA format unit, with the bits of bent metal roughly staked together of the 28BYJ-48! Its unsuitability for applications requiring precise step movements and high-speed operation is obvious. A typical driver design is shown in Fig.5.

This represents a minimum-hardware / maximum-software solution. Being a unipolar motor, it doesn’t need the complexity of H-bridge drivers to reverse current flow; just four single-transistor switches, one for each phase. Another very popular device is used, the Texas Instruments ULN2003A (436-8451) , a chip featuring seven diode-protected 500mA Darlington transistor drivers. All that does is allow the low-current GPIO pins of the Pico to drive the higher voltage and high-current inputs of the motor. All signal sequencing is performed by the MCU code. Once again, if you’re using an MCU board with MikroBUS sockets, then the Stepper 3 Click module (136-0777) performs the same function. A demo program created with the Arduino IDE can be downloaded from the Downloads section below.

Which to choose?

For small applications requiring high-precision position and motion control, there is really only one answer: the NEMA-format hybrid stepper motors. They are available from many different manufacturers, and inexpensive because of the large production quantities involved. The main concern for the designer is to include a large enough power supply able to handle the total holding current of a multi-motor project. It may be necessary to use forced cooling (fans) for the motors and coil driver circuits in extreme conditions.

Articles in the series

This short series of articles is the basics of interfacing peripheral components to a microcontroller (MCU).

  • Microcontroller Interfaces: LEDs and Lamps - In this first instalment, the simplest of display devices are considered: the single LED and the filament lamp.
  • Microcontroller Interfaces: Switches and Buttons - In this second instalment, the simplest of input devices are considered: the mechanical switch and the pushbutton.
  • Microcontroller Interfaces: Electromagnets and Solenoids - In this third instalment, I’ll show how special care is needed when designing an interface for an electromagnetic actuator.
  • Microcontroller Interfaces: PMDC Motors - In this fourth instalment, we look at Permanent Magnet DC Motors, they are cheap, have linear characteristics and are very easy to control using the PWM units built-in to most modern microcontrollers. Interfaces can be as simple as a single transistor depending on the application.
  • Microcontroller Interfaces: RC Servos - The RC Servo or Hobby Servo has been used to move the control surfaces of Radio-Control (RC) model aircraft for many years. It’s very popular with builders of both small humanoid robots and when converted for continuous rotation, wheeled robots.
  • Microcontroller Interfaces: Stepper Motors (this article) - The stepper or stepping motor has a similar construction to a BLDC motor – a permanent magnet rotor surrounded by a stator of electromagnets. It is driven electrically in a similar way, but rotates in discrete steps, and can stop and hold position.

If you're stuck for something to do, follow my posts on Twitter/X. I link to interesting articles on new electronics and related technologies, retweeting posts I spot about robots, space exploration and other issues. Click on Bill Marshall at the top of this article to see my back catalogue of DesignSpark articles.

Engineer, PhD, lecturer, freelance technical writer, blogger & tweeter interested in robots, AI, planetary explorers and all things electronic. STEM ambassador. Designed, built and programmed my first microcomputer in 1976. Still learning, still building, still coding today.

Comments