device

Connection to device on SPI bus on Arduino hardware

Description

This object represents a connection to the SPI device on Arduino® hardware. Attach an SPI device to the appropriate pins on the Arduino 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 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 'SPIChipSelectPin' name-value pair is mandatory for creating the SPI 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,'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.

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:

>> deviceObj.Interface
ans = 
      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

writeReadWrite and read data from SPI device

Examples

collapse all

Create an arduino object with the I2C library.

arduinoObj = arduino('COM9','Uno','Libraries','SPI')
Updating server code on board Uno (COM9). This may take a few minutes.
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: {'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

See Also

Introduced in R2019a