Stepper Motor
#Description
A stepper motor is a very powerful and precise motor as you can define the position precisely to a step. It is often used in CNC machines like 3D printers.
To use a stepper motor, you should connect a the motor to a driver. Compatible drivers are the A4988 or DRV8825 (or similar). Please refer to our documentation website to see how it is connected. The first pin of the component should be connected to the DIR pin and the second pin of the component should be connected to the STEP pin (unless the "Flip Pins" option is set).
This component requires 2 pins.
#Functions
#Connecting the driver

Here you can see a schematic diagram on how to connect the stepper motor with a DRV8825 driver and an Arduino. (Source Makerguides).
Please refert to Makerguides’s tutorial to see more details on how to connect the DRV8825 driver or the A4988 driver with a stepper and Arduino board.
#JavaScript / Node.js
StepperMotor component for controlling stepper motors via AccelStepper (STEP/DIR driver). Extends ComponentBase directly as a complex output component with custom serial commands.
#Constructor options
| Option | Type | Required | Description |
|---|---|---|---|
pin | number | DigitalPin | required | — |
dirPin | number | DigitalPin | required | — |
stepsPerRevolution | number | optional | — |
flipPins | boolean | optional | — |
#Methods
moveTo(absoluteSteps: number)→ thisMove to an absolute position in steps.
move(relativeSteps: number)→ thisMove a relative number of steps from current position.
setSpeed(stepsPerSecond: number)→ thisSet constant speed in steps per second (bypasses acceleration).
setMaxSpeed(stepsPerSecond: number)→ thisSet maximum speed in steps per second.
setAcceleration(stepsPerSecondSquared: number)→ thisSet acceleration in steps per second squared.
setDeceleration(stepsPerSecondSquared: number)→ thisSet deceleration in steps per second squared. When set to a value > 0, the motor uses this rate when slowing down instead of the acceleration rate.
setCurrentPosition(position: number)→ thisReset the current position counter without moving.
stop()→ thisDecelerate to a stop.
stopNow()→ thisStop immediately (no deceleration).
get position→ numberGet the current position in steps.
get value→ number | undefinedfrom ComponentBaseGet the current value of the component.
#Example
import { Board, StepperMotor } from "@blokdots/components";
const board = new Board("/dev/ttyACM0");
await board.open();
const stepperMotor = new StepperMotor({ board, pin: "D5", dirPin: "D5" });#C++ (Arduino)
Default: DRIVER mode (STEP/DIR). Non-blocking: tick() calls stepper.run().
#Required libraries
#include <AccelStepper.h>— AccelStepper v1.64.0
#Setup
#include "blokdots.h"
StepperMotor myComponent(5, 5);#Constructor
StepperMotor(stepPin, dirPin, stepsPerRevolution, enablePin, invertDir, invertStep, enableOutputsOnBegin)| Parameter | Type | Default |
|---|---|---|
stepPin | uint8_t | required |
dirPin | uint8_t | required |
stepsPerRevolution | int32_t | 200 |
enablePin | int8_t | -1 |
invertDir | bool | false |
invertStep | bool | false |
enableOutputsOnBegin | bool | true |
StepperMotor(bus, stepPin, dirPin, evtChanged, evtTargetReached, stepsPerRevolution, enablePin, invertDir, invertStep, minEmitIntervalMs, emitPositionStep, enableOutputsOnBegin)| Parameter | Type | Default |
|---|---|---|
bus | EventBus | required |
stepPin | uint8_t | required |
dirPin | uint8_t | required |
evtChanged | uint8_t | required |
evtTargetReached | uint8_t | 0 |
stepsPerRevolution | int32_t | 200 |
enablePin | int8_t | -1 |
invertDir | bool | false |
invertStep | bool | false |
minEmitIntervalMs | uint16_t | 20 |
emitPositionStep | int32_t | 1 |
enableOutputsOnBegin | bool | true |
#Methods
setStepsPerRevolution(steps)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
steps | int32_t | required |
stepsPerRevolution()→ int32_tsetMaxSpeed(stepsPerSecond)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
stepsPerSecond | float | required |
setAcceleration(stepsPerSecond2)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
stepsPerSecond2 | float | required |
setDeceleration(stepsPerSecond2)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
stepsPerSecond2 | float | required |
setSpeed(stepsPerSecond)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
stepsPerSecond | float | required |
setCurrentPosition(pos)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
pos | int32_t | required |
setEnablePin(pin, enableActiveLow)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
pin | int8_t | required |
enableActiveLow | bool | true |
enableOutputs()→ StepperMotor&disableOutputs()→ StepperMotor&moveTo(absoluteSteps)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
absoluteSteps | int32_t | required |
move(relativeSteps)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
relativeSteps | int32_t | required |
stop()→ StepperMotor&stopNow()→ StepperMotor&moveToRevolutions(revs)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
revs | float | required |
moveRevolutions(revsDelta)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
revsDelta | float | required |
moveToDegrees(degrees)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
degrees | float | required |
moveDegrees(degreesDelta)→ StepperMotor&| Parameter | Type | Default |
|---|---|---|
degreesDelta | float | required |
emittedValue()→ int