6

I am reading about I2C. On this site:

http://playground.arduino.cc/Main/WireLibraryDetailedReference#endTransmission

It says that endTransmission() can return one of the following status codes:

  • 0: Successful send.
  • 1: Send buffer too large for the twi buffer. This should not happen, as the TWI buffer length set in twi.h is equivalent to the send buffer length set in Wire.h.
  • 2: Address was sent and a NACK received. This is an issue, and the master should send a STOP condition.
  • 3: Data was sent and a NACK received. This means the slave has no more to send. The master can send a STOP condition, or a repeated START. 4: Another twi error took place (eg, the master lost bus arbitration).

If I attach nothing to my Arduino (or with pull-up resistors to both SDA/SCL), I always get status 2. But how can a NACK (or anything) be received when there is nothing to communicate with? Does it mean something else?

Here is my example code

#include "Wire.h"
void setup() {
  Serial.begin(9600);
  Wire.begin();
}
void loop()  {
  Wire.beginTransmission(42);
  Wire.write(0);
  byte status = Wire.endTransmission();
  Serial.println(status); // always prints 2
}
7

A NACK is signaled by an acknowledgement slot in which SDA remains high while SCL cycles under control of the master.

Since high is the un-driven state of the pulled-up bus, in the absence of a peripheral at the selected address to positively acknowledge by pulling it down, a NACK condition will passively result.

  • Could you please elaborate more? You mean that peripheral is not connected? How about was able to get data for a while, then it stopped working and keep return 0x02? – Dzung Nguyen May 19 '17 at 8:20
  • 2
    Anything that means a peripheral fails to respond can cause this - no peripheral, broken traces or wires, wrong address, noise making the address look wrong, weak pullups distorting the data, absurdly strong pullups distorting the signalling, bad power, buggy software or silicon design in the peripheral, even mismatching support of extensions like clock stretching. – Chris Stratton May 19 '17 at 14:18

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.