Deploy Arduino Functions to Arduino Hardware Using MATLAB Function Block
This example shows how to program a MATLAB® Function block to access multiple peripherals on the Arduino® hardware.
The MATLAB Function block enables you to implement custom MATLAB functions in a Simulink® model. You can use the MATLAB Function block to generate readable, efficient, and compact C/C++ code that can be deployed to the Arduino hardware.
For a detailed overview of the MATLAB Function block, see Implement MATLAB Functions in Simulink with MATLAB Function Blocks and Implement MATLAB Functions in Simulink with MATLAB Function Blocks.
Using the MATLAB Function block for code generation provides:
Scheduling and Multitasking – You can set the sample rate of the MATLAB Function block. The generated code runs on the hardware at the specified sample rate. With the multitasking mode On, you can run different blocks at different rates on the hardware. See Treat each discrete rate as a separate task for more information.
Monitor and Tuning – You can monitor signals and tune parameters by using the Monitor & Tune feature in the Simulink model and observe or log the output signal.
Supported Hardware
MATLAB Function blocks support code generation on these boards:
Arduino Due
Arduino Nano 3.0
Arduino Uno
Arduino Mega 2560
Arduino Mega ADK
Arduino Leonardo
Arduino Micro
Arduino MKR1000
Arduino MKR1010
Arduino MKR Zero
Required Products
Deploying functions on Arduino Hardware by using the MATLAB Function block requires these support packages:
Simulink Support Package for Arduino Hardware
MATLAB Support Package for Arduino Hardware
Deploy MATLAB IO Functions on Arduino Hardware
Deploy a function that sets the state of an LED pin based on its input, generates PWM signals, and reads the analog voltage by using the functions listed in Read and Write Data (MATLAB Support Package for Arduino Hardware).
To configure the model on the Arduino board:
Open the Simulink model. At the MATLAB Command Window, enter:
arduino_matlab_codegen
On the Hardware tab, in the Prepare section, click Hardware Settings to open the Configuration Parameters dialog box.
Select the Hardware Implementation pane and select your Arduino hardware from the Hardware board parameter list. Do not change any other settings. Click OK.
To see the function written in the editor, double click the MATLAB Function block.
Tip
Before you deploy the function, you must validate the function. See Run the MATLAB Function.
To build the model and run it on the hardware, on the Hardware tab of the Simulink model, in the Mode section, select
Run on board
, and then click Build, Deploy & Start.To perform signal monitoring and parameter tuning, on the Hardware tab of the Simulink model, in the Mode section, select
Run on board
, and then click Monitor & Tune. For example, you can change the PWM duty cycle and observe the generated PWM signal or you can connect the analog pins to VCC or GND and log the analog voltages in the scope.
Follow the preceding steps for the
arduino_matlab_codegen_initblock
model. In this model, all
pin configurations you must perform at the beginning of a program execution are in
the init
block. Configuring the pins in the
init
block makes the function in the MATLAB function block modular.
Run the MATLAB Function
Before deploying the function, run the function in the MATLAB Command Window. You can then:
Verify that the MATLAB function communicates with the hardware as expected.
Detect run-time errors, such as peripheral conflicts, that are harder to diagnose during deployment.
Limitations
Invalid PWM Pins
These PWM pin numbers are not allowed inside a MATLAB Function block.
Board | PWM Pins |
---|---|
Arduino Mega 2560, Mega ADK | 9,10 |
Arduino Leonardo, Micro | 5 |
Arduino Uno, Nano3.0 | 3,11 |
Arduino Due | 11,12 |
Arduino MKR1000, MKR WiFi 1010, MKR Zero | 4,5 |
If you use a PWM function from the MATLAB Support Package for Arduino Hardware in the MATLAB function block, then do not use the pins listed in this table in the Arduino PWM blocks from the Simulink Support Package for Arduino Hardware. Simulink requires a timer for scheduling on the Arduino hardware. When a PWM function is used in the MATLAB Function Block to generate a PWM signal, a timer is chosen for scheduling on the Arduino hardware. The PWM pins connected to that timer therefore cannot be used.
Connected I/O
If you run Connected I/O on your model that has the MATLAB Function block with Read and Write Data (MATLAB Support Package for Arduino Hardware) functions and other Simulink Arduino blocks, Connected I/O does not run on the MATLAB Function block. For the other Simulink Arduino blocks, Connected I/O runs normally.
Resource Conflicts
To avoid resource conflicts:
Before you access the Arduino peripherals, set the mode of the pins by using
configurePin
(MATLAB Support Package for Arduino Hardware) to the mode that you plan to use.Using the same pin across different peripherals in a model may lead to undefined behaviour. For example, if you are using
readDigitalPin
(MATLAB Support Package for Arduino Hardware) to read digital input from a pin in the MATLAB Function block, the same pin is not allowed as an output pin in another block in your model.