Issue with Incorrect Encoder Values – SparkFun Auto pHAT and Hobby Motor with Encoder

Dear Customer Service Team,

I am currently using the following products:

  • SparkFun Auto pHAT for Raspberry Pi (ROB-16328)
  • Hobby Motor with Encoder - Metal Gear (ROB-16413)

I am encountering an issue where, when reading the encoder values, I occasionally receive incorrect readings. For example, out of 50 encoder counts, I sometimes get a value like “-120” in the middle of the series, and then the values continue as expected (e.g., “50”).

As this is my first time working with encoders, I am wondering if this behavior is normal, or if I might be doing something wrong.

To mitigate the issue, I have implemented a kind of damping in my code: values that deviate by more than 15% from the previous reading are ignored. However, I am wondering if there are other, more effective approaches to handle this problem.

Has anyone experienced something similar, or can suggest ways to improve the accuracy of the encoder readings?

Please post the code, using code tags.

If you are using interrupts, that behavior is a typical result of not following the stringent rules for interrupt coding.

1 Like

I am using the code from the SparkFun Auto pHAT Hookup Guide. Here is a screenshot of the described effect.