"I2C" (Device Connection Protocol)

  • "I2C" is a low-level device communication protocol for devices connected directly to the processor running the Wolfram Language.
  • I2C is a half-duplex bus protocol for devices that allows many devices to be attached to a single device with only two wires, the Serial Data Line (SDA) and Serial Clock Line (SCL). The SDA is used for communicating data, while the SCL synchronizes the data as it is sent from device to device.
  • There are two classes of devices that are compatible with I2C, master devices and slave devices. Master devices facilitate all communication on the bus, but slave devices are not allowed to communicate on the bus unless explicitly permitted by the master. The Wolfram Language implementation of I2C designates the device that is running the Wolfram Language as the master on the I2C bus.
  • The I2C clock speed is derived from the Linux kernel and defaults to 100 kHz on the Raspberry Pi.
  • The system may need to be set up prior to using the I2C kernel; see "I2C Setup" for more information.
  • The I2C bus can only be accessed when running the Wolfram Language on the Raspberry Pi.
  • Note: On Raspbian versions lower than Jessie, to access GPIO you must be logged in as root or be running the Wolfram Language as root. All higher versions do not need root access.

    Device Discovery

    • I2C devices are discovered automatically. Use FindDevices to list the devices currently on the I2C bus.

    Opening the Device

      DeviceOpen["I2C",addr]

      opens a connection to the I2C device with a slave address addr.

      DeviceOpen["I2C",{addr,bus}]

      opens a connection to the I2C device with a slave address addr on the specified bus.

    • Valid I2C slave addresses are integers from 8 to 119. 10-bit addresses are not supported. Consult the manufacturer's documentation on the device for a particular device's address.
    • On the Raspberry Pi, the only valid bus is 0.

    Reading Data

      DeviceRead[dev]

      reads a single byte from the device on the I2C bus.

      DeviceRead[dev,n]

      reads n bytes from the device on the I2C bus.

    • The I2C slave device may need to be configured independently of the Wolfram Language before communication occurs.
    • Note that some I2C slave devices may respond differently to read operations requesting a different number of bytes. Consult the manufacturer's documentation on the device for more information.

    Writing Data

      DeviceWrite[dev,data]

      writes data to the device.

    • data must be a list of integers between 0 and 255 or a single Integer between 0 and 255.
    • The I2C slave device may need to be configured independently of the Wolfram Language before communication occurs.

    Closing and Releasing Resources

      DeviceClose[dev]

      closes the connection to the I2C bus and frees the bus for other programs to use.

    Examples

    Basic Examples  (1)

    Find I2C devices on the bus:

    Open the device:

    Write a sequence of bytes to the device:

    Read a single byte from the device:

    Read a sequence of 10 bytes from the device: