Using an optical rotary encoder to move a stepper motor

Hi.

My first post, so my apologies if i done something incorrectly (I did read the sticky)

I have a project where i've used code based on the sketch below to move stepper motors with 600 PPR optical encoders. (the encoders are similar to this: https://www.amazon.ca/Encoder-Photoelectric-Incremental-Rotary-2-Phases/dp/B07JM9YRTQ/ref=sr_1_2?dchild=1&keywords=600+ppr+encoder&qid=1634762153&sr=8-2).

The project is using STSPIN820 drivers from pololu. This driver allows full-step, half-step, 1/4-step, 1/8-step, 1/16-step, 1/32-step, 1/128-step, and 1/256-step.

Everything works fine, except I wish I used 50 or 100 PPR encoders. The 600 PPR steppers move the steppers too fast unless I'm microstepping 1/8 or slower. I could even use speeds slower than 1/256.

Is there a simple way to change the script to ignore a given number of pulses from the 600 PPR ROE, to effectively make it a 50 or 100 PPR roe? This is less expensive than buying newer encoders for the project.

thanks in advance

/*******Interrupt-based Rotary Encoder Sketch*******
by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
*/

const int pinA = 2;     // Our first hardware interrupt pin is digital pin 2
const int pinB = 3;     // Our second hardware interrupt pin is digital pin 3
const int stepPin = 10;  // pin for pulsing a step to the stepper driver
const int dirPin = 9;   // pin for setting stepper driver direction
const bool CW = 0;      // clockwise rotation direction (viewed from encoder body out toward shaft--like CNC Machines)
const bool CCW = 1;     // counter-clockwise rotation direction (see above, these directions appear reverse when viewed from shaft side)
//#define SLEEP 12     // Pin 12 connected to SLEEP pin

volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent
volatile long encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile int encoderPosOld = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile bool encoderDir = CW; // stores the direction of rotation clockwise = 0 or counter-clockwise = 1

void setup() { 
  pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  Serial.begin(115200); // start the serial monitor link
 //  pinMode(SLEEP, OUTPUT);   
//   digitalWrite(SLEEP, HIGH);  // Wake up EasyDriver
}

void PinA() {
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos --; //decrement the encoder's position count
    encoderDir = CW;

    digitalWrite(dirPin, encoderDir);
    digitalWrite(stepPin, HIGH);
    //delayMicroseconds(5);
    digitalWrite(stepPin, LOW);
    
    if (encoderPos < -2147483648L) {
      encoderPos = 0L;
    }
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void PinB() {
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos ++; //increment the encoder's position count
    encoderDir = CCW;
    
    digitalWrite(dirPin, encoderDir);
    digitalWrite(stepPin, HIGH);
    //delayMicroseconds(5);
    digitalWrite(stepPin, LOW);
    
    if (encoderPos > 2147483647L) {
      encoderPos = 0L;
    }
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void loop() {
  if(encoderPosOld != encoderPos) {
    Serial.print(encoderPos);
    if(encoderDir == CW) Serial.println(" CW");
    if(encoderDir == CCW) Serial.println(" CCW");
    encoderPosOld = encoderPos;
 //    digitalWrite(SLEEP, LOW);  // Wake up EasyDriver
  }
}```

Sure. After the first pulse, count up the pulses to what ever number you want. If the count is NOT what you want, DON'T move the stepper. When the count reaches your magic number, reset the count and let the stepper move.

Paul

Do you want the stepper to turn one turn for one turn of the encoder?

JCA34F: Thanks for your question. Currently with full stepping, one revolution of the encoder will yield 3 revolutions of the stepper. The project uses the pololu STSPIN820 driver, and a selector button that I can use to choose between full-step, half-step, 1/4-step, 1/8-step, 1/16-step, 1/32-step, 1/128-step, and 1/256-step (I did not include this aspect of the code, as i did not want to include code that might seem extraneous).

So when i use the device, the full step, half step, and 1/4 step settings are almost useless, as they spin the stepper too fast, causing vibration. I did not realize i could simply put a counting function within the interrupt as suggested above.

Just insert a counter inside your PinA and PinB routines to count the number of pulses before you increment your encoder variable.

/*******Interrupt-based Rotary Encoder Sketch*******
  by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
*/

const int pinA = 2;     // Our first hardware interrupt pin is digital pin 2
const int pinB = 3;     // Our second hardware interrupt pin is digital pin 3
const int stepPin = 10;  // pin for pulsing a step to the stepper driver
const int dirPin = 9;   // pin for setting stepper driver direction
const bool CW = 0;      // clockwise rotation direction (viewed from encoder body out toward shaft--like CNC Machines)
const bool CCW = 1;     // counter-clockwise rotation direction (see above, these directions appear reverse when viewed from shaft side)
//#define SLEEP 12     // Pin 12 connected to SLEEP pin

volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile unsigned long encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile byte stepCount;
const byte maxStepCount = 4; // scale encoder pulses by /4
volatile bool encoderDir = CW; // stores the direction of rotation clockwise = 0 or counter-clockwise = 1

unsigned long encoderPosOld = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)

void setup() {
  pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  attachInterrupt(0, PinA, RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1, PinB, RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  Serial.begin(115200); // start the serial monitor link
  //  pinMode(SLEEP, OUTPUT);
  //   digitalWrite(SLEEP, HIGH);  // Wake up EasyDriver
}

void PinA() {
  byte reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    stepCount = (stepCount - 1) % maxStepCount;
    if ( stepCount == 0 ) {
      encoderPosRaw--; //decrement the encoder's position count
      encoderDir = CW;

      digitalWrite(dirPin, encoderDir);
      digitalWrite(stepPin, HIGH);
      //delayMicroseconds(5);
      digitalWrite(stepPin, LOW);
    }
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
}

void PinB() {
  byte reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    stepCount = (stepCount + 1) % maxStepCount;
    if ( stepCount == 0 ) {
      encoderPos ++; //increment the encoder's position count
      encoderDir = CCW;

      digitalWrite(dirPin, encoderDir);
      digitalWrite(stepPin, HIGH);
      //delayMicroseconds(5);
      digitalWrite(stepPin, LOW);
    }
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
}

void loop() {
  noInterrupts();
  unsigned long encoderPosNew = encoderPos;
  interrupts();

  if (encoderPosOld != encoderPosNew) {
    Serial.print(encoderPosNew);
    if (encoderDir == CW) Serial.println(" CW");
    if (encoderDir == CCW) Serial.println(" CCW");
    encoderPosOld = encoderPosNew;
    //    digitalWrite(SLEEP, LOW);  // Wake up EasyDriver
  }
}

A couple of notes:

  • you don't need to disable/re-enable interrupts inside PinA and PinB, that happens automatically since these are interrupt routines.
  • your encoderPos variable should be volatile unsigned long so any wrap-around will work without any code. Your checks for if (encoderPos > 2147483647L) can never be true since that is the maximum value a 32 bit int can hold. It can never be larger than that. Same for your negative check.
  • You need to make a copy of encoderPos in your main loop since a 4 byte variable can not be accessed atomically.