MojoScale Studio Docs
API Reference

stepper

Stepper motor control module.

Studio Docs Sensors & I/O

import stepper

The stepper module drives common stepper motor hardware directly from GPIO. Use ``driver`` for STEP/DIR boards such as A4988, DRV8825, or TMC-style drivers. Use ``four_wire`` for simple four-coil motors through a transistor driver such as ULN2003. The calls are blocking: while a move is running, your script waits until the requested number of steps has been sent. For repeated movement, schedule small moves with ``system.schedule``.

Quick example

import stepper

axis = stepper.driver(
    step_pin=18,
    dir_pin=19,
    enable_pin=21,
    steps_per_rev=200,
    microsteps=16,
    rpm=120,
)

axis.step(3200)
axis.move_to(0)
axis.release()

import stepper

motor = stepper.four_wire(14, 27, 26, 25, steps_per_rev=2048, rpm=12)
motor.step(512)
motor.release()

importstepper

Stepper motor control module.

The stepper module drives common stepper motor hardware directly from GPIO. Use ``driver`` for STEP/DIR boards such as A4988, DRV8825, or TMC-style drivers. Use ``four_wire`` for simple four-coil motors through a transistor driver such as ULN2003. The calls are blocking: while a move is running, your script waits until the requested number of steps has been sent. For repeated movement, schedule small moves with ``system.schedule``.

API 24 available

stepper.driver(step_pin: int, dir_pin: int, enable_pin: int = None, steps_per_rev: int = None, microsteps: int = None, rpm: float = None, pulse_us: int = None, inverted: bool = None) -> stepper_motor

Create a STEP/DIR stepper controller.

Use this with driver boards that expose STEP and DIR pins. The optional enable pin is active-low: Studio drives it low while enabled and high when disabled or released.

Parameters
Name Type Pass as Required Description
step_pin int positional or keyword Yes GPIO output pin connected to the driver's STEP input.
dir_pin int positional or keyword Yes GPIO output pin connected to the driver's DIR input.
enable_pin int positional or keyword No Optional active-low enable GPIO. Defaults to -1, meaning no enable pin is controlled.
steps_per_rev int positional or keyword No Optional full steps per motor revolution. Defaults to 200 for common 1.8 degree motors.
microsteps int positional or keyword No Optional configured microstep multiplier on the driver. Defaults to 1. Set this to match your MS pins or driver settings.
rpm float positional or keyword No Optional default movement speed in revolutions per minute. Defaults to 60.
pulse_us int positional or keyword No Optional STEP pulse high time in microseconds. Defaults to 4.
inverted bool positional or keyword No Optional bool that flips direction. Defaults to False.
Returns

stepper_motor stepper_motor object.

stepper.driver().step(steps, rpm=None) -> bool

Blocking relative move. Positive and negative step counts select direction.

Parameters
Name Type Pass as Required Description
steps int positional or keyword Yes Required value.
rpm float positional or keyword No Optional value. Defaults to None.

stepper.driver().move_to(position, rpm=None) -> bool

Blocking absolute move to a logical step position.

Parameters
Name Type Pass as Required Description
position int positional or keyword Yes Required value.
rpm float positional or keyword No Optional value. Defaults to None.

stepper.driver().set_position(position) -> bool

Set the current logical position without moving the motor.

Parameters
Name Type Pass as Required Description
position int positional or keyword Yes Required value.

stepper.driver().position() -> int

Return the current logical step position.

stepper.driver().set_rpm(rpm) -> bool

Change the default movement speed.

Parameters
Name Type Pass as Required Description
rpm float positional or keyword Yes Required value.

stepper.driver().rpm() -> float

Return the current default movement speed.

stepper.driver().enable() -> bool

Enable holding torque. For STEP/DIR drivers, the enable pin is active-low.

stepper.driver().disable() -> bool

Disable holding torque and leave the controller object usable.

stepper.driver().release() -> bool

Alias for disable.

stepper.driver().info() -> dict

Return pins, mode, speed, position, and enabled state.

stepper.driver().close() -> bool

Disable outputs and close the native controller object.

stepper.four_wire(in1: int, in2: int, in3: int, in4: int, steps_per_rev: int = None, rpm: float = None, sequence: str = None, inverted: bool = None) -> stepper_motor

Create a direct four-wire stepper controller.

Use this for small steppers wired through a transistor array or bridge, such as a 28BYJ-48 with ULN2003. The firmware energizes the four outputs in a full-step or half-step sequence.

Parameters
Name Type Pass as Required Description
in1 int positional or keyword Yes GPIO output pin connected to coil driver input 1.
in2 int positional or keyword Yes GPIO output pin connected to coil driver input 2.
in3 int positional or keyword Yes GPIO output pin connected to coil driver input 3.
in4 int positional or keyword Yes GPIO output pin connected to coil driver input 4.
steps_per_rev int positional or keyword No Optional logical steps per revolution. Defaults to 2048 for common half-step 28BYJ-48 setups.
rpm float positional or keyword No Optional default movement speed in revolutions per minute. Defaults to 10.
sequence str positional or keyword No Optional coil sequence. Use "half_step" or "full_step". Defaults to "half_step".
inverted bool positional or keyword No Optional bool that flips direction. Defaults to False.
Returns

stepper_motor stepper_motor object.

stepper.four_wire().step(steps, rpm=None) -> bool

Blocking relative move. Positive and negative step counts select direction.

Parameters
Name Type Pass as Required Description
steps int positional or keyword Yes Required value.
rpm float positional or keyword No Optional value. Defaults to None.

stepper.four_wire().move_to(position, rpm=None) -> bool

Blocking absolute move to a logical step position.

Parameters
Name Type Pass as Required Description
position int positional or keyword Yes Required value.
rpm float positional or keyword No Optional value. Defaults to None.

stepper.four_wire().set_position(position) -> bool

Set the current logical position without moving the motor.

Parameters
Name Type Pass as Required Description
position int positional or keyword Yes Required value.

stepper.four_wire().position() -> int

Return the current logical step position.

stepper.four_wire().set_rpm(rpm) -> bool

Change the default movement speed.

Parameters
Name Type Pass as Required Description
rpm float positional or keyword Yes Required value.

stepper.four_wire().rpm() -> float

Return the current default movement speed.

stepper.four_wire().enable() -> bool

Re-energize the current coil phase.

stepper.four_wire().disable() -> bool

Turn all coil outputs off and leave the controller object usable.

stepper.four_wire().release() -> bool

Alias for disable.

stepper.four_wire().info() -> dict

Return pins, mode, sequence, speed, position, and enabled state.

stepper.four_wire().close() -> bool

Disable outputs and close the native controller object.