# Set up and Blink - MATLAB and Simulink with Raspberry Pi

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/024/296/medium800/raspberry_pi_RaspberryPiOverview.png?1428093536)

You can use MATLAB to communicate with a Raspberry Pi&nbsp;board and its peripheral devices using MATLAB Support Package for Raspberry Pi.

You can program Raspberry Pi&nbsp;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&nbsp;board.&nbsp;

This tutorial covers a step-by-step guide to:

MATLAB Support Package&nbsp;

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

Simulink Support Package

1. Set up the Simulink support package for&nbsp;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&nbsp;to blink an LED

This is the first tutorial in a series on using MATLAB and Simulink to program a&nbsp;Raspberry Pi.&nbsp;In this tutorial Raspberry Pi 2&nbsp;is used as an example, however the same steps can be used for other boards like Raspberry Pi Model B+&nbsp;etc.&nbsp;

http://www.youtube.com/watch?v=3Bcw2VzlFA0#t=0m47s

# Set up and Blink - MATLAB and Simulink with Raspberry Pi

## Parts and Hardware

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

- [MathWorks](http://www.mathworks.com/academia/student_version/)
- [Amazon US](http://www.amazon.com/MATLAB-Simulink-Student-Version-R2014a/dp/0989614026/ref=sr_1_1?ie=UTF8&qid=1422487997&sr=8-1&keywords=matlab+student+version)
- [Amazon UK](http://www.amazon.co.uk/MATLAB-Simulink-Student-Version-R2014a/dp/0989614026/ref=sr_1_1?ie=UTF8&qid=1422488152&sr=8-1&keywords=matlab+student+version)

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

- [MATLAB home-use](https://www.mathworks.com/products/matlab-home/)

**Hardware**

1. [Raspberry Pi 2](https://www.adafruit.com/products/2358)&nbsp;
2. [USB cable](https://www.adafruit.com/products/2008)
3. [Ethernet cable](https://www.adafruit.com/product/995)

# Set up and Blink - MATLAB and Simulink with Raspberry Pi

## Set up MATLAB and Simulink support package for Raspberry Pi

 **Start MATLAB**  
Open MATLAB and click the Add-Ons drop down menu on the top right

![](https://cdn-learn.adafruit.com/assets/assets/000/024/305/medium800/raspberry_pi_Picture2.png?1428332623)

 **Start Support Package Installer**  
Click on Get Hardware Support Packages in the drop down menu to start the installer. Select 'Install from Internet' as the&nbsp;source for installing the support package

![](https://cdn-learn.adafruit.com/assets/assets/000/024/306/medium800/raspberry_pi_Picture6.png?1428332718)

 **Select Raspberry Pi&nbsp;from a list of support packages**  
Click Next to see a list of support packages and select Raspberry Pi&nbsp;from the list to install both the support packages at once.

![](https://cdn-learn.adafruit.com/assets/assets/000/024/307/medium800/raspberry_pi_RaspberryPiSelect.png?1428332958)

 **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://www.mathworks.com/mwaccount/register?uri=%2Fmwaccount%2F)[.](https://www.mathworks.com/accesslogin/createProfile.do?uri=http://www.mathworks.com/)

![](https://cdn-learn.adafruit.com/assets/assets/000/024/308/medium800/raspberry_pi_Picture9.png?1428333145)

 **Continue and Complete the Installation**  
Accept the license agreement on the next screen and click Next through the following screens to finish the installation of both MATLAB and Simulink Support Package for Raspberry Pi.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/024/309/medium800/raspberry_pi_RaspberryPiSuccess.png?1428333503)

 **Firmware Update**

Click Next and on&nbsp;the Firmware Update page select the appropriate board (for this tutorial, Raspberry Pi 2 Model &nbsp;B is chosen).&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/024/313/medium800/raspberry_pi_RaspUpdateFirmware.png?1428436271)

 **Configure Network**

Click Next and in the configure network screen, select Direct connection to host computer (to follow along with this tutorial).&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/024/314/medium800/raspberry_pi_RaspConfigNetw.png?1428436588)

 **Select the drive**

In the next window 'Select a drive', the MicroSD cards that are detected by MATLAB will show up in a list format.&nbsp;

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 `targetupdater` command can be used in MATLAB.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/024/315/medium800/raspberry_pi_RaspSelectDrive.png?1428436859)

 **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&nbsp;the Support package.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/024/318/medium800/raspberry_pi_RaspWriteFirm-1.png?1428437197)

 **Continue and Complete the Installation**  
Click Next through the following screens to finish the installation for both MATLAB and Simulink Support Package for Raspberry Pi.

# Set up and Blink - MATLAB and Simulink with Raspberry Pi

## Using MATLAB Support Package and Raspberry Pi

Now that we have the necessary software installed, it's time to blink the on-board&nbsp;LED. In this example,&nbsp;we will use the MATLAB support package.&nbsp;

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

` >> mypi = raspi`

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

![](https://cdn-learn.adafruit.com/assets/assets/000/024/444/medium800/raspberry_pi_RaspiConn.png?1428940142)

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

`>> writeLED(mypi,'led0',1)`

Here we are sending&nbsp;a request from MATLAB to Raspberry Pi board, to write the value of 1 on the on-board LED. This should switch ON&nbsp;your LED.

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

`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&nbsp;can also be&nbsp;used along with&nbsp;the Raspberry Pi object 'mypi'.

To take a look at the&nbsp;featured examples from MATLAB Support Package for Raspberry Pi,&nbsp;type `raspi_examples`&nbsp;in MATLAB Command Window.&nbsp;

## Common Error Messages

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

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

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.&nbsp;

If for some reason the connection between MATLAB and Raspberry Pi&nbsp;board is lost, the following error message appears.&nbsp;

<sub>The host and client connection is lost. Make sure the board is plugged in and/or recreate arduino and its related objects.</sub>

The best practices to follow to resolve this error -

1. Disconnect the USB cable and Ethernet cable from both the Pi board and computer&nbsp;
2. Clean up MATLAB by typing `clear`&nbsp;in MATLAB Command Window
3. Plug in the USB cable to computer and&nbsp;Pi board
4. Try connecting to the board from MATLAB by using `raspi`&nbsp;command&nbsp;

# Set up and Blink - MATLAB and Simulink with Raspberry Pi

## Using Simulink Support Package and Raspberry Pi

 **Create your own Simulink model**

1. In MATLAB, select **New \> Simulink Model** &nbsp;under the HOME Tab

![](https://cdn-learn.adafruit.com/assets/assets/000/024/445/medium800/raspberry_pi_newSimulinkModel.png?1428940577)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/024/446/medium800/raspberry_pi_SourcesTab.png?1428940863)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/024/448/medium800/raspberry_pi_PulseGen.png?1428949845)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/024/449/medium800/raspberry_pi_LEDinLib.png?1428950050)

![](https://cdn-learn.adafruit.com/assets/assets/000/024/450/medium800/raspberry_pi_LEDBlockParam.png?1428950340)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/024/451/medium800/raspberry_pi_ConvBlock.png?1428950965)

6. When you move the mouse pointer close to the&nbsp;arrow on the block, it will change to a plus sign. Left click when you seee the plus sign and drag the mouse&nbsp;to make the intended connection. Once the connection is made you can release the left click button.&nbsp;&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/024/452/medium800/raspberry_pi_myBlinkingLED.png?1428951212)

7. Save this model and to configure the model go to **Tools \> Run on Target Hardware \> Prepare To Run...** &nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/024/453/medium800/raspberry_pi_PrepareTarget.png?1428951355)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/024/454/medium800/raspberry_pi_configModel.png?1428951521)

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.&nbsp;The on-board LED should blink one time every second.

![](https://cdn-learn.adafruit.com/assets/assets/000/024/455/medium800/raspberry_pi_DeployToRas.png?1428951692)

# Set up and Blink - MATLAB and Simulink with Raspberry Pi

## Resources

 **Examples for getting started**

- [MATLAB examples](http://www.mathworks.com/products/matlab/examples.html)
- [Simulink examples](http://www.mathworks.com/products/simulink/model-examples-b.html)

**Full tutorials**

- [MATLAB Getting Started Tutorial](http://www.mathworks.com/academia/student_center/tutorials/index.html "Link: http://www.mathworks.com/academia/student\_center/tutorials/index.html") (3 hrs 15 mins)
- [Simulink Getting Started Tutorial](http://www.mathworks.com/academia/student_center/tutorials/index.html "Link: http://www.mathworks.com/academia/student\_center/tutorials/index.html") (2 hrs 15 mins - 10 mins per module)


## Featured Products

### Raspberry Pi 2 - Model B v1.2 - ARM Cortex-A53 with 1G RAM

[Raspberry Pi 2 - Model B v1.2 - ARM Cortex-A53 with 1G RAM](https://www.adafruit.com/product/2358)
Didn't think the Raspberry Pi could get any better? You're in for a big surprise! The Raspberry Pi 2 Model B is out and it's amazing! With an upgraded ARM Cortex-A53&nbsp;quad-core processor, Dual Core VideoCore IV Multimedia coprocessor, and a full Gigabyte of RAM, this...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/2358)
[Related Guides to the Product](https://learn.adafruit.com/products/2358/guides)
### USB Patterned Fabric Cable - A/MicroB

[USB Patterned Fabric Cable - A/MicroB](https://www.adafruit.com/product/2008)
Oooh it's so soft! And that beautiful&nbsp;pattern! If you have to have visible cables&nbsp;then you might as well have the nicest fabric bound cable with a eye-catching snake-like pattern. &nbsp;That's why we now carry this standard A to micro-B USB cable&nbsp;for USB 1.1 or 2.0....

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/2008)
[Related Guides to the Product](https://learn.adafruit.com/products/2008/guides)
### Ethernet Cable - 3 ft long

[Ethernet Cable - 3 ft long](https://www.adafruit.com/product/995)
We have so many Internet-connected goodies in the shop, we figured it's time to carry a cable so you can easily connect them up! This cable is 3 feet long, comes in Adafruit black and has all 8 wires installed. Perfect for use with the <a...></a...>

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/995)
[Related Guides to the Product](https://learn.adafruit.com/products/995/guides)

## Related Guides

- [OctoPrint - Open Source Host Software](https://learn.adafruit.com/octoprint-open-source-host-software.md)
- [Introducing the Raspberry Pi 2 - Model B](https://learn.adafruit.com/introducing-the-raspberry-pi-2-model-b.md)
- [Pi Box](https://learn.adafruit.com/pi-box.md)
- [apt.adafruit.com](https://learn.adafruit.com/apt-adafruit-com.md)
- [Node.js Embedded Development on the Raspberry Pi](https://learn.adafruit.com/node-embedded-development.md)
- [PiGRRL - Raspberry Pi Gameboy](https://learn.adafruit.com/pigrrl-raspberry-pi-gameboy.md)
- [3D Printed Raspberry Pi A+ Case](https://learn.adafruit.com/3d-printed-raspberry-pi-a-plus-case.md)
- [Send Raspberry Pi Data to COSM](https://learn.adafruit.com/send-raspberry-pi-data-to-cosm.md)
- [Bluefruit LE Python Library](https://learn.adafruit.com/bluefruit-le-python-library.md)
- [Adafruit WebIDE](https://learn.adafruit.com/webide.md)
- [Raspberry Pi Video Looper](https://learn.adafruit.com/raspberry-pi-video-looper.md)
- [Adafruit 16-Channel PWM/Servo HAT & Bonnet for Raspberry Pi](https://learn.adafruit.com/adafruit-16-channel-pwm-servo-hat-for-raspberry-pi.md)
- [Reverse Engineering a Bluetooth Low Energy Light Bulb](https://learn.adafruit.com/reverse-engineering-a-bluetooth-low-energy-light-bulb.md)
- [Adafruit's Raspberry Pi Lesson 8. Using a Servo Motor](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-8-using-a-servo-motor.md)
- [Raspberry Pi NFC Minecraft Blocks](https://learn.adafruit.com/raspberry-pi-nfc-minecraft-blocks.md)
