RPi::StepperMotor - Control a typical stepper motor with the Raspberry Pi


    use warnings;
    use strict;

    use RPi::StepperMotor;

    my $sm = RPi::StepperMotor->new(
        pins  => [12, 16, 20, 21],
        speed => 'half',            # optional, default
        delay => 0.01               # optional, default
        name  => 'tilt'             # optional, default undef
        expander => $expander_obj   # optional, default undef

    $sm->cw(180);  # turn motor 180 degrees clockwise
    $sm->ccw(240); # 240 degrees the other way

    $sm->speed('full'); # skip every second step, turning the motor twice as fast
    $sm->delay(0.5);    # set the delay to a half-second in between steps

    $sm->name('new name');

    $sm->cleanup; # reset pins back to INPUT


Control a 28BYJ-48 stepper motor through a ULN2003 driver chip, with the option to run it from an MCP23017 GPIO Expander.



Instantiates and returns a new RPi::StepperMotor object.


    pins => $aref

Mandatory, Array Reference: The ULN2003 has four data pins, IN1, IN2, IN3 and IN4. Send in the GPIO pin numbers in the array reference which correlate to the driver pins in the listed order.

    speed => 'half'|'full'

Optional, String: By default we run in "half speed" mode. Essentially, in this mode we run through all eight steps. Send in 'full' to double the speed of the motor. We do this by skipping every other step.

    delay => Float|Int

Optional, Float or Int: By default, between each step, we delay by 0.01 seconds. Send in a float or integer for the number of seconds to delay each step by. The smaller this number, the faster the motor will turn.

    expander => Object

Optional, RPi::GPIOExpander::MCP23017 object instance. Send one of these objects in, and we'll run the stepper motor from that instead. The pins parameter still needs to be sent in, but will be in the limit 0-15, the four pins attached from the expander to the motor, in the order of IN1 through IN4.


Turns the motor in a clockwise direction by a specified number of degrees. Clockwise is defined when the shaft of the motor is facing you/upwards.



Mandatory, Integer: The number of degrees to turn the motor in a clockwise direction.


Turns the motor in a counter-clockwise direction by a specified number of degrees. Counter-clockwise is defined when the shaft of the motor is facing you/upwards.



Mandatory, Integer: The number of degrees to turn the motor in a counter-clockwise direction.


Sets all pins back to INPUT mode. This should be called near the end of your script.

Takes no parameters, has no return.


This is the amount of time to delay between each step of the motor. It defaults to 0.01 seconds.



Optional, Float|Int: The number of seconds (or fraction of seconds) to delay between each step of the motor.


The currently set delay time.


When you have more than one servo in an application, it may be useful to give each motor its own name for printing purposes.



Optional, String. The name you want to give the servo.

Return: The name if one has been set, otherwise undef.


The motor can operate in 'half' speed mode (where all eight steps are used) or 'full' speed mode, where every second step is skipped. 'half' speed mode is more accurate, but 'full' speed mode is faster.



Optional, String: Send in 'full' to skip every second step rendering the motor twice as fast. The other option is 'half', which is the default setting.


The currently set speed.


I've included a very simple binary that is installed with this distribution that performs all of the functions in the module easily from the command line.


    stepper <direction> <degrees> [speed]


    # turn clockwise 180 degrees at normal (half) speed

    stepper cw 180
    # turn counter-clockwise 240 degrees at full speed

    stepper ccw 240 full


Steve Bertrand, <steveb at>


Copyright 2018 Steve Bertrand.

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at: