This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Connection to device on Arduino hardware I2C bus


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.


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



dev = i2cdev(a,address)
dev = i2cdev(a,address,Name,Value)



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.


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.


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.


>> 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.


>> dev.Bus

ans =


Data Types: double

This property is read-only.

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


>> dev.Address

ans =


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


collapse all

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

a = arduino('COM4');
ans = 8×1 cell array

Use the address supplied to create an I2C device connection.

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

       Pins: A4(SDA), A5(SCL)
        Bus: 0
    Address: 84 (0x54)

Copyright 2018 The MathWorks, Inc.

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