A WiringPi Stepper Motor Control Module
by Mike Green
wpi-stepper is a flexible control class for stepper motors, written using the excellent WiringPi-Node library. If you find yourself in need of such a module, give it a try.
wpi-stepper for a few reasons:
- My wife and I keep chickens (20 laying hens and a rotating cast of broilers, if you're curious).
- We built an automatic sliding door for our chicken coop that uses a stepper motor.
- All existing stepper control modules I could find in JS either don't work, can't be installed in current versions of Node, or require you to wire your motor to the driver in a way that makes no rational sense.
wpi-stepper allows you to wire your motor and controller however you prefer, and you can also program your own pin activation sequences by simply feeding some arrays of 1's and 0's to the
Stepper class. I can see this being useful not only for driving stepper motors, but also for controlling anything that requires a repeating sequence of activation and deactivation.
Note: This library uses WiringPi to toggle GPIO pins, which means that it must be run as root in order to work. If you're not running as root, your script will bail when WiringPi tries and fails to initialize.
npm install --save wpi-stepper
Pre-compiled to ES5 with Babel and the
var Stepper = Stepper;
The raw ES6 source, if you're transpiling it yourself:
const pins =17 // A+16 // A-13 // B+12 // B-;const motor = pins steps: 200 ;motorspeed = 20; // 20 RPM// Move the motor forward 800 steps (4 rotations), logging to console when done:motor;
Stepper is an
EventEmitter, so you can subscribe to various events emitted by the class throughout its life cycle:
motor;motor;motor;motor;motor;// => "Starting to move!"// => "Stopping that. Doing this instead!"// => "Starting to move!"// (a few seconds later...)// => "I'm all finished!"
Custom Activation Modes
wpi-stepper comes configured for a 4-wire stepper motor out of the box, and thus far that's all I've tested it with. However, you can easily use the
Stepper class to drive other types of motors with different numbers of wires by passing it a custom
mode option when you initialize an instance.
Activation modes are arrays of arrays, whose inner members are either
0 and correspond to each pin, in the order you first specified them.
wpi-stepper exports two available activation modes out of the box, and they look like this:
DUAL (this is the default mode)
Use this activation mode if you're driving a bipolar, 4-wire stepper motor:
;const pins = 17 16 13 12 ;const mode = MODESDUAL;/*[[ 1, 0, 0, 1 ], // Pin states: (17: on, 16: off, 13: off, 12: on)[ 0, 1, 0, 1 ],[ 0, 1, 1, 0 ],[ 1, 0, 1, 0 ]]*/const motor = pins mode ;
SINGLE (for unipolar motors)
;const pins = 17 16 13 12 ;const mode = MODESSINGLE;/*[[ 1, 0, 0, 0 ], // Pin states: (17: on, 16: off, 13: off, 12: off)[ 0, 1, 0, 0 ],[ 0, 0, 1, 0 ],[ 0, 0, 0, 1 ]]*/const motor = pins mode ;
As the motor turns, the
Stepper class will step through these activation modes and apply the appropriate states to the pins you register. For both of the included modes, the pattern repeats every 4 steps. This can and should vary depending on how many wires your motor has.
See the API documentation.
wpi-stepper is released under the terms of the MIT License.