Stepper Motor 3-Channel Encoder.

I have an incremental 3-Channel Encoder A,A/,B,B/,I,I/.

Encoder Specs

I am using this code:

/* Read Quadrature Encoder
   Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.

   Sketch by max wolf / www.meso.net
   v. 0.1 - very basic functions - mw 20061220

*/

int val;
int encoder0PinA = 2;
int encoder0PinB = 3;
int encoder0PinC = 4;
int encoder0PinD = 5;
int encoder0PinE = 6;
int encoder0PinF = 7;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int encoder0PinCLast= LOW;
int encoder0PinELast= LOW;
int n = LOW;
int m = LOW;
int x = LOW;

void setup() {
  pinMode (encoder0PinA, INPUT);
  pinMode (encoder0PinB, INPUT);
  pinMode(encoder0PinC, INPUT);
  pinMode(encoder0PinD, INPUT);
  pinMode(encoder0PinE, INPUT);
  pinMode(encoder0PinF, INPUT);
  Serial.begin (9600);
}

void loop() {
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
    } else {
      encoder0Pos++;
    }
    Serial.print (encoder0Pos);
  }
  encoder0PinALast = n;{
    //----------------------------------------------------------------------------------------------------------
  }
m = digitalRead(encoder0PinC);
 if ((encoder0PinCLast==LOW) && (m==HIGH)){
  if (digitalRead(encoder0PinD) == LOW){
    encoder0Pos--;
  }else {
    encoder0Pos++;
  }
  Serial.print (encoder0Pos);
  }
  encoder0PinCLast=m;{
    //----------------------------------------------------------------------------------------------------------------
  }
    x = digitalRead(encoder0PinC);
 if ((encoder0PinELast==LOW) && (x==HIGH)){
  if (digitalRead(encoder0PinF) == LOW){
    encoder0Pos--;
  }else {
    encoder0Pos++;
  }
  Serial.print (encoder0Pos);
  }
  encoder0PinELast=x;{
  }}

The problem is when I open the serial Monitor the values are all negative no matter the direction of the stepper motor. What am I doing wrong?

Post a wiring diagram (anything but Fritzing).

Which type do you have? WEDS, single ended, open collector output, or WEDL, line driver output? Do you really need the I (index) channel? How fast will the encoder be turning (RPM)?

Use INPUT_PULLUP mode for encoder inputs, they are often open-collector, although because this is a differential unit that might not be the case, but why not make the code general?

Why do you have 3 copies of the code reading 3 different sets of pins?

I think you've been confused by the differentlal outputs.

For one encoder you only read A and B, ignore all the others, two input pins, one copy of the code to detect changes. A/ and B/ is simply an inverted copes of A & B, there to provide better noise immunity in an industrial environment.

The I output is index pulse, unless you are going to home the system using it, ignore it.

outsider:
Which type do you have? WEDS, single ended, open collector output, or WEDL, line driver output? Do you really need the I (index) channel?
How fast will the encoder be turning (RPM)?

Line driver with a RS422 protocol.
I just thought I will use all the channels for more accurate readings? I will look into what is an index channel.
The RPM is variable depending on each situation, how is it interfering with the encoder?

MarkT:
Use INPUT_PULLUP mode for encoder inputs, they are often open-collector, although because this
is a differential unit that might not be the case, but why not make the code general?

The I output is index pulse, unless you are going to home the system using it, ignore it.

I might need homing in the future, but I believe the code is more accurate with A,B and A/B/ channels.

jremington:
Post a wiring diagram (anything but Fritzing).

Hi,

Can you please tell us your electronics, programming, Arduino, hardware experience?

I cannot see where RS422 is mentioned.

Can you tell us what is the exact part number of your encoder please?

Do you have a DMM?

Can I suggest you reload the encoder code from the playground and just concentrate on inputs A and B, ignore /A and /B and I and /I.

Just use Arduino pins 2 and 4 that you have A and B connected to.

Tom… :slight_smile: