主要内容

Create Custom Arduino Library Using Template

R2026b

The recommended approach to create a custom Arduino® library is to use the arduinoio.customLibrary.createLibraryTemplate function. This function generates a MATLAB® class file and a C++ header file with the required structure and boilerplate code. You then modify these files to implement your hardware-specific behavior.

Create a template for a custom Arduino library. Replace "LibraryName" with the name of your library.

arduinoio.customLibrary.createLibraryTemplate("LibraryName")

MATLAB creates a folder named +LibraryNameFolder inside +arduinoioaddons that contains a MATLAB class file and a src folder with a C++ header file.

If your custom library depends on third-party Arduino libraries, download them using arduinoio.customLibrary.downloadLibrary.

Modify the generated C++ header file in the src folder to implement hardware-specific command handling. For more information, see Create and Configure C++ Header File.

Modify the generated MATLAB class file to add methods for your hardware. For more information, see Create and Configure MATLAB Custom Arduino Library Class.

Connect to an Arduino board and send it the custom Arduino library.

a = arduino("COM5","uno","Libraries","LibraryNameFolder/LibraryName");

Create an add-on object to connect to the hardware using the custom Arduino library.

addonObject = addon(a,"LibraryNameFolder/LibraryName");

Test the connection by calling a method defined in the custom Arduino library. The generated template includes a testRead method that returns Hello from Arduino.

addonObject.testRead
ans =

    'Hello from Arduino'

For more information about the generated folder structure, MATLAB class, and C++ header file, see arduinoio.customLibrary.createLibraryTemplate.

See Also

|