device

Connection to device on I2C bus on Arduino hardware

Description

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

Creation

Description

example

deviceObj = device(arduinoObj,,'I2CAddress',I2CAddress,Name,Value) creates an object that represents the connection between an I2C connected to the I2C bus on the Arduino hardware. The Arduino 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.

Input Arguments

expand all

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

Name-Value Pair 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','0x71')

I2C bus number, specified as 0 or 1. The default value is 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 = 
      85 

Data Types: double

This property is read-only.

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

Example:

>> deviceObj.Bus
ans = 
      0 

Data Types: double

This property is read-only.

A serial clock pin for the serial clock signal that the I2C Master generates.

Example:

>> deviceObj.SCLPin
ans = 
      'A5' 

Data Types: char

A serial data pin for a serial data signal.

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('COM9','Uno','Libraries','I2C')
arduinoObj = 
  arduino with properties:

                  Port: 'COM9'
                 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'}

Create a device object.

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

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

Show functions

Introduced in R2019a