"I2C" (Device Connection Protocol)
Device Discovery
-
I2C devices are discovered automatically. Use FindDevices to list the devices currently on the I2C bus.
Opening the Device
-
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.
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.
Reading Data
-
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.
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.
Writing Data
-
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.
DeviceWrite[dev,data]
writes data to the device.
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)
FindDevices["I2C"]i2cDev = Last[%]DeviceOpen[i2cDev]Write a sequence of bytes to the device:
DeviceWrite[i2cDev, {64, 0, 0, 0}]Read a single byte from the device:
DeviceRead[i2cDev]Read a sequence of 10 bytes from the device:
DeviceRead[i2cDev, 10]Tech Notes
History
Introduced in 2015 (10.3)