Skip to main content

Constant rotation

What this example shows​

The motor turning steadily in one direction and not stopping. This is the simplest thing you can ask a stepper to do, and the right first test after wiring the board up: if this runs, your wiring, current limit and pin assignments are all correct.

The shipped sketch uses BasicStepper stepper;, which the library header defines as AccelStepper::FULL4WIRE on pins 2, 3, 4 and 5. That drives motor coils directly and does not fit this board. Construct it with BasicStepper::DRIVER and your STEP and DIR pins instead, as below.

The example code​

Select Board
ConstantSpeed.ino
#include <Basic-Stepper-Driver-SOLDERED.h>

#define dirPin 4
#define stepPin 5

BasicStepper stepper(BasicStepper::DRIVER, stepPin, dirPin);

void setup()
{
stepper.setMaxSpeed(1000);
stepper.setSpeed(200);
}

void loop()
{
stepper.runSpeed();
}

Expected result:

The motor turns continuously in one direction at 200 steps per second. On a 200-step motor in full step, that is about one revolution per second.

There is no serial output. The only thing to watch is the shaft.

runSpeed() implements no acceleration: the motor is asked for 200 steps per second from the first step. A loaded motor may stall or buzz instead of starting. If it does, drop setSpeed() to something small, confirm it turns, then work upwards, or use Move a set number of steps, which ramps.

Functions used​

BasicStepper(uint8_t interface, uint8_t pin1, uint8_t pin2)returns None

Constructs a stepper on the given interface. Pass BasicStepper::DRIVER for this board, then the STEP pin and the DIR pin, in that order. Inherited from AccelStepper, which also accepts two further pins and an enable flag for the direct-drive interfaces.

ReturnsConstructor; no return value.

Parameters

TypeNameDescription
uint8_tinterfaceMotor interface type. BasicStepper::DRIVER (1) for a step/dir driver. Defaults to FULL4WIRE (4).
uint8_tpin1With DRIVER, the STEP pin. Defaults to 2.
uint8_tpin2With DRIVER, the DIR pin. Defaults to 3.
setMaxSpeed(float speed)returns void

Sets the ceiling on speed. It applies to setSpeed() as well: a constant speed higher than this is clamped to it, so setMaxSpeed has to be called first and has to be the larger of the two.

ReturnsNothing.

Parameters

TypeNameDescription
floatspeedMaximum speed in steps per second.
setSpeed(float speed)returns void

Sets the constant speed used by runSpeed(). Positive counts as clockwise in AccelStepper's own terms, which in practice means whichever way your coil wiring makes it turn; negative reverses it. Speeds above 1000 steps per second are documented as unreliable.

ReturnsNothing.

Parameters

TypeNameDescription
floatspeedConstant speed in steps per second. Limited by setMaxSpeed().
runSpeed()returns boolean

Steps the motor at most once, at the constant speed from the most recent setSpeed(). It applies no acceleration at all. Call it as often as you can, and at least once per step interval, or the motor runs slower than you asked.

ReturnsTrue if the motor was stepped on this call.

Parameters

This function takes no parameters.

Putting it together​

Two calls in setup(), one in loop(). The only rule is that loop() must keep coming back to runSpeed(): it steps at most once per call, so a delay() or a slow sensor read anywhere in the loop shows up directly as a slower motor.

setAcceleration() is worth mentioning for what it does not do here. The library header is explicit that runSpeed() holds a constant speed and implements no accelerations, so setting an acceleration alongside it changes nothing. Acceleration only applies to run(), runToPosition() and runToNewPosition(), which is what the position-move page uses.

To reverse direction, call setSpeed() with a negative value. To stop, stop calling runSpeed(); the driver keeps holding the motor at its current step unless you pull EN high or put the driver to sleep.