How do I use I2C devices with Arduino?

Arduino have the Wire Library that communicate with I2C.

This library allows you to communicate with I2C / TWI devices. On the Arduino boards with the R3 layout (1.0 pinout), the SDA (data line) and SCL (clock line) are on the pin headers close to the AREF pin. The Arduino Due has two I2C / TWI interfaces SDA1 and SCL1 are near to the AREF pin and the additional one is on pins 20 and 21. As a reference the table below shows where TWI pins are located on various Arduino boards.

Board I2C / TWI pins

  • Uno, Ethernet A4 (SDA), A5 (SCL)
  • Mega2560 20 (SDA), 21 (SCL)
  • Leonardo 2 (SDA), 3 (SCL)
  • Due 20 (SDA), 21 (SCL), SDA1, SCL1

You can take a look at , this examples.

Like @TheDoctor said:

The I2C data lines both need resistors to 3.3V. This keeps them from drifting if they are disabled. Andy resistor value should work, but it is recommended to use one between 10k ohm and 47k ohm.


Additional shields are not needed.

You only need the Wire library and a couple of wires to connect the device to SDA and SCL.

The position of these pins is different on the various Arduinos, please have a look here.


For future readers: use the Adafruit_MCP23017 library.

Tags:

I2C