Having a look around on the internet, there are a few forums in which stepper motor control is discussed, however, none of them go into the detail required for an electronics newbie to be able to get their motor up and running with the hardware connections and the sketch. So here’s our complete step by step guide to get any Arduino to control a stepper motor.

Being able to control a stepper motor with your Arduino opens up a world of opportunity for new projects. Stepper motors offer precise control over their speed, position and direction so they are a popular choice for robotics, 3D printers, CNC projects and servo drive mechanisms.

pololu stepper motor driver

We will be using the Pololu range of stepper motor drivers as they are really cheap, easy to work with, offer simplistic control and are compatible with all of the Arduino development boards.

What You Will Need To Control A Stepper Motor With Your Arduino

How To Control Your Stepper Motor

First we will start by connecting the hardware together and then we will move on to the software/sketch. In this example we have used the same power supply to power the Arduino and the motor through the Pololu driver however these can be two separate power supplies.

Assemble The Components

You have two options when assembling the components, the first is to solder all of the connections to the Pololu motor controller and then solder the connections to the Arduino through a pin header and the second option is to solder pin headers onto the underside of the Pololu motor controller and then plug it into a breadboard to use jumpers to connect it to the Arduino. Use the soldered connections for a permanent installation or on something which is going to be moving around a lot and use the breadboard if you are just playing around with ideas on a bench.

The basic circuit and connections to the Arduino and the Pololu driver are shown below, click to enlarge the image if required:

arduino stepper motor circuit diagram

Connect the Pololu direction pin to Arduino pin 0 and the step pin to Arduino pin 1 as these are setup in the sketch. You can connect more than one motor to the Arduino by making simultaneous connections to any of the available pins.

Bridge the reset and sleep pins on the drive controller as we will not be using these.

Your stepper motor should have four different coloured wires, these will be identified on your motors data sheet as shown below:

stepper motor wiring diagram

The wires work as pairs, one on each side of the opposing motor windings, in this case red and yellow form one pair and blue and orange form a second pair. These wires need to be connected in their pairs to the 1A and 1B and the 2A and 2B pins on the drive controller. It doesn’t matter which way around they are connected as long as one pair is connected to the 1 pins and the other pair to the 2 pins. Your motor may have different coloured wires so it is important to get the datasheet, alternately you can measure the resistance across wires with a multimeter, the pairs will have a resistance and the non pairs will show open circuit.

Choosing Different Components

Choosing A Different Stepper Motor

Depending on your application, you may require a bigger or smaller stepper motor. Stepper motors are generally sized by their output torque, you will need to figure our, either through experimentation or through calculations how much torque you require. You can then select a motor which can produce your required torque, the data sheet will then tell you the required current and then you can select a motor controller to suite.

Sizing the Stepper Motor Driver

The drive controller used in this example, the A4988 can drive a motor up to 1A and the second controller mentioned in the parts list, the DRV8825 can drive a motor up to 2A. Pololu make a number of stepper motor drive controllers (shown here) for different size motors, it is just important that you have a controller which can produce more current than the motor requires otherwise it will burn out.

Upload The Sketch

Now you can upload your sketch onto your Arduino, if you haven’t uploaded a sketch before then follow this guide on getting started.

Here is the link to download the steppermotor code.

The sketch starts by assigning the motor and direction pins in the setup function as declared in the created variables.

The Arduino then continuously runs through the loop code which will turn the motor in one direction a certain number of pulses (rotation angle) and at a certain speed before reversing and running in the opposite direction at the same speed and number of pulses.

Changing The Motor Speed, Number Of Rotations And Direction

There are three parameters in the sketch which can be modified to produce your desired motor output, these are the speed at which the motor rotates, the total angle of rotation or number of rotations traveled  and finally the motor direction.

We will start with the speed. The motor speed is determined by how long the Arduino waits between consecutive pulses. The speed is set in row 9 under the variable motorSpe and represents the time in milliseconds between pulses. The shorter the time between pulses, the faster the motor will turn. This has its limitations though, both through the motor and the controller. You should be able to lower this value to about 100 milliseconds without any issues, if the motor starts humming, skipping around or moving erratically then the pulse speed is likely too fast and should be slowed down.

The second parameter is the rotation angle or number of rotations. Stepper motors have a certain number of steps per revolution or poles, this figure is used to determine how many degrees or turns the motor will make. The number of pulses in defined in row 10 under the rotation variable. It is currently set at 1000 pulses which means that on a 200 step per revolution motor, the motor will make 5 complete turns.  This can be adjusted as high or low as necessary for your project. If we had set the rotation to 20, then the motor would move 20 steps which corresponds to 20/200ths of a full rotation or 18 degrees.

The final parameter is the rotation direction which is defined in row 8 and is set on the drive controller in rows 20-27. Setting the direction output pin HIGH will rotate the motor in one direction and LOW will rotate the other direction.

If you’re looking for a project in which you can use your stepper motor controller, have a look at our solar tracker project, it utilises a linear actuator which can be stepper motor controllers to precisely position a solar array such that it is always in direct sunlight.

Did you find this guide helpful? Were you able to control and run a stepper motor? Let us know in the comments section below.

Have a look at our other Arduino projects here.