How To Use Matlab and Simulink With Arduino
How To Use Matlab and Simulink With Arduino
Guide Contents 2
Overview 3
Parts and Software 4
Build the circuit 5
Set up MATLAB and Simulink support package for Arduino Due 6
Blink an LED Using MATLAB Support Package and Arduino 9
Common Error Messages 9
Set up compiler support 11
Simulink model 12
Generate code, Load and Run 14
Resources 16
You can use MATLAB to communicate with an Arduino board using MATLAB Support Package for Arduino.
You can program an Arduino board using Simulink Support Package for Arduino. The support package automatically
generates code from your Simulink model that then runs on the Arduino board (in a click of a button).
This is the first tutorial in a series on using MATLAB and Simulink to program an Arduino. In this tutorial Arduino Due is
used as an example, however the same steps can be used for other boards like Uno, Mega 2560 etc.
Software
MATLAB and Simulink Student Suite Release 2015a - now available for $99
MathWorks (https://adafru.it/d5E)
Amazon US (https://adafru.it/eoc)
Amazon UK (https://adafru.it/eod)
If you are not a student, you can purchase the MATLAB Home-Use license for personal use:
Hardware
1. Small breadboard (http://adafru.it/64)
2. 220 Ohm resistor
3. LED (http://adafru.it/845)
4. Connecting wires (http://adafru.it/153)
5. Arduino Due (http://adafru.it/50)
6. USB cable (http://adafru.it/2008)
Use the following image to build a simple circuit that connects an LED to pin 9 of Arduino Due.
Using the USB connector, connect Arduino Due to the computer that has a MATLAB & Simulink installation.
To establish a connection between MATLAB and Arduino boards, the following command can be used from MATLAB.
>> a = arduino
This command connects MATLAB to the first Arduino detected on the system. A MATLAB variable by the name 'a'
should now appear on the workspace or an error message indicating what went wrong.
To switch on the LED connected to Pin 9 on the Due, execute the following command
>> writeDigitalPin(a, 9, 1)
Here we are sending a request from MATLAB to Arduino, to write on the Digital Pin 9 the value of 1. This should switch
ON your LED.
for i = 1:10
writeDigitalPin(a, 9, 1);
pause(0.5);
writeDigitalPin(a, 9, 0);
pause(0.5);
end
Other functions that are related to the MATLAB Support Package for Arduino can also be used along with the arduino
object 'a'.
To take a look at the featured examples from MATLAB Support Package for Arduino type arduinoExamples in MATLAB
Command Window.
As mentioned in the error message, it is good practice to mention the COM port at which the Due is connected to the
computer. For Windows Users, Control Panel > Hardware and Sound > Devices and Printers.
>> a = arduino('COM4','Due')
The host and client connection is lost. Make sure the board is plugged in and/or recreate arduino and its related objects.
The other common error message that one can encounter during the stage of reconnection is -
To resolve this error message, repeat all the steps from before after typing clear all in MATLAB Command Window. In
the event that this also does not help, close MATLAB and reopen before proceeding further.
Simulink support package for Arduino is supported on 32-bit and 64-bit Microsoft Windows and Mac OS X. For the
code generation process to work, a supported compiler installation is required. Refer to the following page on
MathWorks website for a list of supported compilers on Windows and Mac OS:
For example, you can download a Simulink supported compiler from the Microsoft website (https://adafru.it/d5J) for
free.
Enter mex -setup at the MATLAB command prompt to start the compiler set up. When the compiler set up is complete,
you should see on MATLAB Command Window the compiler being used.
Enter arduinodue_gettingstarted at the MATLAB Command Window to open a demo model. Again, in this tutorial
Arduino Due is used as an example but the same steps can be used for other boards like Uno, Mega 2560 etc.
Double-click the Pulse Generator block to explore its pre-configured parameters. The Digital Output block is set up to
send the output of the Pulse Generator to pin 9 of the Arduino Due. Double-click the block to explore its pre-
configured parameters.
Click on the Simulink Library icon to open the Simulink library browser
In the left navigation pane, select Simulink Support Package for Arduino Hardware
On the right navigation pane, double-click the [Examples] block to open the documentation
Full tutorials