Arduino & Stepper motors

From Interaction Station Wiki
Jump to navigation Jump to search

Stepper motor: how and why

A stepper motor is essentially a servo motor that uses a different method of motorization. Where a servo motor uses a continuous rotation DC motor and integrated controller circuit, stepper motors use multiple toothed electromagnets arranged around a central gear to define position.

Stepper motors require an external control circuit or micro controller (for instance a Raspberry Pi or Arduino) to individually energize each electromagnet and make the motor shaft turn. When electromagnet ‘A’ is powered it attracts the gear’s teeth and aligns them, slightly offset from the next electromagnet ‘B’. When ‘A’ is switch off, and ‘B’ switched on, the gear rotates slightly to align with ‘B’, and so on around the circle, with each electromagnet around the gear energizing and de-energizing in turn to create rotation. Each rotation from one electromagnet to the next is called a "step", and thus the motor can be turned by precise predefined step angles through a full 360 Degree rotation.

Stepper motors are available in two varieties; unipolar or bipolar. Bipolar motors are the strongest type of stepper motor and usually have four or eight leads. They have two sets of electromagnetic coils internally, and stepping is achieved by changing the direction of current within those coils. Unipolar motors, identifiable by having 5,6 or even 8 wires, also have two coils, but each one has a center tap. Unipolar motors can step without having to reverse the direction of current in the coils, making the electronics simpler. However, because the center tap is used to energize only half of each coil at a time they typically have less torque than bipolar.
These motors have four to eight wires you need to use to control the pulses to make the shaft step continuously, so they’re more complicated to control than the previously described motors. They are squatter looking than the rest of the DC motor family, and have less torque than you might expect for their size and weight. However, they’re also the fastest way to integrate both speed and position control into a project. The design of the stepper motor provides a constant holding torque without the need for the motor to be powered and, provided that the motor is used within its limits, positioning errors don't occur, since stepper motors have physically predefined stations.


Steppers work by moving in a bunch of little increments, or steps. If you step them fast enough, it looks like continuous motion. Each time one of the coils is energized, it pulls one of the teeth on the shaft toward it to complete one step. For example, a 200-step motor moves in a full 360° circle at 1.8° per step.

Components needed

Now you might want to connect the stepper motor to a microcontroller like Arduino.
You will need:

  • Arduino Uno

Arduino uno.jpg

  • Adafruit Motor Shield

Motor shield.jpg
There are also other drivers you can use to connect the motors to your microcontrollers, like for instance [|this ones] or an H-Bridge. We will use the Motor shield as it provides a set-up to work with more than one motor and leaving your digital pins free to use for other sensors or actuators.

  • Jumper wires
  • External power supply

Sower sup ext.jpg
Your Arduino can only provide 5 volt while the motor usually requires between 9 and 12 volt so we are adding an external power supply. You can supply energy in other ways as well, like for instance with a battery. In our case we are using a switching power supply connected to a barrel jack to screw terminal with two jumper wires.

Wiring instructions

1. Place the Motor shield on the top of your Arduino

Arduino motor shield.jpg

You can see that your shield has 3 blue screw terminals on top, 2 for your motors and one for your power supply. The other pins of the Arduino are still free in case you want to add sensors or other parts to your project.
2. Connect the motor to one of the two main screw terminal blocks
There are several types of stepper motors and they can have different numbers of wires of different colors making it a bit hard sometimes to understand how to properly connect it to your circuit without having to use a multimeter.
Here is a diagram of the wires color combinations and how they should be connected to the motor shield.
Motor wires scheme-02.jpg
The final result should look like this:
Motor connection.jpg
3.Connect the Motor Shield to the power supply
Connect the power supply wires to the power screw terminal on your shield. There is a + and - sign on the shield to help you. If you wired it correctly an LED light on the shield should turn on. If not, check if your power supply is actually working.
Power connection.jpg
4.Connect the Arduino to your computer

Arduino Coding

1. Open the Arduino IDE on your computer
If you don't have the software you can download it from here:
2. Install the Motor Shield library
Arduino often requires libraries to be able to communicate to different types of hardware, like for instance our shield.
Install the Adafruit Motor Shield Library by going on Sketch > Include Library > Manage Libraries. A search bar will appear where you can look for it. In this example we are using the Adafruit Motor Shield V2 Library because that's our shield model.
Install it with all its dependencies.
3. Test your motor with the Library example code
Now that you installed the library you can use one of the example codes that come with it to test if your motor is working and wired correctly.
Go to File > Examples > Adafruit Motor Shield V2 Library > Stepper test .
At line 19 of your code you can check if you are using the same motor port. Write 1 if you have your motor connected to the M1 M2 port of your shield or 2 if you are using the M3 M4.
Before uploading the code make sure that you have the right board ad port selected. You can check this by going to Tools.
Now upload the code on your board by pressing the →.
4. Customize your code
Your motor should now be making 4 different kinds on motions accordingly to the example code.
You can now modify it, accordingly to what you want to achieve by determining the speed, number of steps and direction. Remember that unlike Servo motors the value determining the amount of rotation is not in degrees° but in steps. A full 360 Degree rotation corresponds to 200 steps.