Jeremy S. Cook and Zach Wendt are engineers who enjoy hands-on projects and sharing how technology can impact project design. Jeremy writes for a variety of technical publications. Zach works for Arrow Electronics, a major supplier of Arduino products.
If you have a project that you’d like to take into the real world and actuate physical devices, this normally means incorporating some sort of motor. While many motors run on DC or AC power (like what drives your ceiling fan), stepper motors are a great option if you need precise angular control. Steppers work by controlling electromagnetic coils to make the shaft rotate in a series of steps.
Naturally, stepper motors vary in size and complexity, as well as cost. One of the cheapest and most common ways to get your hands dirty is a 28BYJ-48 stepper motor, a ULN2003A driver board, and an Arduino Nano clone. You’ll need a power source, hookup wire, a mini-USB programming cable, breadboard, and—optionally—a screw terminal board, as pictured in the examples here.
Photo: Jeremy S. Cook
Once you have the needed parts, setup of the stepper motor is fairly straightforward. First, hook wires from four output pins of your Arduino to the driver board. While any set of digital I/O pins can be used, the code linked below runs Arduino pins 2, 3, 4, and 5 to driver inputs 1,2,3, and 4, respectively.
Attach a 5V power supply to the driver, and make its ground common with your Arduino. If you’re a bit of a rule breaker, you may be able to get away with using the Arduino’s +5V and Ground pins to power the servo. This is not recommended though, as it can spike the power beyond what the Arduino is intended to supply.
With this done, go into your Arduino Program and load up the AccelStepper library (available here) or via a search of the Arduino IDE’s library manager. You may notice that Arduino already has a stepper library built in, but AccelStepper produces better functionality.
Now, load the code found here onto your Arduino, and watch your servo bounce back and forth from one position to another. You can then modify parameters to change the behavior, or even add it to your own code.
What’s Going On?
While getting a stepper running is exciting, it amounts to following a cookbook (Betty Crocker electrical engineering, as you might say). You understand the inputs, the results are “delicious,” but you have no idea how you got the proverbial dough to rise.
Explaining things further, inside of the servo are two coils that produce a magnetic field. One is in the top half of the motor housing, and the other is on the bottom. When current is passed through these coils, each exhibits an electromagnetic force through a series of metallic teeth inside the motor. Depending on the direction that the current travels, the resulting force can either be in the north or south direction.
The rotor—the thing that actually spins—is set up to exhibit the property of four magnetic poles. When each coil is energized in the correct series, it causes the rotor to step through each position, based on the magnet’s pole alignment. When each coil step is energized individually, this is known as a wave drive, since steps 1,2,3, and 4 repeat over and over like a wave, alternatively attracting the proper north and south poles of the rotor.
Another driving method is known as full stepping, which energizes two magnets at the same time to give the motor a much higher torque rating. Half stepping can also be used, and alternates between energizing one and two coils at a time, giving a torque level in between full and wave methods, but allowing for twice the step resolution.
That’s a very quick introduction — this video does a great job of explaining further what’s going on. It should also be noted that other types of servos have different coil and magnet configurations, but all of them use some variation on this principle.
The Hard Way
With the explanation above, it would seem that one could manually cycle through each coil and turn the motor by hand, so to speak. While theoretically possible to physically connect the wires in the proper sequence over and over, you could also code your own driver program. For a wave drive, this would energize coils 1,2,3, and 4 in sequence via pins 2,3,4, and 5, if hooked up on an Arduino, as noted earlier. The stepper driver will take care of amplifying the signal from your Arduino via its transistor array.
If you’re a proficient programmer, coding a wave drive will be a trivial matter, though full step and half stepping might take a little more thought. If, however, you need a little help with the context, this GitHub repository has several examples of manually driving the motor with an Arduino board. How it works is thoroughly explained here.
While other motor types and models are available, stepper motors like the 28BYJ-48 present a great option that can help you get started with this type of automation at a low cost—even if you need more than one!