Sunday, September 13, 2015

ARTICLE #5 - Control an Arduino from MATLAB

Introduction :



MATLAB is a technical computing software and also a high level programming language widely used by engineers and scientists in many fields to perform mathematical computation, algorithm development, simulation, analysis and data visualization and also GUI application development.

In this tutorial we will learn how to interface Matlab with the Arduino board.it simply means that you will use Matlab software to communicate with your Arduino board without using the Arduino IDE. So you will directly write the commands on the Matlab command window, send them to the Arduino board via the USB cable wich will then execute these commands and send back the results.


We will use Matlab built-in specific Arduino commands to write a simple blinking LED program, then we will write another program that reads the surrounding temperature using a TMP36 sensor (you can click here to see my previousarticle about the TMP36 temperature sensor) and also we will build a simple GUI application that plots this temperature data in a 2D graph.



Project parts :

Software : 

  • MATLAB
  • MATLAB support package for Arduino

Hardware



  • Arduino Uno
  • 1 Breadboard
  • 1 Red LED
  • 1 TMP36 temperature sensor
  • Wire connectors


Interfacing steps :

To install the MATLAB and Simulink hardware support packages for Arduino in the 2014a version and later releases, you click on “Add-ons” in “Home” tab, choose “Get hardware support packages”. Then from the “Support Package installer” window you click on “Install from internet” and you click on “Next”, after that you search for “Arduino” in the scroll bar section and you click on it, check all the three install boxes of the packages and you click “Next”. This launches the support packages installation, you follow  the installation steps until it finishes.

After the support packages were downloaded, we will test if all of this works fine. Connect the Arduino board to your PC. Under the MATLAB command window, you create an Arduino object by typing the following command line :


a = arduino(‘comX’,‘uno’)

(Where X should be replaced by the correspondent COM port of your Arduino)

You should see the following text once you click “ENTER” button :


Attempting connection .............

Basic I/O Script detected !

Arduino successfully connected !


This object can now perform digital and analog I/O functions and utilize all the functions specified by the arduino.m library.  


Circuit wiring 1 : Blinking LED




Matlab code:





Circuit wiring 2 : plotting temperature with TMP36 sensor






CODE :






Temperature Plot :



No comments:

Post a Comment