Stepper motor feedback with optical encoder

What I'm trying to achieve is to control a stepper motor with user defined number of steps, through the use of an optical encoder that is mechanically linked to the stepper motor.

The motor should rotate until the encoder reads the number of steps is equal to the user defined number of steps

I'm aware that stepper motors, if run within rated values should not slip and thus do not need feedback - even for precise stepping

I've written some simple code, speed is not an issue - thus pulsing outpin directly in a while loop- but it seems to be flawed

Looking forward to a reply

The code

//#define ENCODER_OPTIMIZE_INTERRUPTS
#include <Encoder.h>

Encoder knobLeft(2, 3);

int counter=0;
int stepPin=5;
int dirPin=4;
int rev=1000;

void setup() {
Serial.begin(115200);

pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, LOW);

}

long positionLeft = -999;

void loop() {
long newLeft;
newLeft = knobLeft.read();

if (newLeft != positionLeft ) {
// Serial.print("Left = ");
//Serial.print(newLeft); //Serial.println();
positionLeft = newLeft;
counter++;

}

while(rev>counter){
digitalWrite(stepPin,HIGH);
delayMicroseconds(750);
digitalWrite(stepPin,LOW);
delayMicroseconds(750);

}

}

"it seems to be flawed" does not give any idea of how the program actually behaves or what you would like it to do.

What happens if you make your stepper move very much slower - like 2 steps every second?

I find it easier to manage the step timing like this

digitalWrite(stepPin,HIGH);
 delayMicroseconds(10);
 digitalWrite(stepPin,LOW);
 delayMicroseconds(1500);

with all the step interval in one place.

For a practical program delayMicroseconds() is unsuitable beacuse it blocks the Arduino from doing other stuff - such as reading the encoder. See the second example in this Simple Stepper Code

Actually, that may be your problem. You are using the blocking WHILE when you probably should move the motor just one step between readings of the encoder.

...R

Hi, thanks for the reply

Sorry, my problem description seems to be flawed :slight_smile:

When the encoder turns the full number of specified steps, the motor simply slows down slightly and then continues to rotate.

If I rotate the encoder by hand, whilst the motor is rotating, a full of rotation which should stop the motor, the motor again simply slows down when the encoder is turning and then when the I stop rotating the encoder it speeds up again and continues to run.

It seems as though the encoder is interrupting the pulsing of the motor, hence the decrease in motor speed.

I will try implement your method to delay the pulsing of the motor

Thanks for your insight

Gerard

When you have revised your program post the latest version if you need more help.

...R

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

How many steps is the stepper, link to specifications, please.
What stepper controller are you using, link to specifications, please.
How many step is the encoder, link to specifications, please.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

WHY???
You have the project infront of you, we don't.
You know how it is wired, we don't.
You know how you are powering the servo, contoller and arduino, we don't.

A picture, a circuit diagram and a sketch is worth a thousand words and can save a thousand ill informed posts.

Thanks .. Tom... :slight_smile: