# Set up and Blink - MATLAB and Simulink with Arduino

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/024/295/medium800/learn_arduino_ArduinoOverview.png?1428093334)

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 &nbsp;Simulink model that then runs on the Arduino board (in a click of a button).&nbsp;

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

MATLAB Support Package&nbsp;

1. Set up the MATLAB support package for Arduino&nbsp;
2. Send commands from MATLAB to Arduino to blink an LED

Simulink Support Package

1. Set up the Simulink support package for Arduino&nbsp;
2. Build a simple Simulink model for controlling pins on the Arduino
3. Generate, download and run code on the Arduino to blink an LED

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

http://www.youtube.com/watch?v=9ttIZoUXCKY

# Set up and Blink - MATLAB and Simulink with Arduino

## Parts and Software

 **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. [Small breadboard](http://www.adafruit.com/products/64 "Link: http://www.adafruit.com/products/64")
2. 220 Ohm resistor
3. [LED](http://www.adafruit.com/products/845 "Link: http://www.adafruit.com/products/845")
4. [Connecting wires](http://www.adafruit.com/products/153 "Link: http://www.adafruit.com/products/153")
5. [Arduino Due](http://www.adafruit.com/products/50 "Link: http://www.adafruit.com/products/50")&nbsp;
6. [USB cable](https://www.adafruit.com/products/2008)

# Set up and Blink - MATLAB and Simulink with Arduino

## Build the circuit

 **Circuit**

Use the following image to build a simple circuit that connects an LED to pin 9 of Arduino Due.

**Connect the hardware**

Using the USB connector, connect Arduino Due&nbsp;to the computer that has a MATLAB & Simulink installation.

![](https://cdn-learn.adafruit.com/assets/assets/000/010/675/medium800/learn_arduino_CircuitDiagram.png?1377797046)

# Set up and Blink - MATLAB and Simulink with Arduino

## Set up MATLAB and Simulink support package for Arduino Due

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

![](https://cdn-learn.adafruit.com/assets/assets/000/022/783/medium800/learn_arduino_Picture2.png?1422570520)

 **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/022/787/medium800/learn_arduino_Picture6.png?1422573360)

 **Select Arduino from a list of support packages**  
Click Next to see a list of support packages and select Arduino from the list for MATLAB Support Package and Arduino Due&nbsp;for the Simulink Support Package.

![](https://cdn-learn.adafruit.com/assets/assets/000/022/788/medium800/learn_arduino_Picture7.png?1422573608)

![](https://cdn-learn.adafruit.com/assets/assets/000/022/789/medium800/learn_arduino_Picture8.png?1422573649)

 **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/022/790/medium800/learn_arduino_Picture9.png?1422573705)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/022/757/medium800/learn_arduino_SuccessMATLAB.png?1422490259)

![](https://cdn-learn.adafruit.com/assets/assets/000/022/764/medium800/learn_arduino_SuccessSimulink.png?1422491838)

# Set up and Blink - MATLAB and Simulink with Arduino

## Blink an LED Using MATLAB Support Package and Arduino

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

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

` >> a = arduino`

This command connects MATLAB to the first Arduino detected on the system. A&nbsp;MATLAB variable by the name 'a' should now appear on the&nbsp;workspace or an error message indicating what went wrong.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/022/792/medium800/learn_arduino_Picture10.png?1422574670)

To switch on&nbsp;the LED connected to Pin 9 on the Due, execute the following command&nbsp;

`>> writeDigitalPin(a, 9, 1)`

Here we are sending&nbsp;a request from MATLAB to Arduino, to write on the Digital Pin 9 the value of 1. 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`  
`    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&nbsp;used along with&nbsp;the arduino object 'a'.

To take a look at the&nbsp;featured examples from MATLAB Support Package for Arduino type `arduinoExamples`&nbsp;in MATLAB Command Window.&nbsp;

## Common Error Messages

<sub>Cannot detect Arduino hardware. Make sure original Arduino hardware is properly plugged in. If using unofficial(clone) hardware, specify port and board type. For more information, see this user's guide for additional troubleshooting steps.</sub>

As mentioned in the error message, it is good practice to mention the COM port at which the Due is connected to the computer.&nbsp;For Windows Users, **Control Panel \> Hardware and Sound&nbsp;\> Devices and Printers**.

`>> a = arduino('COM4','Due')`

If for some reason the connection between MATLAB and Arduino 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 from both Arduino and computer&nbsp;
2. Clean up MATLAB by typing `clear`&nbsp;in MATLAB Command Window
3. Plug in the USB cable to computer and Arduino
4. Try connecting to the Arduino from MATLAB by using `arduino` command&nbsp;

The other common error message that one can encounter during the stage of reconnection is -&nbsp;

<sub>Failed to open serial port COM4 to communicate with Arduino board Due. Make sure there is no other MATLAB arduino object for this board. For troubleshooting, see Arduino Hardware Troubleshooting.</sub>

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

# Set up and Blink - MATLAB and Simulink with Arduino

## Set up compiler support

 **Install a supported compiler**

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:  
  
[List of supported compilers](http://www.mathworks.com/support/compilers/R2014a/index.html)  
  
For example, you can download a Simulink supported compiler from the [Microsoft website](http://www.microsoft.com/en-us/download/details.aspx?id=8279 "Link: http://www.microsoft.com/en-us/download/details.aspx?id=8279") for free.

**Compiler set up in MATLAB**

Enter `mex -setup` at the MATLAB command prompt to start the compiler set up. When&nbsp;the compiler set up is complete, you should see on MATLAB Command Window the compiler being used.

![](https://cdn-learn.adafruit.com/assets/assets/000/022/784/medium800/learn_arduino_Picture3.png?1422572440)

# Set up and Blink - MATLAB and Simulink with Arduino

## Simulink model

 **Open a Simulink demo model**  
  
Enter `arduinodue_gettingstarted` at the MATLAB Command Window&nbsp;to open a demo model.&nbsp;&nbsp;Again, in this tutorial Arduino Due is used as an example but&nbsp;the same steps can be used for other boards like Uno, Mega 2560 etc.&nbsp;

Double-click the Pulse Generator block to explore its pre-configured parameters.&nbsp;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.   
  
You are now ready to program Arduino boards!

![](https://cdn-learn.adafruit.com/assets/assets/000/022/760/medium800/learn_arduino_arduinoDueGettingStarted.png?1422491086)

 **Optional - Create the model on your own**

- 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&nbsp;to open the documentation

![](https://cdn-learn.adafruit.com/assets/assets/000/022/785/medium800/learn_arduino_Picture4.png?1422572575)

 **Open step-by-step guide**  
Click 'Getting started with Arduino Due&nbsp;Hardware' in the list of demos to open the step-by-step guide to build this model on your own.

![](https://cdn-learn.adafruit.com/assets/assets/000/022/786/medium800/learn_arduino_Picture5.png?1422572702)

# Set up and Blink - MATLAB and Simulink with Arduino

## Generate code, Load and Run

If you opened the demo model, you can go straight to Run the model on Arduino section. If you followed the steps to create your own model, then to configure the model for Arduino boards follow this section.

**Prepare the model to run on Arduino boards**

- Connect the Arduino board to your computer with a USB cable
- Select **Tools \> Run on Target Hardware \> Prepare to Run...**
- Review the parameters in the dialog that opens
- Unless already set, set the Target hardware parameter to the&nbsp;Arduino board being used (Arduino Due here)
- Do not change any other settings
- Click OK
- Save the model

**Run the model on Arduino**

- Click on the 'Deploy to Hardware' button as shown in the figure below
- You should see the LED on Pin 9 blinking

![](https://cdn-learn.adafruit.com/assets/assets/000/022/796/medium800/learn_arduino_Picture11.png?1422575791)

![](https://cdn-learn.adafruit.com/assets/assets/000/022/766/medium800/learn_arduino_SimulinkArduinoDueBlinkingLED.png?1422542075)

# Set up and Blink - MATLAB and Simulink with Arduino

## 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)

**Open Examples for the Support Package**

![](https://cdn-learn.adafruit.com/assets/assets/000/022/793/medium800/learn_arduino_supplemental_software_button.png?1422574897)

![](https://cdn-learn.adafruit.com/assets/assets/000/022/794/medium800/learn_arduino_supplemental_software_examples.png?1422574916)

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

### Super Bright Red 5mm LED (25 pack)

[Super Bright Red 5mm LED (25 pack)](https://www.adafruit.com/product/297)
Need some really bright LEDs? We are big fans of these clear red LEDs, in fact we use them exclusively in our kits. They are very bright and have about 20degree LED beam. They go easily into a breadboard and will add that extra zing to your project.

- Pack of 25 clear red...

In Stock
[Buy Now](https://www.adafruit.com/product/297)
[Related Guides to the Product](https://learn.adafruit.com/products/297/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

Out of Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Breadboarding wire bundle

[Breadboarding wire bundle](https://www.adafruit.com/product/153)
75 flexible stranded core wires with stiff ends molded on in red, orange, yellow, green, blue, brown, black and white. These are a major improvement over the "box of bent wires" that are sometimes sold with breadboards, and faster than stripping your own solid core wires. Makes...

In Stock
[Buy Now](https://www.adafruit.com/product/153)
[Related Guides to the Product](https://learn.adafruit.com/products/153/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)
### Arduino Due - assembled

[Arduino Due - assembled](https://www.adafruit.com/product/1076)
Put your Arduino project on TURBO mode with the high-speed, high-power Arduino Due! The Due cranks it up to 11 with an 84 MHz ARM core processor - 512K of FLASH storage! 96K of RAM! Both USB client and host! The Arduino Due is ideal for those who want to build projects that require high...

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

## Related Guides

- [Multi-tasking the Arduino - Part 1](https://learn.adafruit.com/multi-tasking-the-arduino-part-1.md)
- [Arduino Lesson 6. Digital Inputs](https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs.md)
- [Arduino Lesson 4. Eight LEDs and a Shift Register](https://learn.adafruit.com/adafruit-arduino-lesson-4-eight-leds.md)
- [Programmable 12v Outdoor Cafe Lights](https://learn.adafruit.com/programmable-12v-outdoor-cafe-lights.md)
- [Arduino Lesson 8. Analog Inputs](https://learn.adafruit.com/adafruit-arduino-lesson-8-analog-inputs.md)
- [Memories of an Arduino](https://learn.adafruit.com/memories-of-an-arduino.md)
- [Adafruit Arduino Selection Guide](https://learn.adafruit.com/adafruit-arduino-selection-guide.md)
- [Arduino Tips, Tricks, and Techniques](https://learn.adafruit.com/arduino-tips-tricks-and-techniques.md)
- [Wireless Game Show Poppers for the Classroom!](https://learn.adafruit.com/wireless-game-show-poppers.md)
- [Arduino Lesson 15. DC Motor Reversing](https://learn.adafruit.com/adafruit-arduino-lesson-15-dc-motor-reversing.md)
- [Skill Badge Requirements: Microcontrollers](https://learn.adafruit.com/skill-badge-requirements-microcontrollers.md)
- [Ladyada's Learn Arduino - Lesson #0](https://learn.adafruit.com/ladyadas-learn-arduino-lesson-number-0.md)
- [Multi-tasking the Arduino - Part 3](https://learn.adafruit.com/multi-tasking-the-arduino-part-3.md)
- [Ladyada's Learn Arduino - Lesson #1](https://learn.adafruit.com/ladyadas-learn-arduino-lesson-number-1.md)
- [Experimenter's Guide for Metro](https://learn.adafruit.com/experimenters-guide-for-metro.md)
