How To Use Matlab and Simulink With Raspberry Pi
How To Use Matlab and Simulink With Raspberry Pi
with Raspberry Pi
Created by Anuja Apte
https://learn.adafruit.com/how-to-use-matlab-and-simulink-with-raspberry-pi
Overview 3
Resources 15
You can use MATLAB to communicate with a Raspberry Pi board and its peripheral
devices using MATLAB Support Package for Raspberry Pi.
You can program Raspberry Pi boards to run your algorithms using Simulink Support
Package for Raspberry Pi Hardware. The support package generates code from your
Simulink model in a click of a button that then runs on the Raspberry Pi board.
This is the first tutorial in a series on using MATLAB and Simulink to program
a Raspberry Pi. In this tutorial Raspberry Pi 2 is used as an example, however the
same steps can be used for other boards like Raspberry Pi Model B+ etc.
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. Raspberry Pi 2 (http://adafru.it/2358)
2. USB cable (http://adafru.it/2008)
3. Ethernet cable (http://adafru.it/995)
MathWorks Account
Click next and log in to your MathWorks account. If you don't have a MathWorks
account, you can create one during the install process or by visiting this page on the
MathWorks website (https://adafru.it/19oB). (https://adafru.it/d5K)
Firmware Update
Click Next and on the Firmware Update page select the appropriate board (for this
tutorial, Raspberry Pi 2 Model B is chosen).
Click Next and in the configure network screen, select Direct connection to host
computer (to follow along with this tutorial).
In the next window 'Select a drive', the MicroSD cards that are detected by MATLAB
will show up in a list format.
Write firmware
In the next window, select write option to erase existing items on the memory card
and flash the latest firmware that is needed by the Support package.
>> writeLED(mypi,'led0',1)
Here we are sending a request from MATLAB to Raspberry Pi board, to write the
value of 1 on the on-board LED. This should switch ON your LED.
for i = 1:10
writeLED(mypi,'led0',1);
pause(0.5);
Other functions that are related to the MATLAB Support Package for Raspberry Pi can
also be used along with the Raspberry Pi object 'mypi'.
To take a look at the featured examples from MATLAB Support Package for Raspberry
Pi, type raspi_examples in MATLAB Command Window.
Cannot establish an SSH connection to the board with device address "169.254.0.2".
Caused by:
Ensure that the Raspberry Pi board is connected to the computer that you are working
with. Usually when the lights on the ethernet port and the ACT LED are blinking, there
is a connection between the Pi board and the computer.
If for some reason the connection between MATLAB and Raspberry Pi board is lost,
the following error message appears.
The host and client connection is lost. Make sure the board is plugged in and/or recreate
1. Disconnect the USB cable and Ethernet cable from both the Pi board and
computer
2. Clean up MATLAB by typing clear in MATLAB Command Window
3. Plug in the USB cable to computer and Pi board
4. Try connecting to the board from MATLAB by using raspi command
1. In MATLAB, select New > Simulink Model under the HOME Tab
2. Click on the Library Browser Icon on the simulink model and go to Sources tab
under Simulink in the Simulink Library Browser window
3. Drag the Pulse Generator block from the Sources library to your model and change
the values as shown here
6. When you move the mouse pointer close to the arrow on the block, it will change
to a plus sign. Left click when you seee the plus sign and drag the mouse to make the
intended connection. Once the connection is made you can release the left click
button.
8. When the Configuration Parameters page opens up, set the Target hardware
parameter to Raspberry Pi. Do not change any other settings.
9. In your Simulink model, click the Deploy To Hardware button on the toolbar. Code
will be generate from the model, which is then deployed to your Pi 2. The on-board
LED should blink one time every second.
Full tutorials