Main Content


Send message to Arduino device from MATLAB

Add-On Required: This feature requires the MATLAB Support Package for Arduino Hardware add-on.



[dataOut,payloadSize] = sendCommand(arduinoObj,libName,commandID,dataIn) sends a command to the Command Handler implemented by your add-on library.

[dataOut,payloadSize] = sendCommand(arduinoObj,libName,commandID,dataIn,timeout) sends a command to the Command Handler implemented by your add-on library with a user-specified acknowledgement timeout.


collapse all

Create a command, and use sendCommand to send it to the Arduino® hardware.

Get the parent Arduino object.

arduinoObj = obj.Parent;

Define the library name.

libName = 'ExampleAddon/HelloWorld';

Define the command ID, and create data to send.

commandID = obj.READ_COMMAND;
dataIn = [];

Send the message.

[dataOut, payloadSize] = sendCommand(obj, libName, commandID, dataIn);

Show the result in the MATLAB® command line.

Hello World!

Input Arguments

collapse all

Arduino object, specified as an object that is an internal variable called from within classes that derive from matlabshared.addon.LibraryBase.

Name of library defined in the custom class, specified as a character vector.

Unique command identifier, specified as a number. This number must match the ID in the command handler defined in your custom library header file.

An array of integers, specified as a vector. The size of dataIn is different for different boards. The below table specifies the size of dataIn for different boards :

BoardsdataIn size
Leonardo, Micro150 bytes
MKR1000, MKR1010, MKR Zero, Mega2560,MegaADK, Due, Nano 33 IoT, Nano 33 BLE, Nano 33 BLE Sense720 bytes
Uno, Nano, Pro Mini, Digital Sandbox57 bytes

Each byte is limited to an unsigned integer between 0 and 255. Any values greater than 255 must be converted into two unsigned integers between 0 and 255, and will be sent as two separate bytes.

Timeout duration, specified as a numeric value that is the number of seconds to wait for an acknowledgement from the server code. Specifying a timeout duration blocks the MATLAB command line until the timeout duration is met. The default is 5 seconds if timeout is not specified.

Output Arguments

collapse all

Data returned, as an array of doubles

Data Types: uint8

Length of the output returned by sendCommand, as a number.

Version History

Introduced in R2015b