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 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. 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.
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 (MATLAB Support Package for Arduino Hardware).
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.
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 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.
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 acorss 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