Setting Arduino Development for MATLAB - Arduino Programming using MATLAB, 1st Edition (2015)

Arduino Programming using MATLAB, 1st Edition (2015)

2. Setting Arduino Development for MATLAB

This chapter explains how to work on setting up Arduino board on a computer and then, access it from MATLAB.

2.1 Getting Started

In this chapter, we set up Arduino board development using MATLAB support package for Arduino hardware. To set up this development, you must have MATLAB 2014a or later and MATLAB account to verify while installing.

2.2 Setting up Arduino Development for MATLAB

In this section, we try to set up Arduino development for MATLAB. You can configure MATLAB Support Package for Arduino hardware using MATLAB 2014a or later. We also need internet connection to download this package.

Let's start.

Run MATLAB application. Click Get Hardware Support Packages on Add-Ons icon on toolbox.

a2-1

Then, you get a dialog. Select Install from Internet.

If done, click Next> button.

a2-2

Select Arduino on Support for. You should see two Arduino support packages. You can select both.

If done, click Next> button.

a2-3

You will be asked to logon with your MATLAB account. You should have MATLAB license. Click Log In button.

a2-4

You should the authentication dialog. Fill your account. After that, click Log In button.

a2-5

If success, you should get a software license agreement. Checked I accept and then click Next> button.

a2-6

You will get confirmation. Click Next> button.

a2-7

Click Install button to start installation.

a2-8

After done, you will be asked to configure Arduino board. Select Arduino and then, click Next> button.

a2-9

Confirmation form will be shown. Click Continue> button.

a2-10

The program will check if your platform needs Arduino driver or not. If you're working on Linux and Mac, you don't need a driver. You need to install Arduino driver if you're working on Windows platform. Click Next> button if done.

a2-11

Installation is over. Click Finish button to close installation.

a2-12

2.3 Connecting Arduino Board to Computer

Now you can connect Arduino board to computer. Then, verify which serial port is used for Arduino board. On Mac platform, you type this command.

$ ls /dev/cu*

On Linux platform, you type this command.

$ ls /dev/tty*

You can use Device Manager on Windows platform.

After that, you should see serial port of Arduino board which is attached on the computer.

My OSX detected my Arduino board used /dev/cu.usbmodem1421 serial port.

a2-13

On MATLAB command Windows, type this command

>> a = arduino

MATLAB will detect your Arduino board. You should detected Arduino board information on Maltab Command Window.

a2-14

2.4 Hello Arduino: Blinking LED

In this section, we build a blinking LED program using MATLAB. Arduino Uno/Mega/Leonardo boards provides onboard LED which is connected on pin 13.

Let's start to write our Blink program.

Firstly, you set working folder on MATLAB. You can change it on MATLAB IDE, see a red arrow.

a2-15

Then, click New Script icon to create a new script file.

a2-19

After that, you can get a script editor, shown in Figure below.

a2-17

board = arduino();

led = 'D13';

for k=1:10

disp('turn on LED');

writeDigitalPin(board,led,1);

pause(1);

disp('turn off LED');

writeDigitalPin(board,led,0);

pause(1);

end

disp('close Arduino board');

clear board;

Save those scripts into a file, called blinking.m. Now you can run it.

>> blinking

You may get error message, shown in Figure below.

a2-16

This error occurs because we create arduino object while there is existing arduino object. We can't use multiple arduino object. Delete existing arduino on Workspace Window. See a red arrow on above Figure.

We can clear our arduino object usage using clear syntax.

Now you can run the program again. The following is a sample output of blinking program.

a2-20

You should see blinking LED on Arduino board.

a2-18