# Advice on controlling an old stepper motor

#### zazas321

Joined Nov 29, 2015
926
Hello. I have got a task to design a control firmware for a stepper motor. It needs to rotate to one direction (close the door) when it receives signal, and rotate to another direction(open the door) when it receives another signal. I have been given some stepper motor and a driver:

I am running a driver with 12V and 0.6A current and the motor seems to rotate in both direction using this sketch:

Code:
#define directionPin 3
#define stepPin 2
#define speedDelay 40 // change this to change the speed, the lower it is the faster it will go

void setup() {
// Initialize the Serial port:
Serial.begin(9600);

// set up the driver pins:
pinMode(stepPin, OUTPUT);
pinMode(directionPin, OUTPUT);

}

void loop() {
// Step forward 800 steps:
Serial.println("Forward");
digitalWrite(directionPin, HIGH);
for (int i=0; i<100; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delay(speedDelay);
}
delay(500);
// Step backward 800 steps:
digitalWrite(directionPin, LOW);
for (int i=0; i<100; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delay(speedDelay);
}
}
Since I require to rotate this stepper motor certain distance , would you recommend me using some library or build my own functions?
I also need to make this stepper rotate in accelerating manner. I have tried using accellstepper library

Code:
// ProportionalControl.pde
// -*- mode: C++ -*-
//
// Make a single stepper follow the analog value read from a pot or whatever
// The stepper will move at a constant speed to each newly set posiiton,
// depending on the value of the pot.
//
// Copyright (C) 2012 Mike McCauley
// $Id: ProportionalControl.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem$

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 2, 3);
//direction d3
//pull d2
// Tested with a 10k linear pot between 5v and GND
#define ANALOG_IN A0

void setup()
{
stepper.setMaxSpeed(200);
stepper.setSpeed(1);
}

void loop()
{
stepper.moveTo(analog_in);
//stepper.setSpeed(1);
//stepper.set();
stepper.run();
//stepper.runSpeedToPosition();

}
I am though confused about how exactly this library works : https://www.airspayce.com/mikem/ard...tepper.html#ace236ede35f87c63d18da25810ec9736

For example, how does moveTo function works? What value do I need to pass. Since the description mentions target position, how does a number that I pass refer to target position? What would happen if I pass 100 or -100?

#### Attachments

• 806.5 KB Views: 13
• 1,018.7 KB Views: 13

Joined Feb 20, 2016
3,987
I would imagine you run the motor until it hits a limit switch. Then set that position as "home" using the "setCurrentPosition()" command.
Then, if you "moveTo 100", it should step 100 steps. Similarly, "moveTo -100" will go back to home and continue another 100 steps. That is, if your software and the hardware system allows it. Like, you cannot close the dore more that fully closed.
Make up a lever on the stepper and have it operate a microswitch then have your code set the home position.

Play around with it on the bench.

#### zazas321

Joined Nov 29, 2015
926
Okay thank you sir. How do I know if my stepper motor has a limit position? Can I rotate it in any direction any amount of times? From what I have heard, stepper motors usually can only rotate 1 or just a few cycles

Joined Feb 20, 2016
3,987
Usually, steppers can go around an unlimited number of turns, just like a "normal" motor.
Limit switches will be operated by a switch, for example, when the door is closed, so the controller will know to stop driving the motor.
Also, another switch at the open position may be a good idea.

My 3D printers all have limit switches at the home position only. The stepper motors drive to home then are stopped. Now the controller knows where the machine is.
So, the controller can now move to a series of known positions to print the work. This assumes the motors do not miss a step. The beauty of a stepper motor is that it moves a known amount for each step, so, if nothing slips or jams, you can work out where it is by the number of steps that have been moved.
EDIT: It looks like 1.8 degrees per step. See this data sheet.

#### Attachments

• 93.7 KB Views: 6
Last edited: