Main Content

device

Connection to serial device 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 serial device connected to Arduino® or ESP32 hardware. You can interact with the serial device using the functions listed in Object Functions.

Creation

Description

serialdevObj = device(arduinoObj,'SerialPort',SerialPort) creates an object that represents the connection between the serial device connected to the Arduino or ESP32 hardware at the serial port specified by SerialPort. The Arduino hardware is represented by an arduino object.

example

serialdevObj = device(arduinoObj,'SerialPort',SerialPort,Name,Value) creates an object that represents the connection with additional options specified by one or more Name,Value pair arguments between the serial device connected to the Arduino or ESP32 hardware.

example

Input Arguments

expand all

The ID of the serial port available on the Arduino or ESP32 hardware specified as a number.

Example: serialdevObj = device(arduinoObj,'SerialPort',1)

Note

The Arduino Mega2560, Due, and MegaADK boards have three serial ports available while the Arduino Leonardo, Micro, MKR1000, MKR1010, MKRZero, Nano33IoT, and Nano33BLE have one serial port available.

Note

For ESP32 modules, the supported SerialPort value is 2.

Arduino or ESP32 hardware connection, specified as an arduino object.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200);

The rate at which bits are transmitted in bits/s specified as a number.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200);

Note

If you are using more than one device, make sure the devices operate at same BaudRate for data consistency.

Number of data bits to transmit, specified as a number.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200, 'DataBits',8);

Number of bits to indicate the end of data transmission, specified as a number.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200, 'DataBits',8, 'StopBits',1);

Data Types: double

Parity for the serial communication specified as a character vector.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200, 'DataBits',8, 'StopBits',1, 'Parity','even');

Maximum time in seconds to wait for serial data, specified as a number.

Example: serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate',115200, 'DataBits',8, 'StopBits',1, 'Parity','even','Timeout',2);

Properties

expand all

This property is read-only.

Type of interface used for communication.

Example:

>> serialdevObj.Interface
ans = 
      Serial

Serial port available on the Arduino or ESP32 hardware, specified as a number.

Example:

>> serialdevObj.SerialPort
ans = 
      1 

Data Types: double

Transmission pin for serial communication.

Example:

>> serialdevObj.TxPin
ans = 
      'D18' 

Data Types: char

Receive pin for serial communication.

Example:

>> serialdevObj.RxPin
ans = 
      'D19' 

Data Types: char

The rate at which bits are transmitted in bits/s specified as a number.

Example:

>> serialdevObj.BaudRate
ans = 
      115200

Data Types: double

This property is read-only.

Number of bytes of data available to read from the serial device, specified as a number.

Example:

>> serialdevObj.NumBytesAvailable
ans = 
      0 

Data Types: double

Parity for the serial communication specified as a character vector.

Example:

>> serialdevObj.Parity
ans = 
      'even' 

Data Types: char

Number of bits to indicate the end of data transmission, specified as a number.

Example:

>> serialdevObj.StopBits

ans =
     1

Data Types: double

Number of data bits to transmit, specified as a number.

Example:

>> serialdevObj.DataBits

ans =
     8 

Data Types: double

Maximum time in seconds to wait for serial data, specified as a number.

Example:

>> serialdevObj.Timeout

ans =
     1

Data Types: double

Object Functions

readRead data from serial device
writeWrite data to serial device

Examples

collapse all

Create an arduino connection.

arduinoObj = arduino("COM16","Leonardo","Libraries",{'SPI','Serial','I2C'})
arduinoObj = 
  arduino with properties:

                  Port: 'COM16'
                 Board: 'Leonardo'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11', 'D13'}
   AvailableAnalogPins: {'A0-A5', 'D4', 'D6', 'D8-D10', 'D12'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'I2C', 'SPI', 'Serial'}

Create a connection to the serial device on the Arduino hardware.

serialdevObj = device(arduinoObj,'SerialPort',1)
serialdevObj = 
  device with properties:

             Interface: 'Serial'
            SerialPort: 1
                 TxPin: 'D1'
                 RxPin: 'D0'
              BaudRate: 9600 (bits/s) 
     NumBytesAvailable: 0 

Show all properties, functions

Create an arduino connection.

arduinoObj = arduino("COM16","Leonardo","Libraries",{'SPI','Serial','I2C'})
arduinoObj = 
  arduino with properties:

                  Port: 'COM16'
                 Board: 'Leonardo'
         AvailablePins: {'D2-D13', 'A0-A5'}
  AvailableDigitalPins: {'D2-D13', 'A0-A5'}
      AvailablePWMPins: {'D3', 'D5-D6', 'D9-D11', 'D13'}
   AvailableAnalogPins: {'A0-A5', 'D4', 'D6', 'D8-D10', 'D12'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'I2C', 'SPI', 'Serial'}

Create a connection to the serial device with the BaudRate set to 115200 bits/s.

serialdevObj = device(arduinoObj,'SerialPort',1,'BaudRate', 115200)
serialdevObj = 
  device with properties:

             Interface: 'Serial'
            SerialPort: 1
                 TxPin: 'D1'
                 RxPin: 'D0'
              BaudRate: 115200 (bits/s) 
     NumBytesAvailable: 0 

Show all properties, functions

More About

expand all

Version History

Introduced in R2019b

See Also

| |