Steve Bertrand

NAME

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

SYNOPSIS

    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
    );

    $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->cleanup; # reset pins back to INPUT

DESCRIPTION

Control a 28BYJ-48 stepper motor through a ULN2003 driver chip.

This is the only setup I've tested. If I come across any more in the future, I will update this distribution and allow a user to selectively pick which setup they would like to use.

METHODS

new

Instantiates and returns a new RPi::StepperMotor object.

Parameters:

    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.

cw($degrees)

Turns the motor in a clockwise direction by a specified number of degrees.

Parameters:

    $degrees

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

ccw($degrees)

Turns the motor in a counter-clockwise direction by a specified number of degrees.

Parameters:

    $degrees

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

cleanup

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

Takes no parameters, has no return.

delay($seconds)

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

Parameters:

    $seconds

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

Returns:

The currently set delay time.

speed($speed)

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.

Parameters:

    $speed

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.

Returns:

The currently set speed.

STEPPER BINARY CLI TOOL

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.

Usage

    stepper <direction> <degrees> [speed]

Example

    # turn clockwise 180 degrees at normal (half) speed

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

    stepper ccw 240 full

AUTHOR

Steve Bertrand, <steveb at cpan.org>

LICENSE AND COPYRIGHT

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:

http://www.perlfoundation.org/artistic_license_2_0