device

Connection to device on I2C or SPI bus on Arduino hardware

Description

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

Creation

Syntax

deviceObj = device(arduinoObj,Name,Value)

Description

example

deviceObj = device(arduinoObj,Name,Value) creates an object that represents the connection between an I2C or SPI device connected to the I2C bus or SPI 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' and 'SPIChipSelectPin' name-value pairs are mandatory for I2C and SPI devices, respectively.

Input Arguments

expand all

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

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: deviceObj = device(arduinoObj,'SPIChipSelectPin','D8');

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')

Dependencies

This argument is valid only for I2C devices.

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.

Dependencies

This argument is valid only for I2C devices.

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.

Dependencies

This argument is valid only for I2C devices.

Data Types: double

This is a mandatory name value pair argument if you are connecting to an SPI device. Represents pin number that is used as the chip select pin on the Arduino hardware to communicate with the SPI device.

Example: deviceObj = device(arduinoObj,'SPIChipSelectPin','D10')

Dependencies

This argument is valid only for SPI devices.

SPI communication mode, specified as a number between 0 and 3. Specify the clock polarity and phase by setting the mode.

SPI ModeClock PolarityClock Phase
000
101
210
311

Example: deviceObj = device(arduinoObj,'SPIChipSelectPin','D4','mode',3) creates a connection to the SPI device and sets the clock polarity to 1 and clock phase to 1.

Dependencies

This argument is valid only for SPI devices.

SPI communication bit order, specified as:

  • 'msbfirst' - The most significant bit is transmitted first.

  • 'lsbfirst' - The least significant bit is transmitted first.

Example: deviceObj = device(arduinoObj,'SPIChipSelectPin','D4','bitorder','lsbfirst') creates a connection to the SPI device and sets the least significant bit in the first column.

Dependencies

This argument is valid only for SPI devices.

The maximum speed of SPI communication in bits/s, specified as a positive integer.

Example: deviceObj = device(arduinoObj,'SPIChipSelectPin','D4','bitrate',2000000) creates a connection to the SPI device and sets the bitrate to 2000000 bits/s.

Dependencies

This argument is valid only for SPI devices

Properties

expand all

This property is read-only.

Specifies the type of interface used for communication.

Example:

>> deviceObj = device(arduinoObj,'SPIChipSelectPin','D4'); 
>> deviceObj.Interface
ans = 
      SPI

I2C

Note

If you are using bus 1 on the Arduino Due for I2C communication, connect two 10k pull-up resistors between SDA and SCL and 5V pins.

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

SPI

This property is read-only.

Arduino pin connected to chip select line on the SPI device, specified as a character vector of the form 'Ax' or 'Dx'. Here, x is the pin number, D is digital, and A is analog.

Example:

>> deviceObj.SPIChipSelectPin
ans = 
      'D10' 

Data Types: char

This property is read-only.

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

Example:

>> deviceObj.SCLPin

ans =

     'D13'

Data Types: char

This property is read-only.

The pin that connects the slave output to the master input.

Example:

>> deviceObj.MISOPin

ans =

     'D12'

Data Types: char

This property is read-only.

The pin that connects the master output to the slave input.

Example:

>> deviceObj.MOSIPin

ans =

     'D11'

Data Types: char

SPI communication mode, specified as a number between 0 and 3. Specify the clock polarity and phase by setting the mode.

SPI ModeClock PolarityClock Phase
000
101
210
311

Example:

>> deviceObj.SPIMode

ans =

     2

Data Types: double

This property is read-only.

The logic levels of the SPI pins.

Example:

>> deviceObj.ActiveLevel

ans =
    ActiveLevelEnum enumeration 
    low

SPI communication bit order, specified as msbfirst or lsbfirst. If it is msbfirst, the most significant bit is transmitted first to the writeRead output. If it is lsbfirst, the least significant bit is transmitted first.

Example:

>> deviceObj.BitOrder

ans = 

    'msbfirst'

This property is read-only.

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

Example:

>> deviceObj.BitRate

ans = 

    4000000

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
writeReadWrite and read data from SPI device

Examples

collapse all

Create an arduino object with the I2C library.

arduinoObj = arduino('COM4','Uno','Libraries','I2C');
arduinoObj = 
           arduino with properties:

                  Port: 'COM4'
                 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','0x50')
  deviceObj = 
              device with properties: 

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

   Show functions

               

Create an arduino object with the SPI library.

arduinoObj = arduino('COM4','Uno','Libraries','SPI')
arduinoObj = 
           arduino with properties:

                  Port: 'COM4'
                 Board: 'Uno'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11'}
   AvailableAnalogPins: {'A0-A5'}
    AvailableI2CBusIDs: [0]
             Libraries: {'SPI'}

               

Create a device object.

deviceObj = device(arduinoObj,'SPIChipSelectPin','D10')
   deviceObj = 
              device with properties: 

             Interface: 'SPI'
      SPIChipSelectPin: 'D10'
                SCLPin: 'D13'
               MISOPin: 'D12'
               MOSIPin: 'D11'  

   Show all properties, functions

               

Introduced in R2019a