Reading Hall Sensors counting wrong.


i have written a program for contorlling 2 lineair concens actuators. I included the library Encoder.h for reading the encoder pulses, but the problem is that the counting of the pulses is going wrong. For example, i start my code with going to a switch to set the value of the encoder to 0. Then i move up to a encoder value of 40, after this i move down back to the switch, so what I normaly hope by reaching the switch is that the value of the encoder is zero again. But the encoder value is not 0 but its 7. So there is difference between counting by going up and going down. in addition, the value can also differ slightly each time the actuator goes up or down.

does anybody know what the problem can be by counting encoder (roratry encoder / hall sensor ) values.

The code is shown below

Pur_VulMachine1-2-2019.ino (13.4 KB)

are your encoders just rotary knobs, or attached to motor shafts spinning quickly?

if you try the demo example, what do you see in the console:

/* Encoder Library - TwoKnobs Example
 * This example code is in the public domain.

#include <Encoder.h>

// Change these pin 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
//   avoid using pins with LEDs attached

Encoder knobLeft(18,19);
Encoder knobRight(20,21);

void setup() {
  Serial.println("TwoKnobs Encoder Test:");

long positionLeft  = -999;
long positionRight = -999;

void loop() {
  long newLeft, newRight;
  newLeft =;
  newRight =;
  if (newLeft != positionLeft || newRight != positionRight) {
    Serial.print("Left = ");
    Serial.print(", Right = ");
    positionLeft = newLeft;
    positionRight = newRight;
  // if a character is sent from the serial monitor,
  // reset both back to zero.
  if (Serial.available()) {;
    Serial.println("Reset both knobs to zero");

Have you written a very simple test program for the homing routine and specified move? You may have mechanical issues of switch make/break positional differences and mechanical backlash in the actuators.