i2cdev

Connection to device on Arduino hardware I2C bus

Description

An i2cdev object represents a connection to the device on Arduino® hardware I2C bus. Attach an I2C device to the appropriate pins on the Arduino hardware. Create an i2cdev object using the i2cdev function. You can read from and write to your I2C device in MATLAB® using the object functions.

Note

The i2cdev method will not be supported in future releases of MATLAB Support Package for Arduino Hardware. Use device instead.

Creation

Description

example

dev = i2cdev(a,address) creates an object that represents the connection to the device connected to the I2C bus on the specified Arduino hardware a.

example

dev = i2cdev(a,address,Name,Value) adds additional options specified by one or more Name,Value pair arguments.

Input Arguments

expand all

Arduino hardware connection created using arduino, specified as an object.

Example: dev = i2cdev(a,'0x48') creates a connection to I2C device on the specified arduino object a.

Address of device connected to the Arduino I2C bus, specified as a character vector, string, or integer. The address can be a hex or a scalar integer. To identify available I2C bus addresses, use scanI2Cbus.

Example: dev = i2cdev(a,'0x48') creates a connection to the I2C device specified by the address '0x48'.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: dev = i2cdev(a,'0x71','bus',0)

I2C bus, specified as the comma-separated pair consisting of 'bus' and a number. If you are using hardware that supports multiple I2C buses, such as Arduino Due, use bus 0 to connect to SDA and SCL and bus 1 to connect to SDA1 and SCL1.

Example: i2cdev = (a,'0x61','bus',1) creates a connection to the I2C bus 1, or SDA1 and SCL1 pins on the hardware.

Properties

expand all

This property is read-only.

Arduino device pins being used by your I2C device, specified as a cell array of character vectors. The pins are specific to each type of Arduino board. The Arduino Uno uses pins A4 and A5 for SDA and SCL, respectively, while the Arduino Due uses pins 20 and 21.

Example:

>> dev.Pins

ans =

  1×2 cell array

    {'A4'}    {'A5'}

Data Types: cell

This property is read-only.

I2C bus number, specified as a number. The default value is 0. On Arduino hardware that supports multiple I2C buses, such as Arduino Due, using bus 0 to connects to SDA and SCL and bus 1 connects to SDA1 and SCL1.

Example:

>> dev.Bus

ans =

     0

Data Types: double

This property is read-only.

I2C device address on the Arduino hardware, specified as a number.

Example:

>> dev.Address

ans =

   104

Data Types: double

Object Functions

Use these object functions to read from and write to your I2C device.

readRead data from I2C bus
readRegisterRead data from I2C device register
writeWrite data to I2C bus
writeRegisterWrite data to I2C device register

Examples

collapse all

Create Arduino hardware object, and scan for the I2C bus.

a = arduino('COM9');
scanI2CBus(a)
ans = 1×1 cell array
    {'0x68'}

Use the address supplied to create an I2C device connection.

dev = i2cdev(a,'0x68')
Warning: Function i2cdev will be removed. Use <a href="matlab:helpview(arduino.internal.getDocMap, 'device123')">device</a> instead.
dev = 
  i2cdev with properties:

       Pins: A4(SDA), A5(SCL)
        Bus: 0
    Address: 104 (0x68)

Create a connection to an Arduino Due and specify the bus number. You can specify bus 1 only if you are using Arduino Due boards.

a = arduino('COM5','Due','Libraries','I2C');
dev = i2cdev(a,'0x48','bus',1)
dev = 
  i2cdev with properties:

       Pins: SDA1, SCL1     
        Bus: 1
    Address: 72 (0x48)

Introduced in R2014b