Topic 17: Arduino control with Matlab
Arduino and Matlab (download Arduino sketchs and Matlab example code from the MathWorks web site)
The above link will take you to the Mathworks website where you can download the Arduino sketches ('.ino' files) and Matlab examples ('.m' files) of how to get Matlab to interact with Arduino using the code provided there. This example code was all written by Matlab and serves as the original 'MATLAB Support for Arduino (aka ArduinoIO Package)'. I say 'original' because newer versions of Matlab have significantly changed and improved how Matlab interacts with Arduino. Although the changes represent an improvement in terms of simplicity of use and efficiency of communications with Arduino, the newer Matlab support for Arduino is more complex to understand and requires understanding of Matlab Object Oriented programming.
I have created a single 'simple' Matlab '.m' file that contains all the Matlab functions for communicating with Arduino and an example Matlab '.m. file and a Matlab '.fig' file for a Graphic User Interface (GUI) Matlab program to run a servo motor. Those three files are located in a single zipped file located here (click to download file - note the file has an extension of '.txt'. Once it is downloaded, rename the extension to '.zip' and then extract it). Once extracted, it contains three individual files:
The file 'arduino_functions_simple.m' is a single Matlab '.m' file with many different functions. This '.m' file will not run in Matlab - i.e., it does not do anything on it's own. It is simply intended to be a 'resource file' or 'holder file' for all the different Matlab functions for allowing Matlab to interact with Arduino. I have named it 'arduino_simple_functions.m' because I have cut out and removed a lot of the code from the original Matlab-Arduino download at the top of this page to make these functions easier to understand and easier to work with. The code I have removed from the original is largely error checking code. The error checking code is useful to have to make sure Matlab programs are robust and don't crash due to incorrect input parameters, however, having all that extra code in the files makes it far more complex to understand how the Matlab code actually works.
The other two files are:
Create a folder under your Matlab folder and save these two files into that folder on your computer. Connect up an Arduino Uno R3 via the USB port on your computer. After connecting Arduino to your computer, you will need to check the BAUD rate and the COM port number that have been assigned to Arduino. These setting can be checked in the Device Manager on your computer. To get to the Device Manager, go to Start>Control Panel>Device Manager on your computer.
Then click on the 'Ports (COM & LPT)' drop down menu. You should see your connected Arduino there. The two settings (COM port number and BAUD rate) need to correspond to the BAUD rate and the COM port number used in the Matlab code. If they are not the same, the Arduino board will not be recognized by Matlab. If the COM port number is not 'COM3' then you will have to change the COM port number in the Matlab code to match the COM port number assigned to Arduino on your computer. If the BAUD rate is not set to 115200 on your computer, then you will need to change the BAUD rate to this value in the Device Manager.
Once you have checked the settings, you will then need to upload an appropriate Arduino sketch to Arduino using the Arduino Integrated Development Environment (IDE) which is downloaded from the Arduino web site. Since the Matlab code I have provided is a Matlab example that runs a servo motor, upload the 'motor_v1.ino' or the 'motor_v2.ino' sketch to Arduino. Both of these sketches allow for servo motor control on Arduino. Once the sketch is uploaded to Arduino, connect a servo motor to the Arduino as instructed on the Adafruit web site tutorial and run the '.m' file in Matlab. A Matlab GUI will open (the '.fig' file).
Click on the 'Connect Arduino' button and if the COM potr number and the BAUD rate settings mentioned above are correct, then Arduino will be connected. Select the correct Channel that the servo motor is connected to on Arduino and then try moving the servo motor.