Main Content

device

Connection to device on SPI 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 SPI device on Arduino® or ESP32 hardware. Attach an SPI device to the appropriate pins on the Arduino or ESP32 hardware. You can interact with the SPI device using the functions listed in Object Functions.

Creation

Description

example

deviceObj = device(arduinoObj,'SPIChipSelectPin',SPIChipSelectPin,Name,Value) creates an object that represents the connection between SPI device connected to the SPI bus on the 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 'SPIChipSelectPin' name-value pair is mandatory for creating the SPI device connection.

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,'SPIChipSelectPin','D8');

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.

To identify the pins that can be used as chip select pins, see Chip Select Pins.

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

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','SPIMode',3) creates a connection to the SPI device and sets the clock polarity to 1 and clock phase to 1.

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.

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.

Properties

expand all

This property is read-only.

Specifies the type of interface used for communication.

Example:

>> d.Interface

ans = 

  InterfaceEnum enumeration

    Serial

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.

To identify the pins that can be used as chip select pins, see Chip Select Pins.

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

Example:

>> deviceObj.SCLPin

ans =

     'D13'

Data Types: char

This property is read-only.

The pin that connects the peripheral output to the controller input.

Example:

>> deviceObj.SDIPin

ans =

     'D12'

Data Types: char

This property is read-only.

The pin that connects the controller output to the peripheral input.

Example:

>> deviceObj.SDOPin

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

writeReadWrite and read data from SPI device

Examples

collapse all

Create an arduino object with the SPI library.

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

                  Port: 'COM5'
                 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'}
Show all properties

Create a device object.

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

             Interface: 'SPI'
      SPIChipSelectPin: 'D10'
                SCLPin: 'D13'
                SDIPin: 'D12'
                SDOPin: 'D11'

Show all properties, functions

More About

expand all

Version History

Introduced in R2019a

See Also