Issues Reading an encoder

Tried adding this piece of code

noInterrupts();myCount=encoderCount; interrupts();

Tried putting it inside the readEncoder functions after the encoderCount update. It didn't seem to make any difference.

Have you tested the DUE's input pins with pushbuttons to see if they are working? Do you have another MCU board to test the encoder?

I haven't, but it would be difficult to question the input pins being functioning, since I do get the encoder readings in a fair enough manner, it is just that there are missed pulses which is not adequate for my application of precise distance measurement.
With the latest code I posted, there are on average 3 missed encoderCount pulses for every 4 revolutions of the of the encoder. This is pretty much consistent and only in one direction.

I have an Arduino Nano Every somewhere, but since it requires soldering I haven't opted to try it instead.

I still wonder about the 4.8V encoder power, datasheet says 5V minimum, and the 5,9V you saw from the encoder outputs is worrisome. Do you have a separate 5V power supply for the encoder? (Don't forget to connect grounds).

Yes, the 5.9V(no load - when connected to the Arduino it measures around 3.4V, I think) is very odd to me as well. Think I will redo the measurements, although I was very careful doing them last time and repeated them several times to confirm.

I can arrange a separate 5V power supply. Don't you think that 4.8V can be considered good enough for most practical applications requiring nominal 5V?

Do you believe the code has no issues?

Can't say since I'm not familiar with non AVR Arduinos but I did expect my little test code to work.

Maybe you could try one of the Arduino encoder library example sketches like the Paul Stoffengren library.

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(5, 6);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}
1 Like

I believe your problem may be with the encoder's output IC type, read the page "50 | Notes and Tables" in the datasheet ( post # 19). If the cable has 2 unused "complementary" channel wires, what are you doing with them? Maybe they should be pulled to ground with pulldown resistors.

Funny, had not seen this encoder library (or any similar for this matter). Anyway, uploaded and tested this code and interestingly enough for me, while probably not surpassingly for you, it did behave similarly as my code.

The datasheet doesn't specify anything for the case when only 1 pole of the differential signals is used. Having used differential encoder in the past I did not foresee any issues using the encoder signals in such a manner. It is a TTL signal output , so should have no difference if it's being fed into other device or not. This is how I understand it anyway.

An update:

After countless hours of looking for the cause of the inconsistent encoder reading, the reason has been found and eliminated. The reason was a slipping pulley!

Thanks to everyone for helping!

Seems you were right Dave for suspecting a mechanical issue.

1 Like

Thank you for pointing to this library. I am using it and the code is so much cleaner!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.