skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: Teensy Teensyduino Libraries AccelStepper

PJRC Store
Teensy 3.1, $19.80
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
USB Cable, $4.00
Teensy
Main Page
Teensy 3.1
Getting Started
How-To Tips
Code Library
Projects
Teensyduino
Reference

AccelStepper Library

The AccelStepper library allows you to connect multiple stepper motors with controlled acceleration and deceleration.

Download: AccelStepper.zip (version 1.30)

Hardware Requirements

AccelStepper is compatible with all Teensy boards. It can output signals to control transistors, or Step & Direction signals to stepper motor driver boards.


AccelStepper on Teensy++ 2.0 with Pololu A4988 Drivers

Basic Usage

You can create multiple AccelStepper objects, giving a unique name to each motor. AccelStepper can manage any number of motors, as long as you repetitively call their "run" functions.

Defining & Configuring Motors

AccelStepper mystepper(1, pinStep, pinDirection);

A stepper motor controlled by a dedicated driver board.

AccelStepper mystepper(2, pinA, pinB);

A bipolar stepper motor controlled by an H-Bridge circuit.

AccelStepper mystepper(4, pinA1, pinA2, pinB1, pinB2);

A unipolar stepper motor, controlled by 4 transistors.

mystepper.setMaxSpeed(stepsPerSecond);

Sets the maximum speed. The default is very slow, so this must be configured. When controlled by setting position, the stepper will accelerate to move at this maximum speed, and decelerate as it reaches the destination.

mystepper.setAcceleration(stepsPerSecondSquared);

Sets the acceleration to be used, in steps per second per second.

Position Based Control

mystepper.moveTo(targetPosition);

Move the motor to a new absolute position. This returns immediately. Actual movement is caused by the run() function.

mystepper.move(distance);

Move the motor (either positive or negative) relative to its current position. This returns immediately. Actual movement is caused by the run() function.

mystepper.currentPosition();

Read the motor's current absolution position.

mystepper.distanceToGo();

Read the distance the motor is from its destination position. This can be used to check if the motor has reached its final position.

mystepper.run();

Update the motor. This must be called repetitively to make the motor move.

mystepper.runToPosition();

Update the motor, and wait for it to reach its destination. This function does not return until the motor is stopped, so it is only useful if no other motors are moving.

Speed Based Control

mystepper.setSpeed(stepsPerSecond);

Set the speed, in steps per second. This function returns immediately. Actual motion is caused by called runSpeed().

mystepper.runSpeed();

Update the motor. This must be called repetitively to make the motor move.

Example Program

#include <AccelStepper.h>

//AccelStepper Xaxis(1, 2, 5); // pin 2 = step, pin 5 = direction
//AccelStepper Yaxis(1, 3, 6); // pin 3 = step, pin 6 = direction
//AccelStepper Zaxis(1, 4, 7); // pin 4 = step, pin 7 = direction

AccelStepper Xaxis(1, 3, 6); // pin 3 = step, pin 6 = direction
AccelStepper Yaxis(1, 4, 7); // pin 4 = step, pin 7 = direction
AccelStepper Zaxis(1, 5, 9); // pin 5 = step, pin 8 = direction

void setup() {
  Xaxis.setMaxSpeed(400);
  Yaxis.setMaxSpeed(400);
  Zaxis.setMaxSpeed(400);
  Xaxis.setSpeed(45);
  Yaxis.setSpeed(25);
  Zaxis.setSpeed(80);
}

void loop() {  
   Xaxis.runSpeed();
   Yaxis.runSpeed();
   Zaxis.runSpeed();
}


AccelStepper on Teensy 3.0 with Pololu A4988 Drivers

More Details

Please refer to the official AccelStepper library documentation for more details.