element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
STEM Academy
  • Learn
  • Learning Center
  • STEM Academy
  • More
  • Cancel
STEM Academy
Documents Set up and Blink - MATLAB and Simulink with Raspberry Pi
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join STEM Academy to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Engagement
  • Author Author: madhugovindarajan
  • Date Created: 17 Apr 2015 3:52 PM Date Created
  • Last Updated Last Updated: 20 May 2016 3:42 AM
  • Views 3818 views
  • Likes 1 like
  • Comments 11 comments
Related
Recommended

Set up and Blink - MATLAB and Simulink with Raspberry Pi

Overview

image

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 tutorial covers a step-by-step guide to:

MATLAB Support Package

  1. Set up the MATLAB support package for Raspberry Pi.
  2. Send commands from MATLAB to Raspberry Pi to blink the on-board LED.

Simulink Support Package

  1. Set up the Simulink support package for Raspberry Pi.
  2. Build a simple Simulink model for controlling pins on the Raspberry Pi.
  3. Generate, download and run code on the Raspberry Pi to blink the on-board LED.

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.

 

Software

MATLAB and Simulink Student Suite Release 2015a - now available for $99

  • MathWorks
  • Amazon US
  • Amazon UK

If you are not a student, you can purchase the MATLAB Home-Use license for personal use:

  • MATLAB home-use

Hardware

  1. Raspberry Pi 2
  2. USB Cable
  3. Ethernet cable

 

This video is unavailable.
You don't have permission to edit metadata of this video.
 
You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Setup MATLAB and Simulink Support Packages for Raspberry Pi

1) Open MATLAB and click the Add-Ons drop down menu on the top right. Click on Get Hardware Support Packages in the drop down menu to start the installer.


image

2) Select 'Install from Internet' as the source for installing the support package and click next.

image

3) On the list of support packages select Raspberry Pi and click Next, to install both the support packages at once.

image

4) 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.

image

5) Once you accept the license agreement on the next screen, click Next through the following screens to finish the installation of both MATLAB and Simulink Support Package for Raspberry Pi.

image

6) On the Firmware Update page, select the appropriate board (for this tutorial, Raspberry Pi 2 Model  B is chosen).

image

7) In the configure network screen, select Direct connection to host computer (to follow along with this tutorial).

image

8) In the next window titled 'Select a drive', the MicroSD cards that are detected by MATLAB will show up in a list format. If the MicroSD memory card does not get detected by MATLAB, but is detected by the OS - close MATLAB and restart MATLAB as an administrator. To continue with the process, the targetupdatercommand can be used in MATLAB.

imageimage

9) Select write option to erase existing items on the memory card and flash the latest firmware that is needed by the Support package. Click Next through the following screens to finish the installation for both MATLAB and Simulink Support Package for Raspberry Pi.

Blinking On-board LED Using MATLAB Support Package for Raspberry Pi

To establish a connection between MATLAB and Pi 2, the following command can be used from MATLAB.

>> mypi = raspi

This command connects MATLAB to the most recent successful connection to a Raspberry Pi board, including the setup process. A MATLAB variable by the name 'mypi' should now appear on the workspace or an error message indicating what went wrong. This object represents the connection that MATLAB has established with the physical object (Raspberry Pi).

image

To switch on the on-board LED connected, execute the following command

>> 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.

To blink the LED, we have to switch it ON and OFF periodically.

for i = 1:10
    writeLED(mypi,'led0',1);
    pause(0.5);
    writeLED(mypi,'led0',0);
    pause(0.5);
end

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.

Common Error Messages

Error using raspi (line 157)
Cannot establish an SSH connection to the board with device address "169.254.0.2".

Caused by:
Error using raspi (line 153)
Error executing command: FATAL ERROR: Network error: Network is unreachable

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 arduino and its related objects.

The best practices to follow to resolve this error -

  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.

 

Blinking On-board LED Using Simulink Support Package for Raspberry Pi


Create your own Simulink model

1) In MATLAB, select New > Simulink Model under the HOME Tab.

image

2) Click on the Library Browser Icon on the simulink model and go to Sources tab under Simulink in the Simulink Library Browser window.

image

3) Drag the Pulse Generator block from the Sources library to your model and change the values as shown here.

image

4) Use the LED Block from Simulink Support Package for Raspberry Pi Hardware in the Library Browser and change the parameters as shown here.

imageimage

5) Drag and drop the Data Type Conversion block from the Signal Attributes tab under the Simulink library.

image

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. 

image

7)  Save this model and to configure the model go to Tools > Run on Target Hardware > Prepare To Run...


image

8) When the Configuration Parameters page opens up, set the Target hardware parameter to Raspberry Pi. Do not change any other settings.


image

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.


image

  • stem space
  • Share
  • History
  • More
  • Cancel
  • Sign in to reply
  • waleedbintayyab
    waleedbintayyab over 4 years ago

    Hi. I am currently working on a FYP project in which I am using Rasberry pi 4 and simulink and connection is wireless. I want to connect my Pi with other network and my laptop with different network.. but after running the program error is generating.. but when both Rasberry and my laptop are connected via same IP address program is running fine , but my main task is to connect pi and laptop on different network and send simulink data in real time. Please can you guide me?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shinji722
    shinji722 over 8 years ago in reply to madhugovindarajan

    Thank you so much for your time!

    I have finally solved the problem by reinstalling MATLAB...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shinji722
    shinji722 over 8 years ago in reply to madhugovindarajan

    Thanks a lot!

    I have changed the Putty default mode to be SSH, but when I try to run the simulink model, a new error occurred...

    image

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • madhugovindarajan
    madhugovindarajan over 8 years ago in reply to shinji722

    Did you have a previous installation of putty on your machine? Could you check if the default setting on it? I am guessing it is serial and that is why this is happening. Could you try changing it to SSH?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shinji722
    shinji722 over 8 years ago in reply to madhugovindarajan

    Thank you, I really appreciate your help!

    1. The MATLAB I am using is R2016a;

    2. The demo was downloaded from Simulink help examples;

    3. The Pi was directly connected with PC via Ethernet cable;

    4. Yes, the connection is correct, I have tried the following MATLAB code to control the LED, and it worked correctly:

    mypi=raspi;

    for i=1:20

        writeLED(mypi,'led0',1);

        pause(0.2);

        writeLED(mypi,'led0',0);

        pause(0.2);

    end

    5. The Raspberry Pi I am using is Pi 3 model B, and I have selected the right board in the LED block.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • madhugovindarajan
    madhugovindarajan over 8 years ago in reply to shinji722

    Could you please give me more information on this -

     

    1) What release of MATLAB are you using?

    2) Were you able to download any shipped demo from Simulink to the Raspberry Pi (example - https://www.mathworks.com/help/supportpkg/raspberrypi/examples/communicating-with-raspberry-pi-hardware.html )?

    3) What type of connection are you using between the Pi and the PC?

    4) Before doing Simulink stuff with the Pi, are you ensuring that the MATLAB's connection (if any) to the Pi is cleared?

    5) Have you ensured that the right kind of Pi (Pi 2, Pi 3 etc.) was selected in the Simulink blocks? 

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shinji722
    shinji722 over 8 years ago in reply to madhugovindarajan

    Thank you for your response.

    In the experiment, I have set the mode to be External, and deployed to the hard ware.

    Additionally, I have checked the settings in Tools>Run on Target Hardware, the IP address, login username and keyword are correct.

     

    But the error is still there...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • madhugovindarajan
    madhugovindarajan over 8 years ago in reply to shinji722

    When you say run the Blink model on Pi are you deploying to the hardware or are you trying to communicate with the Pi from Simulink? If it is the second one, are you using External mode on your Simulink model and not Normal mode?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shinji722
    shinji722 over 8 years ago

    Very nice guide!

     

    However, I have encountered an error when I try to run the Blink model on my Pi, which says "Error executing command: plink: the -pw option can only be used with the SSH protocol".

     

    One thing interesting that it works when I used Matlab codes to control the LED.

     

    Could you please tell me how to solve this error?

     

    Thanks a lot!

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • madhugovindarajan
    madhugovindarajan over 10 years ago in reply to clem57

    Hi clem57 thanks for the comment. Here are the advantages that made me like the software -

     

    It is easy to get started with MATLAB as it is a high level computing language. There are a lot of toolboxes (libraries, if you will) that can simplify workflows in the users respective domains (Image Processing, Computer Vision etc). MATLAB Support package for Raspberry Pi helps in data acquisition from sensors/devices connected to the Pi without having to compile code for every modification. Here is an example of a simple project that I have worked on in the past which shows an image acquisition application and takes advantage of Computer Vision System Toolbox - Holiday Pi!

     

    Simulink on the other hand is a graphical programming language (helped me, a Mechanical engineer, not an expert with any of the traditional programming languages J). The Simulink Support package for Raspberry Pi helps generate code that can serve as Standalone applications on the Raspberry Pi. There are libraries in Simulink as well which can do a wide variety of things like Signal Processing, Controller Design etc. For example, you can design a PID controller using one block and tune the controller while the code is still running on Raspberry Pi.

     

    There are support packages that helps you similarly with other low-cost hardware (like Arduino, LEGO etc) as well, so I had to learn only one software to MAKE using all these boards J

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube