Arduino's CustomStepper library
Author:  Igor Campos

CustomStepper


CustomStepper is a library for controlling stepper motors. I've decided to make this library after having some troubles with my stepper motor and the regular Stepper library. It comes with functions to make the motor rotate a given number of times, a given angle (in degrees) or rotate until you send another command. I plan to use it to build an Arduino controlled CNC. The library default parameters are the ones of the 28BYJ-48 5-Volt stepper motor with 283712/4455 gear ratio I have.


Contents


Installation

It is really simple to install it, all you have to do is download and extract it at the proper place

Download

You can download the library here: CustomStepper.zip

Install

All you have to do to install the library is to extract it to the "libraries" directory inside your main Arduino IDE directory, there will be a new example available when you open the IDE, it will be under in Files->Examples->CustomStepper->stepper


Methods & Usage

CustomStepper (Constructor)

pin1-pin4

The constructor has from 4 to 8 parameters, the first 4 are mandatory, they are the 4 pins where the motor is connected at.

steps[]

The 5th parameter is the stepping sequence, which is an array, the 1st element is the number of steps in the sequence, it can be up to 8.

spr

The 6th parameter is the number of steps the motor takes per rotation (Note that if your motor is geared, the number of steps per rotation of the axis will be equal to the number of steps per rotation of the motor times the gear ratio). This parameter accepts float because there are some motors (like mine) with weird gear ratios that would translate in a non-integer number of spr.

rpm

The 7th parameter is self-explanatory, the motor RPMs. This parameter is also float in case you want to control the rotation more accurately, although it doesn't have an 100% guaranteed precision because the time is controlled by microseconds which are of long time.

rotation

The 8th parameter is the rotation orientation, which can be one of the defined constants of the library: STOP, CW and CCW, the last two standing for clockwise and counterclockwise respectively.

The optional parameters defaults are the following:

  1. CustomStepper(byte pin1, byte pin2, byte pin3, byte pin4, byte steps[] = (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, float spr = 4075.7728395, float rpm = 12, byte direction = CW);

setRPM

This method allows you to set the RPM of the motor, it must be greater than 0, if you want to stop the motor, use the setDirection(STOP). The type is float.

  1. void setRPM(float RPM);

setSPR

This method allows you to set the SPR (Steps Per Rotation of the motor, it must be greater than 0. The type is float.

  1. void setSPR(float SPR);

setDirection

This method allows you to set the direction of the motor. It can be one of the three predefined constants: STOP, CW, CCW. The last two refers to clockwise and counterclockwise respectively.

  1. void setDirection(byte direction = CW);

rotate

This method works in two modes. If you don't specify any value (it defaults to 0), the motor will rotate until you give it another command (rotate, setDirection(STOP) or rotateDegrees). If you enter a number of rotations, the motor will rotate that given number of times and will stop. The accepted values are positive integers, the motor won't rotate the other way if you supply a negative value. To rotate the other way use setDirection.

  1. void rotate(unsigned int rotations = 0);

rotateDegrees

This method will rotate the motor a given number of degrees. The accepted values are float. (Note that if you try to rotate to an angle that your motor doesn't support, it will rotate to the closest position and compensate the error at the next commands).

  1. void rotateDegrees(float degrees);

run

This method is very important!!! It acts like a timer that makes the motor step when necessary, it MUST be placed inside your loop(). I used this method to leave the hardware timers free for other uses. This allows you to control multiple motors. Without this method in the loop, the motor won't move.

  1. void run();

isDone

This method will return true when the motor is not moving. (When it has finished a command). If the motor is moving, it will return false.

  1. boolean isDone();

Discussion

I have opened a thread here for discussions and suggestions. Hope you enjoy the library!

Share