# How to Use a Stepper Motor

396,321

1,206

89

Published

## Introduction: How to Use a Stepper Motor

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.

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!

## Recommendations

• ### Large Motors Class

8,265 Enrolled

• ### Casting Contest

We have a be nice policy.

## Questions

0

Thank you for such a detailed illustration.

However, I am facing issue while using KR-A5MC (Stepper Driver Circuit having 5 wires for Motor). I have motor with 4 wires. I have just connected all 4 wires of motor to first 4 wires of driver circuit. Ihave used arduino, but it is not working. Can you please suggest me something in this regard?

Well written article. Stepper motors play a key role in Industrial automation. Really learned a lot on how they operate from this post. Thanks for sharing.

Show how to program a stepper to do say 100 revolutions and stop. All the examples only show 1 revolution or less.

So I am using a 4 wire but it just hums, the shaft does not move. I am confused.

Generally, when I encountered the humming it meant that the various poles on the motor are not being enabled/disabled in a sequential order or that you are not using a high enough voltage to get the motor to turn. I had a bit of trouble getting the 5V motor I used to nicely rotate with 3.3V logic.

This is really cool!!!!

Very nice Instructable. Thanks

The very best

it is very helpful to understand the stepper motor thank you

I've been having some trouble with motors. I'm completely new to this and I've been playing about with all sorts of arduino sketches from here and their example ones, but all of the motors feel like they're burning rubber on the inside without actually moving the shaft. I've tried dcs, servos and steppers, so I assume it's something I'm doing, but I can't work it out. Has any got any ideas? Any and every is welcome at this stage!

When you opened with "Whether we care to admit it or not, motors can be found all over in our everyday lives" I just imagined someone denying that motors make our phones vibrate and our garage doors open and how hilarious that would be....

It would be pretty funny, although admittedly I was one of those people (admittedly awhile ago) that was living in those dark ages thinking that motors were everywhere. I'm not sure how I would've answered onto how a phone vibrated though; that's probably part of the reason of why I am now correctly informed.

Nice instructable. I wanted to test and see if I can run several steppers I've been collecting, but I noticed the ULN2803A only stands 500mA per collector. And the stepper motor I have runs at 0.7 amps. I was wondering if you know another popular array (with 4 transistors if necessary) that can handle around 1 amp.

I guess I could also buy transistors per separate, but I prefer a less bulky chip.

7 replies

You can double the capacity to handle current by combining 2 inputs and 2 outputs. If you want to upgrade an existing project, simply solder another 2803 on top of the first chip.

To be honest, I haven't worked much with transistors until this Instructable, so I do not know what a "popular" one might be.

However, after doing a search on digikey, I did find a list of transistor arrays that had outputs of 1amp or greater. There are a bunch of individual transistors that will also work (since it's easier to get a big heat sink on them), but as you said, they are more bulkly.

Let me know if you have any more questions.

Thanks, I did a bit of research too and I bought a L293DNE and I plan to buy a L298, I still don't know too much about the L298 and H bridges, but I'll get into it eventually. Thanks!

Sounds like fun! I haven't gotten much opportunity to work with H-Bridges myself, but they don't look too bad; they're essentially a set of switches that let you complete the circuit in one direction or the other. At least, that's what I learned from this Learn Module: http://learn.digilentinc.com/Documents/325

Thanks for the info! I successfully drove a stepper motor I found in a printer, although the voltage has a strong correlation speed of the motor, causing it to fail if it's not set up according to the stepping speed. I guess this could be solved with a current limiter. I'll keep trying.

You could use a current limiter to get that rotation speed slower. I'm not sure how the printer stepper motor is set up, but if it has a bunch of individual steps like mine, you could just increase the delay time between energizing the individual coils, effectively making it rotate slower.

i want to activate my nema 17 stepper motor just only in 45 degrees angle. how can i do it? please help. thank you. :D

Good article Colvin. If you were to use a stepper motor as a generator which would be better a unipolar or bipolar ? Is there any special requirements to produce good results. I was thinking of a simple kids project.