This reference is for Processing 3.0+. If you have a previous version, use the reference included with your software in the Help menu. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Core Javadoc and Libraries Javadoc.

Name

I2C

Examples
import processing.io.*;
I2C i2c;

void setup() {
  //printArray(I2C.list());
  i2c = new I2C(I2C.list()[0]);
}

void draw() {
  background(map(mouseX, 0, width, 0, 255));

  // send value over I2C to an digital-to-analog
  // converter with address 96 (hex 0x60)
  int val = int(4095 * map(mouseX, 0, width, 0.0, 1.0));
  i2c.beginTransmission(0x60);
  i2c.write(val >> 8);
  i2c.write(val & 255);
  i2c.endTransmission();
}

Description Opens an I2C interface as master

I2C is a serial bus, commonly used to attach peripheral ICs (Integrated Circuits) to processors and microcontrollers. It uses two pins, SDA (for data) and SDL (for the clock signal). Multiple "slave" devices can be connected to the same bus, as long as they are responding to different addresses (see below).

The I2C "master" device initiates a transmission, which includes sending the address of the desired "slave" device. I2C addresses consist of 7 bits plus one bit that indicates whether the device is being read from or written to. Some datasheets list the address in an 8 bit form (7 address bits + R/W bit), while others provide the address in a 7 bit form, with the address in the lower 7 bits.

This library expects addresses in their 7 bit form, similar to Arduino's Wire library, and what is being output by the i2cdetect utility on Linux. If the address provided in a datasheet is greater than 127 (hex 0x7f) or there are separate addresses for read and write operations listed, which vary exactly by one, then you want to shift the this number by one bit to the right before passing it as an argument to beginTransmission().
Methods
beginTransmission() Begins a transmission to an attached device
close() Closes the I2C device
endTransmission() Ends the current transmissions
list() Lists all available I2C interfaces
read() Reads bytes from the attached device
write() Adds bytes to be written to the device
Constructor
I2C(dev)
Parameters
dev String: interface name
Updated on October 2, 2018 05:02:13pm EDT

Creative Commons License