I want to share some hints for using the Arduino's TwoWire (aka I2C) interface:
First, make sure you have wired up the two cables (SDA and SCL) to the analog pins four and five respectively. (It was quite embarrassing finding this out after some time on a super-expensive logic analyzer..)
Then, there is a twist with regards to slave addresses: I2C uses 7 bits for adressing and the following bit signals a read (1) or a write (0) operation. While in most documentations of peripherals you'll find the slave address given in the way use 0xC0 for writing and 0xC1 for reading, the stock Arduino library expects the address to be given as 7-bit value (hence: 0x60) and does the left-shifting and appending of the read-bit itself. This is, however, not obvious unless you read the library's sources..
Finally, the library is not really good in returning hints about what's going on in the communcation process back to the Arduino sketch, which would be a good thing to have when something just doesn't work right.
I quickly fixed these two issues for my own work, if anyone want to give it a try: zip or patch.
This changes the address behaviour and implements getError(), which returns the mask of the IRQ that signaled something going wrong (like not receiving an ACK) from the slave - or 0xFF in case of success. Similar, getErrorString() returns an error string or ACK.
If you want to monitor the success of a master transfer, you have to put it after the call to endTransmission() as the library doesn't do any communication over the wire before that.
(Thanks for gordo and stock for helping me track down this bastard!)
title arduino vs. i2c
tag arduino
tag i2c
tag debugging
tag patch
tag research
written 23.03.2008 18.42