File Exchange Pick of the Week

Our best user submissions

Control an Arduino from MATLAB

Doug's Pick this week is Arduino IO Package by Giampiero Campa.

You may remember my last Pick was about a new capability to download and run Simulink models on an Arduino, a low-cost electronics prototyping platform. Well I just got a new Arduino Mega ADK board and have been playing around with different ways to connect it with MATLAB and Simulink. So if you'll indulge me I'd like to do one more Pick about the Arduino.

Giampiero's submission allows you to easily control an Arduino directly from MATLAB or Simulink. You first download a fixed Arduino program to your board and then you can use either MATLAB code or Simulink blocks to access and control the I/O on your board. For example, the following MATLAB code connects to a board, reads the voltage on one pin and uses it to control the output on another:

% connect to the board

% set pin 9 to output

% read analog input from analog pin 5

% normalize av from 0:1023 to 0:255

% ouptput value on digital (pwm) pin 9 

% close session

If you connect a potentiometer to analog pin 5 and an LED to digital pin 9, you could use this code to set the brightness of the LED by turning the potentiometer!

Giampiero's interface lets you read and write to the digital and analog pins, and also control DC, Servo, and Stepper motors if you have the Motor Shield. One thing I really like about this package is that it uses object-oriented programming, which makes it very simple to see the available methods by hitting the tab key.

You can accomplish the same thing using the Simulink block library, if you prefer that approach:

The big difference between this package and the Run on Target Hardware capability is that the actual algorithm is running on your computer so you will always need to be connected to the Arduino.

Note that the actual Arduino IO Package is available here on our academic website, the File Exchange submission now just contains some additional demos.


Let us know what you think here or leave a comment for Giampiero.

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.