Introduction: How to Get Started With I2C - Wonderful World of Inter IC Communication

About: We sell DIY kits for electronics. Please check out our website: http://www.jouletime.com/

The Arduino, PIC, Rasberry PI, etc. are all incredible. But what do you do with it once you have one?

The best thing is to add sensors and such.

Today a lot of the hot new technology uses the I2C protocol to allow the computera, phones, tablets, or microcontrollers to talk the sensors. Smart phones would be less smart if they couldn’t talk to that accelerometer sensor to know which way your phone is facing.

Step 1: Getting Started With an Arduino

Since we know the Arduino the best, we will use it for our example. So first you need an Arduino.

You will also need some kind of I2C parts, usually on a breakout board.

You can either go to a website like Sparkfun, Adafruit, or even ebay and search for parts on breakout boards.

Or you can “roll-your-own” so to speak. We are running a Kickstarter that happens to be for breakout boards and parts you solder on them. That campaign has ended, but if you are still interested then send us an email. You can contact us through Instructables, Kickstarter, or our website www.jouletime.com. Check out the Kickstarter here.

https://www.kickstarter.com/projects/1257390142/ba...

Basic and Advance Breakout Boards - Plus More

Warning, for starting out pick a part that is 5 volt compatible. You can get 3.3 volt power from the Arduino board, but the I2C is still at 5 volts. Just start simple and stick with 5 volts to begin with.

You need a few jumper wires.

Finally depending on your part, you could need a few capacitors or resistor. Some breakout boards supply all of those parts.

Step 2: Wiring Is Easy

You should put your part on a bread board and run power from the Arduino over. Read the instruction on your device, and figure out how to connect 5 volt power and ground.

Next depending on the part you picked, you may need to run power or ground to an enable pin. You may also need a few resistors or capacitors. Wire these as instructed.

Finally you need to run two wires for communication. On the Arduino Uno, Analog pin A4 is SDA, and A5 is SCL. Connect to the matching pins on your device.

Step 3: Add Software

The final piece is the software. For the Arduino, the library you need to include is “Wire”. This should automatically be available. Arduino website, info on Wire Library

A bug in the Instrutables website won’t let me add the correct wording for including the wire library. You will have to manually fix this if you copy the code.

Github link for the Arduino File

The sample code will scan for any connected devices and let you know if your part is connected.

// --------------------------------------
// i2c_scanner // // Version 1 // This program (or code that looks like it) // can be found in many places. // For example on the Arduino.cc forum. // The original author is not know. // Version 2, Juni 2012, Using Arduino 1.0.1 // Adapted to be as simple as possible by Arduino.cc user Krodal // Version 3, Feb 26 2013 // V3 by louarnold // Version 4, March 3, 2013, Using Arduino 1.0.3 // by Arduino.cc user Krodal. // Changes by louarnold removed. // Scanning addresses changed from 0...127 to 1...119, // according to the i2c scanner by Nick Gammon // www.jouletime.com // Version 5, March 28, 2013 // As version 4, but address scans now to 127. // A sensor seems to use address 120. // Version 6, May 2015 // Added tests at specific addresses for known devices. // Why do a blind search when we are expecting to // find parts connected. You may also find hidden devices. // www.jouletime.com // // This sketch tests the standard 7-bit addresses // Devices with higher bit address might not be seen properly. //

#include // you need to enter the wire library here.

void setup()

{

Wire.begin();

Serial.begin(9600); Serial.println("\nI2C Scanner");

}

void loop()

{

byte error, address; int nDevices;

Serial.println("Scanning...");

nDevices = 0; for(address = 1; address < 127; address++ )

{

// The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address.

Wire.beginTransmission(address);

error = Wire.endTransmission();

if (error == 0) { Serial.print("I2C device found at address 0x");

if (address<16) Serial.print("0"); Serial.print(address,HEX);

Serial.println("!");

if (address==0x03){

Serial.print(" This may be software reset address for a LED-driver connected.");

Serial.println(); Serial.println();}

if (address==0x1C){ Serial.print(" You may have an Accelerometer-01 connected.");

Serial.println(); Serial.println();}

if (address==0x1D){ Serial.print(" You may have an Accelerometer-02 connected.");

Serial.println(); Serial.println();}

if (address==0x20){ Serial.print(" You may have a 16-Bit Expander connected.");

Serial.println(); Serial.println();}

if (address==0x2C){ Serial.print(" You may have a Digital Pot. connected."); Serial.println(); Serial.println();}

if (address==0x3E){ Serial.print(" You may have a 6-bit DAC connected.");

Serial.println(); Serial.println();}

if (address==0x4C){ Serial.print(" You may have a Temp Sensor connected.");

Serial.println(); Serial.println();}

if (address>=0x50 && address<=0x57){

Serial.print(" You may have a EEPROM Memory (Various) connected.");

Serial.println(); Serial.println();}

if (address==0x60){ Serial.print(" You may have a 12-bit DAC connected.");

Serial.println(); Serial.println();}

if (address==0x61){ Serial.print(" You may have a LED-Driver connected.");

Serial.println(); Serial.println();}

if (address==0x6F){ Serial.print(" You may have a RTCC connected.");

Serial.println(); Serial.println();}

if (address==0x70){ Serial.print(" This may be the All-Call address for a LED-driver connected.");

Serial.println(); Serial.println();}

nDevices++;

}

else if (error==4)

{

Serial.print("Unknow error at address 0x");

if (address<16) Serial.print("0");

Serial.println(address,HEX);

}

}

if (nDevices == 0)

Serial.println("No I2C devices found\n");

else Serial.println("done\n");

delay(5000); // wait 5 seconds for next scan

}

Step 4: Next Time .....

Well if everything went as it should, then you now have the basics of I2C.

Next time we will pick a part that we like and show you all the details of writing commands and reading back information from your sensor.

Good Luck