Stepper motor


Having some free time, and trying to make something useful out of it, I recently experiment with an Arduino and a stepper motor. I was surprised how easy it is to connect, program and use. Until you short out something, but this will be explain later so it doesn’t happen to you!

First of, small introduction about the Arduino, from their website: “Arduino is an open-source electronics platform based on easy-to-use hardware and software. It’s intended for anyone making interactive projects. Arduino senses the environment by receiving inputs from many sensors, and affects its surroundings by controlling lights, motors, and other actuators. You can tell your Arduino what to do by writing code in the Arduino programming language and using the Arduino development environment.”

The most important things to remember: it is really easy to use! You basically plug in what you want to control and the programming language is self explanatory. What you need to try this experiment yourself:

  • An Arduino microcontroller, any model will do so you can go with the basic model, called the Arduino Uno. ( ~25$ from the official website)
  • A step motor (15$ from Adafruit)
  • A step motor driver (15$ from Sparkfun)

You can also build a step motor driver yourself using MOSFET transistor, but this would require more explanation that I would maybe add in another post…

Next step, connect everything!

First of, the bipolar stepper motor are controlled using 2 cables per coil (total of 4 cables). You first have to identify which cables are part of the same coil. You can check the motor documentation for the cable color code or you can calculate the resistance of a random pair. If a pair isn’t from the same coil, the resistance will be infinite. Otherwise, for example with the Adafruit motor I get a resistance of around ~35Ω.

With the cables identified it’s time to plug in the stepper motor to the driver. It is really important not to unplug the motor from the driver when it’s powered on or it can break the chip of the board (trust me, I did it!).

Next you have to plug the two control cables and the ground cable from the Arduino to the driver. The control cables will be use to modify the direction and rotate the stepper. You can see the image on top of the page for more visual info!

Finally, here is a simple Arduino code found on Sparkfun’s website. The motor will turn for 200 steps and then switch direction. You can adjust the time delay between the change from High/Low of the step pin to increase or decrease the speed of the motor. Make sure to also adjust the stpPin and dirPin numbers according to your setup.

//Declare pin value
#define stpPin 2
#define dirPin 3
int Distance = 0;

The pin definition and initial value.

void setup() {
  pinMode(stpPin, OUTPUT);
  digitalWrite(stpPin, LOW);
  pinMode(dirPin, OUTPUT);
  digitalWrite(dirPin, LOW);

Finally the loop that will be executed until the end of time.

void loop() {
  // one step
  digitalWrite(stpPin, HIGH);
  digitalWrite(stpPin, LOW);
  // record this step
  Distance = Distance + 1;
  // Check end of movement
  if (Distance == 200)
    // Reverse direction (invert DIR signal)
    if (digitalRead(dirPin) == LOW) {
      digitalWrite(dirPin, HIGH);
    } else {
      digitalWrite(dirPin, LOW);
    // Reset distance back to zero
    Distance = 0;
    // Now pause for half a second

There is also a Arduino library called AccelStepper that simplify the syntax to specify motor speed, acceleration and complex movement.

To finish, here is a picture of my setup of a Kollmorgen P7000 driver controlled with a similar Arduino code. This drive requires a positive and negative cable of both step (white-green) and direction (orange-white) and I added a digital ground (yellow). On the right side of the Arduino, I use analog pin A0 as a digital output to trigger and synchronize high speed cameras.


Leave a Reply

Your email address will not be published.