High Performance Encoder Library and I2C Not Compatible?

Hello, we've been so confused with this issue we've been having and nobody knows what's going on.

We are (required) to use the high performance Arduino encoder library from here to read a pair of 2 phase encoders on some DC motors for speed estimation.

We also are using the I2C library in order to communicate with some sensors we have / other arduinos.

We've tried this with a Mega 2560.

In our main loop, we are just reading the encoder values using the library methods and it all works great and we are able to estimate the speed.

Separately, we can use I2C to successfully talk to other Arduinos or to our sensors.

The problem occurs when we try to do both at the same time. It is the same code from the 2 separately working cases, just put together in the same main loop doing simple things. When we have I2C hooked up, the encoder values do not change and we get a speed of 0. As soon as we disconnect either or both of the SCL/SDA pins we suddenly get working encoders/speeds again (via Serial). As soon as we reconnect, the encoders die again. We also seem to get garbage from I2C when running with the encoders.

We are not putting our encoder pins on the Mega SDA/SCL pins. We have tried full interrupt, half interrupt, and no interrupts on the encoder pins (2/2 are on interrupt pins, 1/2 are on interrupt pins, 0 are on interrupt pins) since the library supports this functionality. This results in no change.

We believe there is some sort of blocking going on between the two systems, but that doesn't really make sense since the encoder ISR's shouldn't be taking that much time nor the I2C ISR's.

Has anyone had experience with this issue before?

I am having just about the same issue as you. Have you figured out a solution to this in the past 3 years? -Trev

I am having just about the same issue as you.

But like the OP you neither provide a wiring diagram nor the code you used.

My guess is that you connected the encoder to pin 20/21 which are the SDA/SCL pins but that’s wild guessing without any useful information.

I2C uses timer interrupts and so does the encoder library. You probably have an interrupt conflict. You can resolve the conflict by converting to SPI if the sensors can accept it. I try to avoid I2C because of this.

charliesixpack: I2C uses timer interrupts and so does the encoder library. You probably have an interrupt conflict. You can resolve the conflict by converting to SPI if the sensors can accept it. I try to avoid I2C because of this.

Briefly glanced to I2C library code (Wire.c Wire.h twi.c twi.h) and haven't found ant timer interrupt use. Am I missing something?

I managed to get this to work, thanks to help in my other thread: http://forum.arduino.cc/index.php?topic=600422.msg4076528#msg4076528

Basically, I was calling

encoder.read()

within the i2c sending function, and something within those two libraries clashed.

When I moved the encoder read function out of the i2c sending function and into the main loop,

void loop(){
if(encoderPosition != encoder.read()) encoderposition = encoder.read();
}

The two arduinos communicated as intended.

See the other thread for the full code.

-Trev

I2C uses timer interrupts and so does the encoder library.

I2C doesn't use timer interrupts but it uses interrupts.

within the i2c sending function, and something within those two libraries clashed.

Not the libaries clashed but you produced an endless loop because you called a function that depends on interrupts inside an interrupt handler. In interrupt context interrupts are disabled so you must not call any code that expect interrupts to happen.

If you posted your code we might have seen that almost immediately.