Stepper Motor or Step-by-Step Motors

Stepper motors are motors that are meant to be granularly controlled using signals in order to make the rotational movement very precise. Typically stepper motors are way more expensive than regular DC motors and that is even due to the fact that stepper motors have gears on the inside that implicitly increase the production price per unit.

There are cheap variants such as the 28BYJ-48 (as illustrated in the previous image) that are sold with a small controller board ULN2003 that drives the motor itself. Unfortunately, the 28BYJ-48 seems to match its price being fairly unreliable and, most of all, there exist a bunch of these motors that are the result of being ripoffs off each other that are designed by slightly different specifications such that it is not immediately clear what parameters must be used to control the motor. One typical error within the datasheet of a 28BYJ-48 are the number of turns necessary to complete a revolution or a $360^\circ$ turn. Roughly speaking, if precise movement is not required then the 28BYJ-48 is an affordable motor but for a more serious application where tracking the amount of movement, then a NEMA-motor is much desirable to a cheap 28BYJ-48.

The following video demonstrates the slow-movement of a stepper motor for one complete revolution. It is possible to make the motor go faster, simply by shortening the time between each rotation but given that stepper motors are only meant to provide controllable movement then there is no reason to do so.

Ideally, the rotor motor would have an external gear system that either transfers the rotational movement to a linear axle, or perhaps a gear system is added that makes the motor pull or push some item. Either way, the main spindle of the motor, albeit moving slow, will end up having an attachment that will make the movement look seamless regardless the amount of small steps that are observable in the video.

Functioning Principle

The motors have two and up to six coils, each coil providing a finer control over the rotation of a magnet in the middle that, in turn, is connected to the shaft that protrudes to the outside of the motor. It is easy to identify the coils that belong together by testing the motor pins for continuity: in case two pins are shorted then they are connected to the same coil. Pin pairs for each coil are connected to a controller that performs the actual smooth movement of the motor by energizing the coils in sequence in order to make the motor rotate.

Sometimes, depending on the build of the motor, these motors have a gear box inside that performs various rotation transformations on the inside and that is meant mostly for precision; which, incidentally, is also the selling point of a stepper-motor and justifies being more expensive than a regular DC motor.

Code Sample

There are various libraries available for Arduino that can control the 28BYJ-48 with an ULN2003 but the situation is rather disastrous given the differences in design from motor to motor leading to the proverbial the phrase "your mileage might vary" with issues ranging from imprecise movement, extremely slow movement, the Arduino locking up, etc.

The way these libraries are designed is that they require an asynchronous context where the actual movement target is set before the actual movement takes place. After the target is set, for example, a certain number of degrees using some function, the library provides different function that must be called periodically to actually make the motor move. There are functions in the API that make the motor step continuously but they are a blocking operation and blocking the ESP event loop will make the ESP crash - interestingly, as we found out, the problem seems to pertain only to the ESP8266 but the ESP32 (probably due to being a multi-core device) does not exhibit the same behavior.

The sample code below will make the stepper motor move clockwise 10000 steps.

#include <AccelStepper.h>
 
...
 
// AIn1-AIn2 and BIn1-BIn2 are 4 digital pins
AccelStepper myStepper(AccelStepper::FULL4WIRE, AIn1, AIn2, BIn1, BIn2); 
 
...
setup() {
    myStepper.setMaxSpeed(200.0);
    myStepper.setAcceleration(50.0);
    myStepper.moveTo(10000);
}
...
loop() {
    myStepper.run();
}

There are also problems with the quality of the libraries that, as per the example above, provide functions and an API but are not too specific on what the values are supposed to mean. For example, AccelStepper is perhaps the most advanced libraries out there for controlling stepper motors but it does not really have any documentation and it is not really clear what should be used, what is implicit or which parameters are required.

As an example listed above sets a speed via a call to setMaxSpeed() and an acceleration as $50\frac{m}{s}$ with setAcceleration() but if one were to leave out the acceleration and only keep the speed, the motor does not move at all, which is very confusing due to the length of a single step of a motor being constant and well-defined such that an "acceleration" would not be a required parameter of the movement of the motor. It is nice to have an option to have a sloped speed-up (acceleration) and a sloped speed-down (deceleration) but the acceleration is just not derived from the underlying physics of a stepper motor motor. There are also innuendos to bad programming practice, such as "computing square roots" that are deemed to be "too slow", which alludes to micro-optimizations that would already be meaningless considering that this is a stepper motor and not a fast-spinning DC motor.

Other libraries have trouble with basic planar geometry, for instance, using the API to ask the motor to rotate $360^\circ$ degrees, apparently makes the motor spin both ways. Internally, we assume that this is due to geometric or trigonometric calculations where $2*\pi$ radians is geometrically identical to $0$ radians such that somewhere internally to the library, the negative value (that is typically used to request a counter-clockwise rotation) is reduced and results in a clockwise movement and otherwise, depending on a different calculation it is not reduced (any other guess without looking at the code is valid).

Lastly, for the 28BYJ-48, the counter-clockwise movement does not work at all and users have empirically determined that making the motor rotate counter-clockwise requires switching pins 2 and 3 also, with varied results depending on which motor knockoff you managed to acquire.

All-in-all, regarding the 28BYJ-48 and given the complications as well as concluding that the motor does not provide precise movement regardless, then you're better off with a standard DC motor that at least would spin both ways consistently, without having to invert pins or go beyond the library API.

Troubleshooting

  • Regardless what sort of stepper motor is being used, in case the motor just vibrates without moving in any direction, whether clockwise or counter-clockwise, then the motor pins are more than likely either in the wrong order or there is some short created between the pins.

Index


iot/instrumentation/stepper_motor.txt · Last modified: 2025/06/01 16:01 by office

Wizardry and Steamworks

© 2025 Wizardry and Steamworks

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.