!!Stepper Library
This library allows you to control unipolar or bipolar stepper motors. To use it you will need a stepper motor, and the appropriate hardware to control it. For more on that, see [[http://www.tigoe.net/pcomp/code/archives/picbasic_pro/000245.shtml| Tom Igoe's notes on steppers]].
'''Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2);''' - initialize a Stepper for two-wire operation.
, e.g.
[@
Stepper myStepper(200, 8,9); // Initialize the Stepper library for a 200-step stepper using pins 8 and 9
@]
'''Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2int motor_pin_3, int motor_pin_4);''' - initialize a Stepper for four-wire operation.
, e.g.
[@
Stepper myStepper(100, 8,9,10,11); // Initialize the Stepper library for a 100-step stepper
//using pins 8 through 11
@]
To determine the number of steps your motor uses, read the label or data sheet. It might tell you the step degrees. For example, a 1.8-degree stepper takes 200 steps per revolution (1.8 * 200 = 360 degrees).
'''void setSpeed(long whatSpeed);''' - set the Stepper's speed, in revolutions per minute
e.g.
[@
myStepper.setSpeed(60); // 60 rpms
@]
'''void step(int number_of_steps);''' - move the motor a number of steps. Direction depends on whether the number is negative or positive.
e.g.
[@
myStepper.step(100); // moves the stepper 100 steps forward
myStepper.step(-100); // moves the stepper 100 steps backward
@]
'''Note:''' step() is a blocking function. This means that it stops all other code from running while it's completing the steps you asked for. For example, if you set the speed to be, say 1 RPM, and then called step(100) on a 100-step stepper, the function would take a full minute to run. To get better interactive control, step only short amounts, and keep the RPMs high.
'''int version();''' - return the version number of the library
e.g.
[@
Serial.println(myStepper.version()); // prints the version number
@]
----
Example circuits:
Unipolar Stepper:
%height=305px width=550px% Attach:unipolar_stepper.png
Bipolar Stepper:
%height=416px width=550px% Attach:bipolar_stepper.png
----
Example program:
[@
/*
Stepper Motor Controller
language: Wiring/Arduino
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 and 9 of the Arduino.
The motor moves 100 steps in one direction, then 100 in the other.
Created 11 Mar. 2007
Modified 7 Apr. 2007
by Tom Igoe
*/
// define the pins that the motor is attached to. You can use
// any digital I/O pins.
#include <Stepper.h>
#define motorSteps 200
#define motorPin1 8
#define motorPin2 9
#define ledPin 13
// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2);
void setup() {
// set the motor speed at 60 RPMS:
myStepper.setSpeed(60);
// Initialize the Serial port:
Serial.begin(9600);
// set up the LED pin:
pinMode(ledPin, OUTPUT);
// blink the LED:
blink(3);
}
void loop() {
// Step forward 100 steps:
Serial.println("Forward");
myStepper.step(100);
delay(500);
// Step backward 100 steps:
Serial.println("Backward");
myStepper.step(-100);
delay(500);
}
// Blink the reset LED:
void blink(int howManyTimes) {
int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
}
@]
----
If anyone's interested in helping to develop this library further, please contact me at tom.igoe at gmail.com