Another way to code your Arduino is to use MATLAB. MATLAB Support Package for Arduino Hardware allows you to use MATLAB to control multiple Arduino board. You can read sensor data through Arduino and see the result in MATLAB. You can control motor via MATLAB command. Unlike Arduino IDE, you don’t have to compile or upload your code to your Arduino in MATLAB. And with the help of MATLAB library, you have access to thousands of powerful library to analyze your data.
To use MATLAB Support Package for Arduino Hardware, you have to install the package. If you don’t have MATLAB on your PC, download it from MATLAB official website. To activate your MATLAB, apply for free MATLAB Student License through PSU software store.
Install MATLAB package
Open your MATLAB and then open the Add-ons.
In search bar, type “MATLAB Support Package for Arduino Hardware”. The first one is the package we want to install. Click inside and install it.
Control Arduino with MATLAB
Download the blinking MATLAB example code, open this code in MATLAB. Connect your Arduino board to your computer. Click the RUN button.
The LED on Arduino board will turn on/off 10 times. Notice that when LED switches state, the TX and RX on Arduino board flash. TX and RX are the indicator of serial communication. This is because we are using serial communication to control Arduino instead of uploading code to Arduino as Arduino IDE. Therefore, when you use MATLAB to control Arduino, the data cable has to be connected the computer all the time.
Let’s take a look at code:
- Note we add “close all; clear all; clc;” to reinitialize the MATLAB. Unlike other coding language like C/C++, MATALB store the previous code running result in the system memory. To remove the interruption from last coding result, Make sure always add “close all; clear all; clc;” in all of your MATLAB code.
- Unlike Arduino IDE, MATLAB don’t have a initialization or infinite loop in the code structure. Your code will only run one turn.
- Same as the example code in Arduino IDE section, we see that the code wants to change a pin number 13 to an output (Arduino pins default to inputs). This lets us command whether the line will be “on” (5 volts) or “off” (0 volts).
- Another important function is the command “pause”. It serves same function as “delay” in Arduino IDE. This tells the Arduino to keep current voltage at all pins for a specified number of second (Not milliseconds as “delay“)
Need More Help?
Go through Get Started with MATLAB Support Package for Arduino Hardware for more example code. You can find more instruction about MATLAB on MATLAB Support Package for Arduino Hardware Home page.