By Jeff Gray, February 2008

This is an example using the Wire library in Arduino to utilize the i2c protocol for sending and receiving data from another device (including sensors).

Note, the pins on the Arduino for this are as follows:

SDA (data) pin Analog Pin 4

SCK (clock) pin Analog Pin 5

Also, you need to use pull up resistors from the SDA and SCK pins, meaning you tie a resistor (preferably around 2-4K ohm) from that pin to the voltage rating of the device you're communicating with (in this case, 5V)

Arduino Code

/*
 * COMPASS MODULE i2c Example 
 * CMP103 - http://www.robot-electronics.co.uk/htm/cmps3doc.shtml
 */

#include <Wire.h> 


void setup() 
{ 
  // this assumes the i2c devices you are using are connected 
  // to analog pin 4 (SDA) - data
  // and analog pin 5 (SCL) - clock
  Wire.begin(); 

  // for debugging the incoming i2c information
  Serial.begin(9600);  // start serial for output 
  Serial.println("Start");
} 

void loop()
{

  // here we begin transmission to a particular i2c address (0x60 here)
  // and send it a 0x01, which on this compass returns a byte with heading info
  Wire.beginTransmission(0x60); 
  Wire.send(0x01);              
  Wire.endTransmission();       

  // receive data
  // now we request 1 byte from address 0x60, and check to see if it has come in
  Wire.requestFrom (0x60, 1);   
  if (Wire.available()) {
     // if available, the byte has come in
     byte bearing = Wire.receive();
     // print it out so we can see what it is
     Serial.println(bearing,DEC);
  } 
}