0

Which I2C command happen during Wire.requestFrom(address, quantity)?

Is it correct that Wire put a Start condition, then 7-bit address appended with read bit? How would that be different from Wire.write(address)?

2

There is no Wire.write(address) in the Wire library. The function write() takes data and puts it into the library internal write buffer. It does nothing on the I2C bus.

For master write operation (sending from master to slave) the following commands are necessary:

Wire.beginTransmission(address);
Wire.write(data);
Wire.endTransmission();

Only the last of these commands does something on the I2C bus. It generates a Start condition, sends the byte consisting of the address (that we set in the beginTransmission() statement) and the direction bit, which is always 0 here, because these commands are meant for master write operation. Then it sends the data and ends the transmission with a stop condition. You cannot control, which value the direction bit has. The parameter of beginTransmission() is only meant to be 7 bit long. If you provide a bigger address, the function will simply cut the highest bit and issuing a write operation anyway.

For master read operation the action on the I2C bus happens inside the requestFrom() function. It sends a Start condition, followed by the address and the direction bit set to 1 (again you cannot control the value of the direction bit here). Then it reads the bytes, that are coming from the slave until all requested bytes were received. Then you can read the bytes from the library internal buffer with the Wire.read() function. Again this function does nothing on the I2C bus. It only interacts with the libraries buffer.

|improve this answer|||||
  • When the master reads data, you say that the slave can end the transmission with a nack. Check the i2c protocol please. – Jot Jul 30 '18 at 15:50
  • @Jot Mhh, seems that you are right. This is not mentioned in the protocol. But I don't understand what happens, when the slave doesn't have enough bytes to send. Are the remaining bytes all read as 0xFF, since the slave doesn't drive the SDA line to low anymore? – chrisl Jul 30 '18 at 16:19
  • Yes, that is what will happen. The master does not receive less bytes but also not more bytes than requested. As long as there is no error on the bus, the master gets the amount of bytes that it wants, regardless how many bytes the slave tries to send. – Jot Jul 30 '18 at 18:35
0

When you send an address with write(), it is a register address on the device, not the device address on the bus.

The device address is set with beginTransmition() and requestFrom().

If the device requires to set the register before requestFrom(), the register address should be sent with write() wrapped in beginTransmition() and endTransmition().

Example:

Wire.beginTransmission(I2C_ADC121);
Wire.write(REG_ADDR_RESULT); // send register to read, the 'result' register
Wire.endTransmission();
Wire.requestFrom(I2C_ADC121, 2);
byte buff[2];
Wire.readBytes(buff, 2); // read value of the 'result' register
|improve this answer|||||

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.