Main Content

Create Standalone Applications for Arduino Hardware

This example shows how to create a standalone application and install and run the generated application on target computers that do not have MATLAB® installed. This example uses the LCD add-on library, but you can use this workflow to create a standalone application for any user-defined function or MATLAB script that uses Arduino® functionality. For more information, see Create LCD Add-on.

This example helps you to:

  1. Generate a standalone application by compiling a user-defined function that displays “Hello World!” on an LCD.

  2. Run the application on a target computer that does not have MATLAB installed.

Generate Standalone Application

Create a standalone application.

  1. Set up the hardware as described in Setup.

  2. Add the ExampleLCD/LCDAddon add-on library to the path.

  3. For this example, build an application from a function called LCDAddOnExample_deploy. The function displays "Hello World!" on an LCD. Save the function to a file.

    function LCDAddOnExample_deploy()
       a = arduino('COM5','Uno','Libraries','ExampleLCD/LCDAddon','ForceBuildOn',true);
       lcd = addon(a,'ExampleLCD/LCDAddon','RegisterSelectPin','D7','EnablePin','D6','DataPins',{'D5','D4','D3','D2'});
       initializeLCD(lcd);
       printLCD(lcd,'Hello World!');
       clearLCD(lcd);
    end
    

  4. Use the Standalone Application Compiler (MATLAB Compiler) app to package the MATLAB programs into applications that can run outside of MATLAB. To open the Standalone Application Compiler app, type standaloneApplicationCompiler at the MATLAB command line. Alternatively, on the Apps tab, expand the Apps gallery. In the Application Deployment section, click Standalone Application Compiler.

    Application Deployment

  5. After you open the app, the Create Compiler Task dialog box prompts you to add a compiler task to a new or an existing MATLAB project. For this example, select Start a new project and create a compiler task and create a new project named LCDAddOnProject in your working folder.

    Create Compiler Task

    A new compiler task named StandaloneDesktopApp1 opens in the Editor.

    Standalone Desktop App

  6. In the Main File section of the compiler task, click Add Main File and select LCDAddOnExample_deploy.m. In the Project panel, the file now has the labels Design and Main Function.

    Main File

    The Files Required for Standalone to Run section of the app displays the MATLAB Support Package for Arduino Hardware.

    Arduino Support Package

  7. In the Files Required for Standalone to Run section, under Custom Requirements, add ExampleLCD/LCDAddon.m, which is the LCD add-on library.

    LCD add-on library

  8. In the Application Info section, replace the string My Desktop Application with the name of your standalone application, LCDAddOnApplication. You can specify other details in this section, such as the author, company, and description.

    Application Info

  9. In the Executable Details section, name the executable LCDAddonApplication. Under Application Type, select Standalone Windows Application if you are using Windows®; otherwise, select Standalone Application. Under Input Type, select Treat inputs to the app as a numeric MATLAB double.

    Executable details

  10. In the Installer Details section, name the generated installer LCDAddonInstaller. You can also specify other installer options such as the runtime delivery method. These options correspond to the options available with the compiler.package.installer (MATLAB Compiler SDK) function.

    Installer Details

  11. You can convert this code to a MATLAB script file by clicking the Export Build Script button. Running the generated build script is equivalent to clicking the Build and Package button.

    Deploy toolbar

    To generate both the standalone application and an installer, click Build and Package. To create the application executable without an installer, select Build and Package > Build.

The compiler generates files in the <compiler_task_name>/output folder in your project folder. The build subfolder contains the standalone application executable, and the package subfolder contains an installer for your standalone application along with MATLAB Runtime. To choose a different output location for the generated files, update the paths in the Output Locations section.

For more details on the application compiler app, see Create Standalone Application from MATLAB (MATLAB Compiler) and Manage Support Packages (MATLAB Compiler).

Install and Run Standalone Application on Target Computer

To deploy your application outside of MATLAB, your version of MATLAB Runtime must be the same version you must have MATLAB Runtime installed at the same version as the MATLAB version used to build the application. Ensure that your end users can run the application by including MATLAB Runtime in the installer or by supplying users with information on how to download it. For information on installing and using MATLAB Runtime, see About MATLAB Runtime (MATLAB Compiler SDK).

To run the generated LCDAddonApplication application on the target computer, the user needs to download the Arduino CLI, run the configuration commands, and install the required cores and libraries.

  1. Download the Arduino CLI from GitHub based on your operating system. The Arduino CLI enables you to configure your Arduino board at the command line.

    Operating SystemArduino CLI 1.0.2
    Windowsarduino-cli_1.0.2_Windows_64bit.zip
    Mac with Apple Silicon Processorarduino-cli_1.0.2_macOS_ARM64.tar.gz
    Linux®arduino-cli_1.0.2_Linux_64bit.tar.gz

  2. Extract the files to a location such as:

    • For Windows: C:\Arduino

    • For Mac: ~/Documents/Arduino

    • For Linux: /home/<userName>/Documents/Arduino

  3. Open the command terminal from the folder where you extracted the files and execute these configuration commands.

    Operating SystemConfiguration Commands
    Windows

    arduino-cli config init --overwrite --dest-file arduino-cli.yaml

    arduino-cli config set directories.data data --config-file arduino-cli.yaml

    arduino-cli config set directories.user user --config-file arduino-cli.yaml

    arduino-cli config set directories.downloads downloads --config-file arduino-cli.yaml

    Mac with Apple Silicon Processor and Linux

    ./arduino-cli config init --overwrite --dest-file arduino-cli.yaml

    ./arduino-cli config set directories.data data --config-file arduino-cli.yaml

    ./arduino-cli config set directories.user user --config-file arduino-cli.yaml

    ./arduino-cli config set directories.downloads downloads --config-file arduino-cli.yaml

  4. Install the following cores:

    • AVR 1.8.3, SAM 1.6.12, SAMD 1.8.9, Arduino Mbed Nano 4.0.4, Arduino Renesas 1.2.0, and RP2040 4.4.4, using the following command, replacing myCore@myCoreVersion with the core and core version number:

      Operating SystemConfiguration Commands
      Windows

      arduino-cli core install myCore@myCoreVersion --config-file arduino-cli.yaml

      Mac with Apple Silicon Processor and Linux

      ./arduino-cli core install myCore@myCoreVersion --config-file arduino-cli.yaml

      For example, if you are using AVR 1.8.3, enter:

      • For Windows:

        arduino-cli core install arduino:avr@1.8.3 --config-file arduino-cli.yaml

      • For: Mac with Apple Silicon Processor and Linux:

        ./arduino-cli core install arduino:avr@1.8.3 --config-file arduino-cli.yaml

    • ESP32 2.0.17 (only if you are using an ESP32 board):

      Operating SystemCommands to Install the Core
      Windows

      arduino-cli config set board_manager.additional_urls https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json --config-file arduino-cli.yaml

      arduino-cli core search esp32 --config-file arduino-cli.yaml

      arduino-cli core install esp32:esp32@2.0.17 --config-file arduino-cli.yaml

      Mac with Apple Silicon Processor and Linux

      ./arduino-cli config set board_manager.additional_urls https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json --config-file arduino-cli.yaml

      ./arduino-cli core search esp32 --config-file arduino-cli.yaml

      ./arduino-cli core install esp32:esp32@2.0.17 --config-file arduino-cli.yaml

  5. Install the libraries, which are Servo 1.2.2, ACAN2515 2.0.2, Adafruit Motor Shield V2 Library 1.0.4, ArduinoBLE 1.3.6, ArduinoMotorCarrier 2.0.0, MKRMotorCarrier 1.0.1, ServoESP32 1.0.3, WiFi101 0.16.0, and WiFiNINA 1.8.8, using the following commands, replacing myLibrary@myLibraryVersion with the library name and version number.

    Operating SystemCommand to Install the Libraries
    Windows

    arduino-cli lib install myLibrary@myLibraryVersion --config-file arduino-cli.yaml

    Mac with Apple Silicon Processor and Linux

    ./arduino-cli lib install myLibrary@myLibraryVersion --config-file arduino-cli.yaml

    For example, if you are using Servo 1.1.8, enter:

    • For Windows:

      arduino-cli lib install Servo@1.1.8 --config-file arduino-cli.yaml

    • For: Mac with Apple Silicon Processor and Linux:

      ./arduino-cli lib install Servo@1.1.8 --config-file arduino-cli.yaml

  6. Set up the hardware as described in Setup.

  7. Execute the application LCDAddonApplication.

  8. In the Configure Arduino window that opens, select the serial port to which the board is connected. To find the serial port on your operating system, see Find Arduino Port on Windows, Mac, and Linux.

    Configure Arduino

    If you do not want to see this window during deployment, use this command in your script. Replace <PORT> with the hardware port that your board is connected to, <BOARD_NAME> with the name of your board, and <ARDUINO_CLI_PATH> with the Arduino CLI path.

    a = arduino("<PORT>","<BOARD_NAME>",'ArduinoCLIPath',"<ARDUINO_CLI_PATH>"); 
    The ArduinoCLIPath name-value argument is supported only in deployed applications.

  9. Select the board.

  10. Enter the path to the folder where you extracted the Arduino CLI files and click Submit.

The LCD now displays "Hello World!".

See Also

| (MATLAB Compiler) | (MATLAB Compiler) | (MATLAB Compiler) | (MATLAB Compiler SDK)