onRequest handler and Serial.print

I was successful in getting an NXT (using Robotc) and an Arduino to talk to each other over I2C but there was a problem I hit that I did not expect.

I found that if I included Serial.print statements in my onRequest handler the data being sent to the NXT master would be "mangled". I have confirmed this with a Saleae logic analyzer and can easily reproduce this problem.

It appears others 'might' have had this issue. When I searched the forum there were some unanswered posts from people who were having similar problems (for example http://arduino.cc/forum/index.php/topic,90516.0.html ).

Is it possible to update the Wire.onRequest library documentation page to warn people about this issue? (Or is this common knowledge and I just didn't know about it? :grin: )


The onRequest handler is (called from) an ISR (interrupt service routine). It is not recommended to do Serial.prints inside those in general, and in particular with I2C because you will delay the response possibly causing the transaction to time out.

You are probably right that this could be better documented.

Answered the query in the other thread, somehow that one slipped through. :)

It's funny (actually it isn't) but I know very well NOT to put Serial.print calls in an ISR and yet... I did it anyway because my head was not quite with it. :blush: