Which I2C command happen during
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)?
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.Sign up to join this community
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.
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().
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; Wire.readBytes(buff, 2); // read value of the 'result' register