Whether we care to admit it or not, motors can be found all over in our everyday lives; they just tend to be hidden. Motors are present in cars, printers, computers, washing machines, electric razors, and much more.
However, there are a number of people (which until recently included myself) that would be uncertain of how to make a motor run if they were handed one. So, let's learn something today. Let's learn how to use a stepper motor!
Step 1: Materials That I'm Using
In order to demonstrate how to use the stepper motor (a hybrid stepper motor), there are a few things that I will end up using.
- The stepper motor itself, a 24BYJ48A
- A Darlington Transistor Array, ULN2803A
- a microcontroller, Digilent'schipKIT uC32
- and a breadboard and breadboard wires
Both the stepper motor and the Darlington Transistor Array are available in the chipKIT Starter Kit.
Step 2: Stepper Motor Theory
Stepper motors are part of a class of motors known as brushless motors; these motors have a shaft but it does not physically touch anything in order to rotate. Rather, stepper motors work by utilizing electromagnets that are concentrically located around the shaft.
The idea behind electromagnets is that when a voltage of any kind is applied to a coil surrounding the piece of "soft" metal, that metal becomes magnetized until the current stops flowing through the coil. The central shaft rotates as the coils surrounding the electromagnets are brought to various voltage states. These voltage states create a magnetic polarity between the shaft and the electromagnet, causing the teeth of the shaft to line up with the teeth of the electromagnet. The motor can then be induced to spin by having the electromagnets appropriately change their polarity in a sequential fashion.
Step 3: Types of Stepper Motors
There are three main types of stepper motors that exist: variable inductance motors, permanent magnet motors, and hybrid motors. Variable inductance motors only use the generated magnetic field to make the central shaft rotate and line up with the energized electromagnets. Permanent magnet motors are similar except that the central shaft is polarized to have magnetic north and south pole which will appropriately rotate to whichever electromagnets are turned on. The difference between this and the variable inductance motor is that the permanent magnet motor's central shaft does not have multiple "teeth"; just a north and south pole.
The hybrid motor, as you likely expect, is a combination of the two. Its magnetized central shaft has two sets of teeth for the two magnetic poles which then line up with the teeth along the electromagnets. Because of the double set of teeth on the central shaft, the hybrid motor has the smallest available step size and so is one of the more popular types of stepper motors. It is also the same type of motor that we will be primarily focusing on. You can learn more about the different types of stepper motors and how they are constructed here.
Step 4: Unipolar Vs Bipolar Stepper Motors
There are two types of stepper motors: unipolar and bipolar stepper motors. On a fundamental level, these two types work exactly the same way; electromagnets are turned on in a sequential fashion, inducing the central motor shaft to spin.
The difference between the two types is the voltage levels. A unipolar stepper motor only operates with positive voltage, so the high and low voltages applied to the electromagnetic coils would be something like 5V and 0V. A bipolar stepper motor has two polarities, positive and negative, so its high and low voltages would be something like 2.5V and -2.5V.
Taking these electrical differences into account, the physical difference between these two styles is that the unipolar configuration requires an extra wire in the middle of each coil to allow current to flow through either to one end of the coil or the other. These two opposite directions produce the two polarities of the magnetic field, effectively mimicking the positive and negative voltage capabilities of the bipolar stepper motor.
Although both of these have a overall voltage range of 5V, the bipolar stepper motor will actually have more torque because current flows the entire coil, producing a stronger magnetic field to induce the shaft to rotate to the appropriate angle. On the other hand, unipolar stepper motors only utilize half of the coil length due to the extra wire in the middle of the coil, so less torque is available to magnetically hold the shaft in place.
Step 5: Stepper Motor Wires
Different stepper motors can have different amounts of wires, typically 4, 5, 6, or 8 wires. A 4-wire arrangement is only able to support bipolar stepper motors, since there is no central wire available.
5-wire and 6-wire arrangements can be used for both unipolar or bipolar stepper motors, depending if the center wire on each of the coils is used or not. The 5-wire configuration implies that the central wire on the two sets of coils are internally connected together.
An 8-wire arrangement, although relatively unused, is the most flexible out of all of the wire configurations as it can be run in a unipolar 5 or 6-wire arrangement, or bipolar mode with a parallel or series configuration.
This particular stepper motor that I am using has 5 wires, implying that it is to be run as a unipolar stepper motor. We learned that this 5th wire is to allow current on that particular coil to flow in two directions. But should we connect it to a 5V power line? Or to 0V ground line?
Step 6: So, Power or Ground?
Theoretically, the wire can go to either a 5V line (since our stepper motor is designed for 5V) or a ground line and then have the electromagnets energized in the appropriate fashion rotate the shaft.
Realistically, for the circuit that I am designing, I will need to attach the center tapped wire to a 5V line. The reason for this is because of the way that I am going to be sending the signals to the stepper motor telling it to "turn on" an electromagnet or not.
Digilent boards operate at 3.3V, so we would need to amplify their signals to 5V. We could use a four op-amps to bring the signals up to 5V, but I'd rather not have to mess with all of the resistors that I would need to use. Instead, I will use a Darlington Transistor Array. This IC has multiple Darlington Transistor pairs.
In a nutshell, a Darlington Transistor pair has two NPN transistors arranged in such a way so that when a high logic voltage is sent from the microcontroller, the output of the transistor pair will be a low voltage (0V), drawing in current from the 5V center tapped line.
However, if a low logic voltage is applied, the output will instead be at a high impedance state, because the NPN transistor will be acting as an "open circuit". This effectively prevents any current from flowing through the IC, and so no current will flow through the coils of the stepper motor. With no current flow in the coils, no magnetic field is created so the central shaft will not move. You can learn more about how Darlington Transistors work here.
Step 7: Stepping the Stepper Motor
There are several different ways that stepper motors are able to be driven including full step, half step, and microstepping. Each of these driving styles offer different amounts of torque and step sizes that the stepper motor can use.
A full step drive always has two of the electromagnets "turned on". To rotate the central shaft, one of the electromagnets gets turned off and the next electromagnet is turned on, causing the shaft to rotate 1/4 of a tooth (at least for hybrid stepper motors). This style of always having two electromagnets on has the most torque out of all of the styles, but the largest step size.
A half step drive alternates between having two electromagnets and just one electromagnet turned on. To rotate the central shaft, the first electromagnet is energized as the first step, then the second one is also energized while the first one is still powered for the second step. The third step turns off first electromagnet and the fourth step turns on the third electromagnet, all while the second electromagnet is still powered. This pattern, shown in a picture above, uses twice as many steps as the full step drive, allowing for half of the step size, but it also has less overall torque since there are not always two electromagnets holding the central shaft in place.
Microstepping, not surprisingly, has the smallest possible step size out of these styles. One of the most common ways to peform microstepping is to do "sine cosine microstepping". This means that the current flowing through each coil is manipulated such that a sine/cosine wave is created. The "overlap" of the waves between two coils results in a large number of substeps. The actual number of substeps is dependent on how many distinct changes in current you can provide to the coils, but microstepping will still have the smallest step sizes, and thus the most precise movement, out of all of the styles. The torque associated with this style is dependent on how much current is flowing through the coils at a particular time, but will always be less than the full step drive.
Step 8: Plans for Our Stepper Motor
I have found that this particular stepper motor, as a hybrid stepper motor, needs to sequentially turn on its four electromagnets about 200 times (as opposed to the supposedly 64 times that I have also seen) to rotate the central shaft a full 360 degrees. This means that each of the 200 or so teeth are about 1.8 degrees apart. I haven't determined an exact number since it's difficult to tell when an exact full rotation has been made as opposed to a full rotation plus a tooth.
I personally do not have a need to have a smaller step size than ~1.8 degrees and am interested in having as much torque as possible, so I will demonstrate (with code) how to use the full step drive which always has two of the electromagnets turned on at a time. You can check out how to use the half step drive in code here.
Step 9: Making the Circuit: Part 1- Power
But before we actually start running the stepper motor, lets make our circuit first.
Create a 5V rail and a ground rail on your breadboard by connecting the 5V supply on the uC32 to the breadboard's positive rail and connection one of the ground pins on the uC32 to the negative rail on the breadboard.
Then, using the pinout diagram for the ULN2803A, connect the GND pin (pin 9) to the negative rail and connect the COM pin (pin 10) to the positive rail.
Finally, since I designed the circuit to drive a unipolar stepper motor, connect the 5th wire (red in the case of this stepper motor) to the COM pin.
If you know you have a unipolar stepper motor, but are not sure which wire is the one that is in the center of the coils there is a way to figure out which wire it is. Simply measure the resistance between pairs of two wires coming out of the stepper motor. When you find a wire that consistently measures half the resistance when it is paired with all of the other wires, that wire is the one attached to the middle of the coils (hence half the resistance).
Step 10: Making the Circuit: Part 2 - Signal Lines
Naturally, we'll need some wires that will signal to our stepper motor which electromagnet should "turn on".
Connect four of the numbered (as opposed to lettered) digital pins on the uC32 to four of the inputs on the Darlington Transistor Array. I used the digital pins 34, 32, 30, and 28 on the uC32 and the inputs 1B, 2B, 3B, and 4B on the Darlington Transistor Array, respectively.
Then, connect the outputs of the transistor pairs, (1C, 2C, 3C, and 4C in this case) to an appropriate wire on the stepper motor. What I mean by that is that you want your outputs to attached in a sequential fashion to the coils of the stepper motor.
For example, you would want your first output connected to the first coil, second output for the second coil, and so on. What you do not want to have is your wires all mixed up with the first output connected to the third coil, the second output connected to the second coil, the third output connected to the 4th coil, and the last output connected to the first coil. If you do not know which wire goes to which coil (and putting all of the outputs in a row doesn't seem to be working), look for a datasheet diagram showing you which one is which, much like the diagram I found for the 28BYJ48A stepper motor.
Step 11: Performing the Full Step Drive in Code
To simulate the full step drive with our microcontroller, we will need to make that our signals that we give to the stepper motor are arranged in such a way so that the coils within the stepper motor are energized sequentially. I mentioned this bit earlier, but it's good to double check, otherwise its awkward when you have set everything up right, but nothing happens because a couple of wires are switched.
To energize a particular coil, it needs to receive a low (0V) voltage signal so that current is able to flow from the center tapped 5V line to the end of the coil at 0V. With the Darlington transistor pair, this means that we need to digitally write the pin associated with that coil to a logic high voltage. This nicely makes sense; drive a pin high to turn on an electromagnet and drive a pin low to turn off an electromagnet.
In full step drive, two electromagnets need to be turned on at a time, so we will digitally write two adjacent electromagnets (such as coils 2 and 3) high and digitally write the remaining two electromagnets low. We then need to implement a delay for enough time to allow the central shaft to start moving and get to its destination. I have found by trial and error that for my stepper motor this is about 1.6 milliseconds. After that waiting period, we can turn off one of the electromagnets (such as coil 2) and then turn on the next electromagnet (coil 4 in this case) and then wait before turning off and on the next set of coils.
You can see how this pattern might look in the above picture. The actual code that I used provided in the text file below.
Step 12: Parting Thoughts
Although they are not the fastest type of motor, stepper motors are a great way to rotate something in precise incremental steps with a decent amount of torque for their size. Feel free to check out the video to see what I mean.
If you have any questions or comments that you want to share, please feel free to do so and I will do my best to answer any question you may have.
Check out the Digilent Blog to see what other cool things the Digilent team and I are up to!