Main Content

device

R2026b

Connection to device on I2C bus on Arduino or ESP32 hardware

Description

Add-On Required: This feature requires the MATLAB Support Package for Arduino Hardware add-on.

This object represents a connection to the I2C device on Arduino® or ESP32 hardware. Attach an I2C device to the appropriate pins on the Arduino or ESP32 hardware. You can interact with the I2C device using the functions listed in Object Functions.

Creation

Description

deviceObj = device(arduinoObj,'I2CAddress',I2CAddress,Name=Value) creates an object that represents the connection between an I2C peripheral connected to the central Arduino or ESP32 hardware. The Arduino or ESP32 hardware is represented by an arduino object. You can also customize the connection further using one or more name-value arguments. The 'I2CAddress' name-value argument is mandatory for creating the I2C device connection.

example

Input Arguments

expand all

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

Name-Value Arguments

expand all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: deviceObj = device(arduinoObj,'I2CAddress','0x71');

This is a mandatory name value pair argument if you are connecting to an I2C device. Address of the I2C device connected to the I2C bus, specified as a character vector, string, or integer. The address can be a hex or a scalar integer. Obtain this address value using the scanI2CBus function.

Example: deviceObj = device(arduinoObj,'I2CAddress','0x68')

I2C bus number, specified as 0 or 1. The default value is 0.

Note

Bus number 1 is only available on Due and Nano33BLE boards. Other Arduino boards support Bus number 0.

Example: deviceObj = device(arduinoObj,'I2CAddress','0x61',Bus=0) creates a connection to the I2C bus on the hardware.

The maximum speed of I2C communication in bits/s, specified as 100000 or 400000 bits/s.

Example: deviceObj = device(arduinoObj,'I2CAddress','0x71',BitRate=100000) creates a connection to the I2C device and sets the bitrate to 100000 bits/s.

Data Types: double

Properties

expand all

This property is read-only.

Specifies the type of interface used for communication.

Example:

>> deviceObj.Interface
ans = 
      I2C

This property is read-only.

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

Example:

>> deviceObj.I2CAddress
ans = 
      104 

Data Types: double

This property is read-only.

I2C bus number, specified as 0 or 1. The default value is 0.

Note

Only Arduino Due board supports both the I2C bus numbers 0 and 1.

Example:

>> deviceObj.Bus
ans = 
      0 

Data Types: double

This property is read-only.

This property is read-only. A serial clock pin for the serial clock signal that the I2C central generates.

Note

SCLPin for I2CBus1 appears as SCL1.

Example:

>> deviceObj.SCLPin
ans = 
      'A5' 

Data Types: char

This property is read-only. A serial data pin for a serial data signal.

Note

SDAPin for I2CBus1 appears as SDA1.

Example:

>> deviceObj.SDAPin
ans = 
      'A4' 

Data Types: char

This property is read-only.

The speed at which bus is set for I2C communication, specified as a positive integer in bits/s.

Example:

>> deviceObj.BitRate
ans = 
      100000

Data Types: double

Object Functions

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

Examples

collapse all

Create an arduino object with the I2C library.

arduinoObj = arduino("COM15","Uno",Libraries="I2C")
arduinoObj = 
  arduino with properties:

                  Port: 'COM15'
                 Board: 'Uno'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11'}
   AvailableAnalogPins: {'A0-A5'}
    AvailableI2CBusIDs: [0]
             Libraries: {'I2C'}
Show all properties

Create a device object.

deviceObj = device(arduinoObj,I2CAddress=0x50)
deviceObj = 
  device with properties:

             Interface: 'I2C'
            I2CAddress: 80 ('0x50')
                   Bus: 0
                SCLPin: 'A5'
                SDAPin: 'A4'
               BitRate: 100000 (bits/s)

Show functions

More About

expand all

Alternative Functionality

App

Use Arduino Explorer to configure the I2C peripheral and read from and write to I2C peripherals connected to Arduino or any other supported hardware. You can use the app in the installed version of MATLAB and MATLAB Online™. For more information, see Connect to I2C Device Using Arduino Explorer App.

Version History

Introduced in R2019a