Language: EN

libreria-arduino-asyncstepper

Arduino AsyncStepper Library

The AsyncStepper library allows non-blocking movement of a stepper motor. This provides a stepper motor with a certain “asynchronous” behavior.

The AsyncStepper class implements a stepper motor whose movements are timed, rather than being blocking. The goal of the library is to be able to move one or more stepper motors in a project, even at different speeds, while not preventing the execution of other tasks in the main control loop.

The AsyncStepper class does not control the stepper motor, it only times the steps. This makes it very versatile, as it is independent of the stepper motor or controller used.

To control the stepper motor, we must provide the actionCW and actionCCW CallBack functions that contain the necessary code to move a step clockwise and counterclockwise, respectively.

The AsyncStepper class is specially designed to work in conjunction with controllers such as the A4988 or the DRV8825. These types of controllers move a step when they receive a pulse.

To make it easier to use these controllers, the AsyncStepper class provides a constructor that receives two pins. In this case, the default CallBack actions are initialized to generate a digital pulse to act on the controller.

To update the position of the stepper motor, the Update() function must be called, which checks the elapsed time and calls the CallBack actions if necessary. This function should be invoked as frequently as possible from the main loop.

User Manual

The AsyncStepper class can be instantiated as an object through one of its constructors,

AsyncStepper(uint16_t motorSteps, int pinCW, int pinCCW);
AsyncStepper(uint16_t motorSteps, StepCallback actionCW, StepCallback actionCCW);

Using AsyncStepper

// Move a certain angle
void Rotate(float angleDelta, StepDirection direction);
void Rotate(float angleDelta, StepDirection direction, StepCallback callback);
  
// Move to a certain angle
void RotateToAngle(float angle, StepDirection direction, StepCallback callback);
void RotateToAngle(float angle, StepDirection direction);
 
// Move continuously
void RotateContinuos(StepDirection direction);

// Change the speed of the stepper motor
void SetSpeedRpm(float rpm);
void SetSpeedDegreesBySecond(float degreesBySecond);
void SetSpeedRadiansBySecong(float radiansBySecond);
void SetMetersBySecond(float metersBySecond, float radius);

// Stop the stepper motor
void Stop();

// Update the position of the stepper motor
// This method needs to be called frequently from the main loop
bool Update();

// Get the current angle
float GetCurrentAngle() const;

// Callback functions for stepping forward
StepCallback ActionCW;
StepCallback ActionCCW;

// Output pins for use with external stepper motor controllers
int PinCW;
int PinCCW;

Installation

  • Download the latest version from GitHub
  • Unzip the file
  • Copy to your libraries folder (usually My Documents\Arduino\libraries)
  • Restart the Arduino IDE

github-full

Examples

The AsyncStepper library includes the following examples to illustrate its use.

  • AsyncStepper: Example demonstrating the control of a stepper motor without a controller
#include "AsyncStepperLib.h"
#include <Stepper.h>

const int motorPin1 = 8;  
const int motorPin2 = 9;  
const int motorPin3 = 10; 
const int motorPin4 = 11; 
const int numSteps = 8;
const int stepsLookup[8] = { B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 };
int stepCounter = 0; 

void clockwise()
{
  stepCounter++;
  if (stepCounter >= numSteps) stepCounter = 0;
  setOutput(stepCounter);
}

void anticlockwise()
{
  stepCounter--;
  if (stepCounter < 0) stepCounter = numSteps - 1;
  setOutput(stepCounter);
}

void setOutput(int step)
{
  digitalWrite(motorPin1, bitRead(stepsLookup[step], 0));
  digitalWrite(motorPin2, bitRead(stepsLookup[step], 1));
  digitalWrite(motorPin3, bitRead(stepsLookup[step], 2));
  digitalWrite(motorPin4, bitRead(stepsLookup[step], 3));
}

const int stepsPerRevolution = 4076;
AsyncStepper stepper1(stepsPerRevolution,
  []() {clockwise(); },
  []() {anticlockwise(); }
);

void rotateCW()
{
  stepper1.Rotate(90, AsyncStepper::CW, rotateCCW);
}

void rotateCCW()
{
  stepper1.Rotate(90, AsyncStepper::CCW, rotateCW);
}

void setup()
{
  Serial.begin(9600);

  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);

  stepper1.SetSpeedRpm(10);
  stepper1.RotateContinuos(AsyncStepper::CCW);
}

void loop()
{
  stepper1.Update();
}