This example shows how to program a MATLAB Function block in Simulink® Support Package for Arduino® Hardware 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 Implementing MATLAB Functions Using Blocks and Create Custom Functionality Using MATLAB Function Block.
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.
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.
MATLAB Function blocks support code generation on these boards:
Arduino Nano 3.0
Arduino Mega 2560
Arduino Mega ADK
Arduino MKR Zero
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 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.
To configure the model on the Arduino board:
Open the Simulink model. At the MATLAB command prompt, enter:
On the Hardware tab, 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.
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, click Build, Deploy & Start on the Hardware tab.
To perform signal monitoring and parameter tuning, click Monitor & Tune on the Hardware tab. 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
init block. Configuring the pins in the
init block makes the function in the MATLAB function block
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.
To add a delay in your function inside the MATLAB Function block, use this snippet:
if coder.target('rtw') coder.ceval('delay', duration); end
durationholds the delay value in milliseconds. Ensure that the total execution time, including the delay, of the MATLAB Function block on the hardware is less than the sample time to avoid overrun condition.
These PWM pin numbers are not allowed inside a MATLAB Function block.
|Arduino Mega 2560, Mega ADK||9,10|
|Arduino Leonardo, Micro||5|
|Arduino Uno, Nano3.0||3,11|
|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.
If you run Simulink IO on your model that has the MATLAB Function block with Read and Write Data functions and other Simulink Arduino blocks, Simulink IO does not run on the MATLAB Function block. For the other Simulink Arduino blocks, Simulink IO runs normally.
To avoid resource conflicts:
Before you access the Arduino peripherals, set the mode of the
pins by using
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
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
You can use I2C and SPI functions inside the MATLAB Function block. See Gyroscope-Based Pedometer Using MATLAB Functions for more details.