Driving Stepper Motors (28BYJ-48)

A supporting video is available for this article :

Brief Introduction to Stepper Motors
A stepper motor behaves just like a normal motor in so much that it has a rotating shaft but that’s where the similarity mostly ends. When hooked up to some sort of controlling device the shaft can be moved in small fixed amounts and hold that position with a reasonable amount of torque. It does this by having multiple coils inside the motor casing which if turned off and on in the correct order can enable the shaft to just move small precise amounts. The number of increments the shaft can perform in 1 full turn varies depending on the motor. For example 64 increments per 360° is typical of many cheap motors. In this case the angle turned per increment would be:

360/64 = 5.625°

The small increments are called steps, hence the name stepper motor. Their primary use is where you need fine and accurate control over the movement of something, such as a robot limb or roving vehicle, or the the more mundane : printers etc.

Limitations/caveats
As brilliant and useful as these devices are, they have downsides. For example they cannot typically spin as fast as their normal motor counter parts. They also typically draw much more current as well, so are often not ideal for battery powered devices.

A typical motor
As mentioned there are many different types of motor available, I will look at one of the cheapest and most readily available ones that you will pick up, the 28BYJ-48 which is used in so many different devices from 3D printers (and normal ones) to blinds and other equipment. Their almost ubiquitous presence makes them cheap to pick up. Here is a picture of one (click to enlarge).

You may notice that the shaft is not in the centre of the motor as you may expect. This is because there is a built in gear box inside that reduces the speed of the motor down 64 times (a 64:1 ratio). This allows a more finite step angle and a much higher torque (i.e. can apply a good amount of force). The pay off is a much reduced speed from an already slower moving motor. In reality the speed is generally perfectly acceptable to the applications these motors find themselves in and is thus a good exchange for the higher torque force. As mentioned it also means we have a much smaller step angle : 5.625/64 = 0.0879°. So much more precision as well.

Using with Micro-controllers
In the introduction above it was mentioned that they draw more current than typical motors and indeed it is far more than normal MCU’s can cope with. So we need additional support to help drive these motors. A typical driver chip would be the 2003(a) darlington driver array capable of providing easily enough current to run these motors. You can even purchase driver boards with this chip on and a connector that fits the above motor connector, all ready to hook up, here’s one:

You can see the matching connector for our motor on this board. On the other side there are 4 pins that need to go to four digital outputs of your MCU (we’re going to use an Arduino but would work easily with others). It is these outputs turned on and off in the correct order that will make our motor move. At the bottom there is an input for a motor supply voltage between 5 and 12V. To the right of that there are two pins joined together with a jumper. This is the motor power control, remove the jumper and no power gets to the motor, so won’t move and thus won’t draw current. This is useful when you realise that these types of motors draw current even when stationary (but also provide a holding torque that way). By default you cannot directly control motor power without additional circuitry to handle the power so usually you leave this as it is.

Software Library
To make things easier there are several libraries available for your MCU if you use the Arduino IDE as your development environment (and indeed even if you don’t but I won’t be covering them in this article).  By default the Arduino IDE already includes a library and all you need do to use it is add the following line to your code

#include <Stepper.h>

This is not necessarily the best library for all situations or motors but it’s a good place to start. One limitation is that it uses what is termed a “4 step sequence” to control the motor rather than an “8 step sequence”. For most purposes with this motor this is again no big deal. However it does mean that the step resolution is halved, i.e. instead of the 64 steps per resolution we will only get 32 steps, but because we are using a 64:1 gearing ratio this still leaves us with a very tiny step angle at the end. i.e. at 32 steps per revolution we have a step angle of 11.25°, but with the 64:1 gear ratio this still gives us an effective step angle of 0.176° or to put it another way 2048 steps per rotation, way more accuracy than most projects will require.

So wire up your motor, driver board and Arduino as follows (click to enlarge).

Powering the stepper motor
Looking at the diagram you can see that two power sources are needed, one for the Arduino and one for the driver board (for the motor). This is because the motor draws so much current in operation that it can affect the operation of the Arduino due to voltage drops etc. It could even damage your board if you try to draw power from it for the motor. In some articles you may see the driver board connected to the VIN pin of the Arduino or other MCU and they correctly stipulate that this is only for testing purposes and would be no good in your final project. However during my tests although the motor appeared to work in this scenario it was unpredictable, missing steps, repeating steps (presumably due to the Arduino resetting or something). Whatever the reason it was confusing when I was trying out the motor and testing its operation, so I would heavily recommend a separate power source. This can be a simple battery (i.e. 9v or rechargeable) or you could use an additional computer USB port (whilst developing the software) via a power circuit board. See below for battery configuration;

Testing
Within your Arduino environment go to File->Examples->stepper->stepper_oneRevolution and this will open an example program to test your motor set up. It should look identical to the following

However this will not drive your motor as it stands, we need to make some adjustments. The first is to change this line:

If you remember our motor has 64 steps per revolution in 8 step mode and 32 in 4 step mode. This library uses 4 step so there are 32 steps per rev, so change the line to read;

In addition we have a 64:1 gear (speed) reduction ratio meaning the total steps per 1 rev of our actual geared shaft would be 32*64 (2048). So it would actually take 2048 steps to do a full rotation of our geared shaft. But the library still needs to know the raw steps for one rotation (that the motor does internally), but for our needs when we come to controlling the motor we need to know the number of steps we would need to do to do one rotation of our geared shaft, i.e. 32 x 64 = 2048. Add this line below under the previous line;

The next line we should change is this one:

Here we pass the steps per rev and the pins. The pins will be pulsed in the order supplied and that is obviously fine for some stepper motors but not for ours, it must be changed to this order:

Alter the two lines further down that read

and

to

and

If all is correctly wired and entered then your motor should slowly turn one way for a full turn and the reverse direction for another full turn before repeating the whole thing forever!

Here’s the fully altered code if you want to be lazy and just copy and paste, notice I’ve also increased the speed ten fold in this example which should be fine if you have the same motor as myself.;

 

But it’s not working!
Check you have altered the code as above.
Check you have definitely wired it correctly (no seriously check it again).
Have you used a separate power supply for the motor (as shown) or have you quickly connected it to the Arduino’s power – don’t, it can be problematic.

What to try next
Try altering the speed, there is a commented line in the code that sets this ( to 60) and it is quite slow. I managed to get mine up to around 600 or so before it was too fast and the motor stopped turning. Try altering the code so that it does half a turn or a quarter. What about moving a certain angle? Try some of the other supplied examples in the Arduino IDE but remember about the changes we made above otherwise they won’t work.

That’s it for now, remember to subscribe to the partner YouTube channel if you like this content.

2 Comments

  1. Where-ever you need precise control, for example they are used for robots, vehicles, 3D printers, normal printers. If you need to move something a very exact amount then you’d use one of these, if you do not need such precision then a normal motor may suffice

Comments are closed.