Contenu principal

Custom Arduino Library Concepts

R2026b

A custom Arduino® library combines MATLAB® and C++ code to provide easy access to Arduino hardware and connected peripherals, eliminating the need for low-level programming and enabling faster development within the MATLAB environment.

The custom Arduino library framework provides the matlabshared.addon.LibraryBase class in MATLAB and the C++ librarybase.h header file. A custom Arduino library consists of a MATLAB class that inherits matlabshared.addon.LibraryBase and a C++ header that includes librarybase.h.

If you use arduinoio.customLibrary.createLibraryTemplate, both files are generated automatically with the required inheritance and includes.

Command Handler

Communication between the Arduino hardware and MATLAB on the development computer uses a server-client relationship, where the hardware acts as the server and MATLAB acts as the client.

The sendCommand function issues a command, known as a commandID, from MATLAB on the development computer. The board receives the command and routes it to the Command Handler method in the C++ header file. A switch statement determines which code segment to execute on the Arduino device. When complete, the command handler calls sendResponseMsg() to send response data back to MATLAB and confirm that the specified commandID executed successfully.

Communication between MATLAB on the development computer and the Arduino hardware incurs a 20 ms time delay.

This schematic shows the structure of a custom Arduino library. The custom Arduino library framework provides matlabshared.addon.LibraryBase and librarybase.h as base components. Your custom library extends these into a MATLAB class that runs on the development computer and a C++ header file that is deployed to the board. The command handler enables bidirectional communication. The MATLAB class sends commands to the board, and the C++ header file processes them and returns responses.

Schematic showing the custom Arduino library framework providing matlabshared.addon.LibraryBase and librarybase.h as base components. The custom library extends these into a MATLAB class that runs on the development computer and a C++ header file that is deployed to the Arduino board. The command handler enables bidirectional communication between them.

Required Knowledge

Creating custom Arduino libraries requires familiarity with:

  • MATLAB classes

  • C++

  • Your hardware device and its APIs

See Also

|