I2C while in ISR?


I want to send data over I2C as soon as an Interrupt fires, is this possible?

Actually, I already think this is not possible since it doesn't work :D. It completely freezes the CPU (Zero).

I just found this on Gammon's Forum

You cannot use delays in an interrupt.

•Don't attempt to delay, eg: delay (100);

•You can get the time from a call to millis, however it won't increment, so don't attempt to delay by waiting for it to increase.

•Don't do serial prints (eg. Serial.println ("ISR entered"); )

•Don't try to do serial reading.

So I think the "Do not use Serial.print" is also effective as "do not use I2C" since I2C uses interrupts as well (?) :slight_smile:

Correct. Set a flag in the interrupt service routine, send the I2C message as soon as you see the flag in loop().

Depends on how your code is setup… I do it all the time in my code. But, if you’re using I2C in both interrupt handlers and “foreground” code, you will have to selectively disable interrupts in the foreground code around the I2C accesses, so an interrupt CANNOT occur in the middle of an I2C operation.

Ray L.