Wiring and Controlling Geared Steppers

IMG_1320

Before we start we start wiring up our geared stepper motors its worth reviewing two related sparks;

The first, Steppers, Servos and DC Motors, tells us what the different types of electric motors are and what they are used for.

The second ,Controlling little motors: Relay Shield, introduces us to the  relay shield which is pace on the arduino to control motors and to manage their power requirements.

The motor shield currently sold in Polygon Door kits will allow us to control up to four DC motors or two Stepper Motors and two Servos. New generations of card allow you to stack relay shields to multiply the possible number of motors controlled.

Your motor comes with a lovely plug that doesn’t fit the motor shield we are using. We have two options here – but the plug/terminal of or use some leads to connect the plug to the motor shield contacts (or use a seperate breadboard)

IMG_1321

If you cut the plug off its a lot clearer to see whats happening but when you need to remove the stepper for mounting or transport you are forced to undo the screws on the contact points of the motor shield.

For clarity’s sake I have cut the plug off in my example.

This stepper has five leads – (other stepper may have four and up to eight). Four and five lead steppers operate similarly from our perspective except that the 5 lead steppers have a seperate ground lead.

In fact 4 lead Steppers are bipolar steppers and 5-8 lead are unipolar – you can read more about the differences here: https://www.circuitspecialists.com/blog/unipolar-stepper-motor-vs-bipolar-stepper-motors/.

The colouring and numbering of Stepper leads is opaque at best and can vary according to the factory in which its been manufactured – on the geared steppers in our kits the cables come out of the stepper as two positive pairs, followed by ground, followed by two negative pairs.

We are going to use a screw driver to connect these wires to a pair of motor driver terminals (and ground terminal) on the motor shield. These terminals are two groups at either end of the board and are labelled M1 & M2 or M3 & M4 (these terminal can drive four DC motors)

To connect the stepper to the motor shield connect them in the order;
orange – pink – red – blue – yellow. From right to left looking at the terminals.

IMG_1322

The adafruit site documents this motor here and as they explain ‘there are only 32 step (11.25 degree) per revolution, and inside is a 1/16 reduction gear set. (Actually its 1/16.032 but for most purposes 1/16 is a good enough approximation) What this means is that there are really 32*16.032 steps per revolution = 513 steps! ‘

Once the motor is wired up we can practice using the combination of libraries that power both this particular Motor Relay Shield (Adafruit V1)  – AFMotor and the libraries required to drive the stepper AccelStepper.

The AccelStepper library: http://www.airspayce.com/mikem/arduino/AccelStepper/index.html

The AFMotor library: https://github.com/adafruit/Adafruit-Motor-Shield-library

The AFMotor library can drive a stepper but we will require AccelStepper to drive more than one Stepper simultaneously – something we will probably want to do to drive a wheeled robot.

Once you’ve used the library manager to install the required libraries grab the code below and upload it via the Arduino IDE.

// Blocking.pde
// -*- mode: C++ -*-
//
// Shows how to use the blocking call runToNewPosition
// Which sets a new target position and then waits until the stepper has 
// achieved it.
//
#include <AccelStepper.h> // this is the library required to drive two steppers simultaneously
#include <AFMotor.h> // this is the motor sheild library - it will drive a stepper but provides only basic functionality we require the additional AccelStepper.h library to drive 2 steppers simultaneously
AF_Stepper motor1(513, 1); // here we intialize the library by telling the program how many steps our motor has and which set of terminals we are using (either 1 or 2). 


// Now we create two functions (these will become methods of the 'stepper' object that we create next) for steps in each direction for the object called motor1 via the AFMotor library function onestep(). 
//first attribute is a direction
//second attribute is either SINGLE, DOUBLE, INTERLEAVE, or MICROSTEP
//SINGLE uses a pair of magnets to push the step, DOUBLE use two pairs - the resulting torque is greater but so is the power usage.
// INTERLEAVE is a method for executing halfstepping for increased resolution at half speed
// MICROSTEP is method using Pulse Width Modulation to create smooth transition between steps


void forwardstep() { 
 motor1.onestep(FORWARD, SINGLE); //onestep is a function of AFMotor library.
}
void backwardstep() { 
 motor1.onestep(BACKWARD, SINGLE);
}



// here we create a object called stepper via the AccelStepper library with the methods defined as the functions above. 

AccelStepper stepper(forwardstep, backwardstep); 

void setup() 
{ 
 stepper.setMaxSpeed(380); // this is the speed we accelerate to when using the accelerate function- the library seems to require it in setup as initialisation (this isn't clear anywhere)
 // the value of setMaxSped seems dependent on the processer/clock and the motor.... my motor seems to lock up at 390 p its not clear what this number means
 
 stepper.setAcceleration(3800); // I have set acceleration high enough to be inoperable - without acceleration this method appears not to work - it only need be set once in setup.
// the alternative is to use runSpeedToPosition() which would require a different logic.

 stepper.moveTo(1026); // the function moveTo sets the position that the motor will 'move to' when it is called by stepper.run(); this number is twice the number of steps to create a full circle.

}

void loop()
{ 
 if(stepper.distanceToGo() == 0) // check to see if the distance remaining out of 1026 (moveTo) is == 0 'Are we there yet'
 stepper.moveTo(-stepper.currentPosition()); // if we are there then make the new moveTo position be negative of the current position
 stepper.run(); // this runs the motor according to setting determined in former functions ... ie. Position and Speed.
 
}

Once the code is running your should find your stepper motor moving rotating backward and forward through a full 360 degrees.

I have commented the code above extensively so you can  read my explanation of what is going on here.

Once you’ve done that and understand what is going on in this example its worthwhile heading to the other examples for the AccelStepper library and loading them up to see how its functions operate.

Once you have a grasp of these examples it is also a good idea to run through the documentation of the accelStepper class  and multiStepper class so you are aware of the functions available to you as you begin programming. As the multiStepper name indicates this class is for driving multiple steppers in a coordinated fashion.