Main Content

device

Connection to device on I2C bus on Arduino or ESP32 hardware

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

Description

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 pairs. The 'I2CAddress' name-value pair 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

Specify at least one comma-separated pair 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: 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 register

Examples

collapse all

Create an arduino object with 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

Version History

Introduced in R2019a