One step at a time

Stepper motors, due to their unique design, can be controlled to a high degree of accuracy without any feedback mechanisms. The shaft of a stepper, mounted with a series of magnets, is controlled by a series of electromagnetic coils that are charged positively and negatively in a specific sequence, precisely moving it forward or backward in small "steps".

There are two types of steppers, Unipolars and Bipolars, and it is very important to know which type you are working with. For each of the motors, there is a different circuit. The example code will control both kinds of motors. See the unipolar and bipolar motor schematics for information on how to wire up your motor.

In this example the motor will step one step at a time, very slowly. You can use this to test that you've got the four wires of your stepper wired to the correct pins. If wired correctly, all steps should be in the same direction. You may also use this sketch to count the number of steps that your motor does in one revolution.

The stepper is controlled by with digital pins 8, 9, 10, and 11 for either unipolar or bipolar motors. The Arduino or Genuino board will connect to a U2004 Darlington Array if you're using a unipolar stepper or a SN754410NE H-Bridge if you have a bipolar motor.

For more information about the differences of the two types, please take a look at Tom Igoe's page on stepper motors.

Hardware Required

  • Arduino or Genuino Board
  • stepper motor
  • U2004 Darlington Array (if using a unipolar stepper)
  • SN754410ne H-Bridge (if using a bipolar stepper)
  • power supply appropriate for your particular stepper
  • hook-up wires
  • breadboard

Circuits

Below you'll find circuits for both unipolar and bipolar steppers. In either case, it is best to power your stepper motors from an external supply, as they draw too much to be powered directly from your Arduino or Genuino board.

Note: Both circuits below are four wire configurations. Two wire configurations will not work with the code provided.

Unipolar Stepper Circuit and schematic

Show images for the unipolar circuit and schematicShow images for the unipolar circuit and schematic

Bipolar Stepper Circuit and schematic

Show images for the bipolar circuit and schematicShow images for the bipolar circuit and schematic

Code

For both unipolar and bipolar steppers

/*
 Stepper Motor Control - one step at a time

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor will step one step at a time, very slowly.  You can use this to
 test that you've got the four wires of your stepper wired to the correct
 pins. If wired correctly, all steps should be in the same direction.

 Use this also to count the number of steps per revolution of your motor,
 if you don't know it.  Then plug that number into the oneRevolution
 example to see if you got it right.

 Created 30 Nov. 2009
 by Tom Igoe

 */


#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;         // number of steps the motor has taken

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one step:
  myStepper.step(1);
  Serial.print("steps:");
  Serial.println(stepCount);
  stepCount++;
  delay(500);
}

See also


Last revision 1028/08/23 by SM