Saturday, January 24, 2009

Driving Stepper Motors with the Arduino


Took apart a junk scanner a while ago and have just been getting back around to tinkering with it. I have the stepper motor wired to a arduino through a UNL2803(for running higher amperage than the arduino can). This is meant to be learning exercise with the arduino with the end goal of doing something useful with it all. I wrote a program to run the scanner with forward or reverse buttons using the stepper motor library functions.

The next step will be to control the steppers with 2 pins, one for step and the other for step dir so I can connect it to a parallel port to drive it with a PC using emc2.



This is the program, I will put up a schematic or photo closeup of the wiring.

/* Stepper motor control with 2 buttons
* to run motor forward or reverse
*
*
* Written by Lee Cavanaugh 01/24/09
* Rev 0.2 - Clean up code and remove junk
* Rev 0.1 - Minimal working version.
*
*
*/
#include
//run stepper motor
#define STEPS 100
int forwardButton=5; //digital input for the forward switch
int reverseButton=6; //digital input for the reverse switch
int forward=0; //reading from the forward button
int reverse=0; //reading from the reverse button
int LedPin=13; //pin for lighting led
int steppermotorpin1=7; //pin 1 of stepper motor
int steppermotorpin2=2; //pin 2 of stepper motor
int steppermotorpin3=3; //pin 3 of stepper motor
int steppermotorpin4=4; //pin 4 of stepper motor
int steppermotorspeed=300; //set stepper motor speed
//Creates "stepper class" to drive a stepper motor.
//Set the pins to connect the motor wires to on the Arduino.
//STEPS sets how many "steps" per revolution from motor physical design.
Stepper stepper (STEPS,steppermotorpin1,steppermotorpin2,steppermotorpin3,steppermotorpin4);

void setup()
{
//Configure the serial connection pass out debugging information
Serial.begin(9600);
//set the speed of the motor in rpm through variable "steppermotorspeed" as set in setup
stepper.setSpeed(steppermotorspeed);
//configure digital inputs for the switches
pinMode(forwardButton, INPUT); //input button to control forward running of the stepper motor
pinMode(reverseButton, INPUT); //input button to control reverse running of the stepper motor
pinMode(7,OUTPUT); //stepper motor pin 1, this is used to shut off the output
pinMode(2,OUTPUT); //stepper motor pin 2, this is used to shut off the output
pinMode(3,OUTPUT); //stepper motor pin 3, this is used to shut off the output
pinMode(4,OUTPUT); //stepper motor pin 4, this is used to shut off the output
}

void loop()
{
forward = digitalRead(forwardButton);
reverse = digitalRead(reverseButton);
//drives the stepper motor forward or reverse when those buttons are pressed individually
if (forward==1 && reverse==0) stepper.step(forward*10);
if (forward==0 && reverse==1) stepper.step(-reverse*10);
//Shuts off the pin after the stepper.step command runs. Otherwise leds stay lit indicating
//current still flows to some of the pins between moves, locks stepper but to conserve energy shut them off.
digitalWrite(steppermotorpin1,0); //Shuts pin off, this shuts power to stepper off, but stepper is not locked in position.
digitalWrite(steppermotorpin2,0); //Shuts pin off, this shuts power to stepper off, but stepper is not locked in position.
digitalWrite(steppermotorpin3,0); //Shuts pin off, this shuts power to stepper off, but stepper is not locked in position.
digitalWrite(steppermotorpin4,0); //Shuts pin off, debugging leds stay on unless I do this.
}

No comments: