I2C

See this web page for I2C protocol:

http://howtomechatronics.com/tutorials/arduino/how-i2c-communication-works-and-how-to-use-it-with-arduino/

This web site is nice - it shows a clear timing diagram with explanation of how the I2C protocol works.

Here's a page for using a Perl library on the Pi:

http://search.cpan.org/~svolkov/Device-I2C-0.05/lib/Device/I2C.pm

https://metacpan.org/pod/RPi::I2C

See also this WIKIPEDIA page on I2C:

https://en.wikipedia.org/wiki/I%C2%B2C

The above page show psuedo C code for "bit-banging" I2C on the Pi, to talk to an Arduino.

NOTE: The Pi perl library may be "too fast" to work with the Arduino!! May need to at least tweak the settings in the library, or possibly write "bit banging" code (see above example) to slow things down.

NOTE: Interesting suggestion for dealing with voltage level shifting between Pi and Arduino:

The Pi GPIO pins are at 3.3V level, the Arduino is at 5V level.

The Arduino will handle 3V input levels just fine. The issue is in limiting the output to only 3V.

One option (that *seems* to work??) is to pull the data pin up to the 3.3V reference voltage provided on the Arduino, set the "digitalWrite" value of the pin to "LOW", and then use the pinMode command to select "INPUT" (to get a "high" level, since the pin will float when in Input mode), or select "OUTPUT" to drive the LOW value out to the pin.

Arduino sketch: ard2pi_i2c

Arduino pins:

D2: idat

D3: iclk

Pi script: ~/scripts/i2ctest

19: idat

26: iclk

Protocol:

    • Start Bit - idat goes low while iclk is still high. iclk then goes low

    • 8 bits formed by (7) address bits + R/Wr* (read=1, write=0) - from master's perspective.

      • MSB put on idat

      • iclk taken high, pause, taken low

    • For ACK bit, SLAVE device will pull idat low. Master samples while iclk is high. If idat is high, it is a NACK

    • Stop Bit - after ACK bit, idat is held low. iclk goes high. idat is brought high (released)

    • General case:

      • Start bit

      • 8 address and r/w bits

      • ack bit

      • 8 data bits

      • ack bit

      • stop bit