11

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
}
Mads Skjern
  • 1,145
  • 3
  • 13
  • 23

1 Answers1

11

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.

Chris Stratton
  • 5,411
  • 20
  • 40