Stepper motor control with rotary encoder

I am designing a remote electronic focus control for a telescope (to avoid having to touch telescope during focusing causing wobble).

I intended to do this with an arduino nano connected to a stepper motor and using a rotary encoder to simply translate a turn on encoder to a turn on the stepper motor (with adjustments for speed to follow).

However I am having a frustrating problem when combining both. If I use code for rotary encoder on it's own just outputting to serial console it appears to work fine albeit a click jumps up 4 positions. but turning clockwise it will show counting up, anti-clockwise counting down.

As soon as I add the code to actually move the stepper motor it goes to hell. I have tried multiple different examples and best I get is it works in one direction, this method uses the Stepper class from libary and does not work in either direction.

If I remove the myStepper.set(newpos-curpos) line it shows as it should in console examples...

Moving 2 clicks clockwise...

Current Position: 0 New Position: 100
Current Position: 100 New Position: 100
Current Position: 200 New Position: 100
Current Position: 300 New Position: 100
Current Position: 400 New Position: 100
Current Position: 500 New Position: 100
Current Position: 600 New Position: 100
Current Position: 700 New Position: 100

Adding the stepper line, moving 2 clicks clockwise gives me...

Current Position: 0 New Position: 100
Current Position: 100 New Position: -100
Current Position: 0 New Position: 100
Current Position: 100 New Position: -100

Finally if I turn the encoder 1/2 a click at a time slowly it reacts as expected...

Current Position: 0 New Position: 100
Current Position: 100 New Position: 100
Current Position: 200 New Position: 100
Current Position: 300 New Position: 100

(New position is number of steps motor should be turning)

This happens with all methods I've tried where one way will turn fine, the other it will bounce (e.g. think its been turned one click forward and then one backwards).

Can anyone see what I've done wrong?

The rotary encoder I am using is this one... https://www.lelong.com.my/rotary-encoder-arduino-robotic-respberry-littlecraft-175999578-2019-04-Sale-P.htm

(EDIT: Just noticed some old code from a different method left in, removed this and didn't have any effect, updated below to remove)

#include <Stepper.h>
#include <Encoder.h>

Stepper myStepper(2048, 5, 6, 7, 8);
Encoder rotEncoder(10, 11);

#define reSW 12

int steps = 100;
long rotPosition  = -999;
long counter;

int curpos = 0;
int newpos = 0;
 
void setup() {
  pinMode (reSW,INPUT);
  myStepper.setSpeed(15);
  Serial.begin(9600);
}

void loop() {
  reCheck();
  newpos = (counter * steps);
  if (newpos != curpos) {
    stepperMove();
  }
}

void reCheck() {
  counter = rotEncoder.read();
  if (counter != rotPosition) {
    rotPosition = counter;
  }
}
void stepperMove() {
  Serial.print("Current Position: ");
  Serial.print(curpos);
  Serial.print(" New Position: ");
  Serial.println(newpos-curpos);
  myStepper.step(newpos-curpos);
  curpos = newpos;
}

It looks like your stepper code may cause the motor to move several steps and during that time the Arduino cannot check the encoder for another step.

I think this is a case where it would be wise to use interrupts to capture the encoder movement in the background. Read about the attachInterrupt() function.

...R

Thanks, that was a big help getting there...

Using interupt it kind of worked, if I was gentle with the encoder it worked as expected, turning at any "normal" speed just sent it into a total wobble.

Was about to totally give up and switch over to a back/forward button design instead when I got another delivery of rotary encoders (ordered some via ebay initially but didn't notice delivery time so got some from amazon as well as wanted to get started quicker).

Both are identical to look at but thought may as well give it a try, sure enough plugged the ebay one in and works absolutely perfect with interupt sketch.

If you are focusing, why no just use two buttons to drive the stepper in different directions , why do you need the encoder ?

vrx:
Using interupt it kind of worked, if I was gentle with the encoder it worked as expected, turning at any "normal" speed just sent it into a total wobble.

As you have not posted your program it is hard to offer advice!

...R

Hello,

If I use code for rotary encoder on it's own just outputting to serial console it appears to work fine albeit a click jumps up 4 positions.

I havent try encoder with stepper motor before, but have you check this?

[u]https://www.instructables.com/id/Using-Stepper-Motor-As-Rotary-Encoder/[/u]

hammy:
If you are focusing, why no just use two buttons to drive the stepper in different directions , why do you need the encoder ?

This is a great suggestion!
I'm working on a similar project, except to tune a remote antenna. I know so little about arduino that I can't ask an intelligent question here, maybe couldn't understand the answer.

I'm thinking to eventually have 3 push buttons for forward slow medium and fast and 3 for reverse. My original set up was a dc gear motor with a forward and reverse. With arduino and a stepper motor I can know the steps and be repeatable instead of starting over every time I change frequency. thanks chris