I have an Arduino due, I am trying to keep track of ticks on two wheel encoders, polling in an infinite loop results in terrible performance. Have any suggestions?

It looks like the due might have an internal counter but I don't know how to use it, is there a library?

  • 3
    please add your code to the post ... the performance issue may be caused by bad code
    – jsotola
    Jun 27 at 23:35

You can use an interrupt handler to measure frequency. Works quite well.

volatile int32_t _ticks = 0;
public void FrequencyIsr()

// Add this to setup()
// Mode can be FALLING, RISING or CHANGE
attachInterrupt(digitalPinToInterrupt(pin), FrequencyIsr, mode);

// Add this to loop()
int32_t currentTime = millis();
// Clear the interrupt flag, so that we can read out the counter
int32_t ticks = _ticks;
// and then calculate the amount of ticks since the last update (if you need the speed)
int32_t timeDelta = currentTime - _lastTime;
int32_t tickDelta = ticks - _lastTicks;
double ticksPerMs = (double)tickDelta/timeDelta;
_lastTime = currentTime;
_lastTicks = ticks;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.