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
Syntax
Description
creates an object that represents the connection between the serial device connected to
the Arduino or ESP32 hardware at the serial port specified by
serialdevObj
= device(arduinoObj
,'SerialPort',SerialPort
)SerialPort
. The Arduino hardware is represented by an arduino
object.
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.serialdevObj
= device(arduinoObj
,'SerialPort',SerialPort
,Name,Value
)
Input Arguments
SerialPort
— Serial port ID
numeric
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
.
arduinoObj
— Arduino or ESP32 hardware connection
arduino
object
Arduino or ESP32 hardware connection, specified as an arduino
object.
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);
BaudRate
— Bit transmit rate
9600 (default) | 300 | 600 | 1200 | 2400 | 4800 | 14400 | 19200 | 28800 | 38400 | 57600 | 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.
DataBits
— Number of data bits to transmit
8
(default) | 5
| 6
| 7
Number of data bits to transmit, specified as a number.
Example: serialdevObj =
device(arduinoObj,'SerialPort',1,'BaudRate',115200, 'DataBits',8);
StopBits
— Number of bits to indicate end of byte
1
(default) | 2
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
— Type of parity check
none
(default) | odd
| even
Parity for the serial communication specified as a character vector.
Example: serialdevObj =
device(arduinoObj,'SerialPort',1,'BaudRate',115200, 'DataBits',8, 'StopBits',1,
'Parity','even');
Timeout
— Maximum time to wait for serial data
1
(default) | [0 8]
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
Interface
— Type of interface used for communication
'Serial'
This property is read-only.
Type of interface used for communication.
Example:
>> serialdevObj.Interface ans = Serial
SerialPort
— Serial port ID
numeric
Serial port available on the Arduino or ESP32 hardware, specified as a number.
Example:
>> serialdevObj.SerialPort ans = 1
Data Types: double
TxPin
— Transmission pin
character vector
Transmission pin for serial communication.
Example:
>> serialdevObj.TxPin ans = 'D18'
Data Types: char
RxPin
— Receive pin
character vector
Receive pin for serial communication.
Example:
>> serialdevObj.RxPin ans = 'D19'
Data Types: char
BaudRate
— Bit transmit rate
9600 (default) | 300 | 600 | 1200 | 2400 | 4800 | 14400 | 19200 | 28800 | 38400 | 57600 | 115200
The rate at which bits are transmitted in bits/s specified as a number.
Example:
>> serialdevObj.BaudRate ans = 115200
Data Types: double
NumBytesAvailable
— Number of bytes to read
numeric
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
— Type of parity check
none
(default) | odd
| even
Parity for the serial communication specified as a character vector.
Example:
>> serialdevObj.Parity ans = 'even'
Data Types: char
StopBits
— Number of bits to indicate end of byte
1
(default) | 2
Number of bits to indicate the end of data transmission, specified as a number.
Example:
>> serialdevObj.StopBits ans = 1
Data Types: double
DataBits
— Number of data bits to transmit
8
(default) | 5
| 6
| 7
Number of data bits to transmit, specified as a number.
Example:
>> serialdevObj.DataBits ans = 8
Data Types: double
Timeout
— Maximum time to wait for serial data
1
(default) | [0 8]
Maximum time in seconds to wait for serial data, specified as a number.
Example:
>> serialdevObj.Timeout ans = 1
Data Types: double
Examples
Create Connection to Serial Device on Arduino Hardware
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
Specify Baud Rate of Serial Device
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
Code Generation Using MATLAB Function Block
Use
device
in a MATLAB® Function block with the Simulink® Support Package for Arduino Hardware to generate code that can be deployed on Arduino or ESP32 Hardware.Specifying
BaudRate
as a name value pair argument is not supported the in MATLAB Function block. You can setBaudRate
from the Configuration Parameters dialog box. To set theBaudRate
:On the Simulink Toolstrip, in the Hardware tab, click Hardware Settings to open the Configuration Parameters dialog box.
In the Configuration Parameters dialog box, select Hardware Implementation. Verify that the Hardware board parameter is set to the appropriate Arduino board.
Go to Hardware board settings > Target hardware resources > Serial port properties.
Enter the value to set
BaudRate
in the particular serial port.
Default value of
Timeout
is0
.The property
DataBits
cannot be changed and always uses a default value8
.The property
Parity
cannot be changed and always uses a default valuenone
.The property
StopBit
cannot be changed and always uses a default value1
.The properties
Interface
,BitRate
,SCLPin
, andSDAPin
are not accessible from thedevice
object.
Version History
Introduced in R2019b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)