
In this article, we will focus on two aspects:
1. How to set up hardware support for Arduino in MATLAB software.
2. How to control the Arduino development board using MATLAB code.
We usually use Arduino IDE to write the code and then upload it to the Arduino development board. The advantage of using MATLAB is that it uses a high-level programming language that is easier than C/C++. Another advantage of using MATLAB is that we can quickly see the results of I/O operations (no need to compile). In addition, MATLAB provides plotting functions that can be used to quickly analyze and visualize the data collected from the Arduino. First, we will learn how to set up the hardware support package for Arduino in MATLAB software. After setting up the hardware support package for Arduino in MATLAB software, we will use MATLAB code to control the LED connected to the Arduino development board.
Setting up hardware support for Arduino
Step 1. Start MATLAB (please use the latest version).

Step 2. In the Environment section, select Add-Ons > Get Hardware Support Packages .

Step 3. The Add-On browsing window appears.

Step 4. Click MATLAB Support Package for Arduino Hardware .
Step 5. Click Install . Now the installer will ask you to log in to your MathWorks Account. If you do not have a MathWorks Account, you can create one during the installation.
Step 6. After logging in, accept the license agreement and continue with the installation.
Step 7. Wait for the software package to download and install.
Step 8. Now you have successfully installed the Arduino support package for MATLAB.
Testing MATLAB
After installing the support package for MATLAB, we need to check whether it is installed correctly.
1. Open MATLAB.
2. Connect Arduino to PC.
3. Type the following command in the MATLAB command window.
4. If we have multiple Arduino connected to the PC, then we can specify the board model and COM port it is connected to using the following command.
- a = arduino( ‘COM5’ , ‘uno’ )
Copy code
5. After entering the above command, MATLAB will try to communicate with your Arduino, if successful, MATLAB will display the properties of the Arduino board connected to your PC.

6. Now we can see the variable ‘a’ in the workspace which is the MATLAB arduino object. To clear the object we can use the following command.
This command removes the Arduino object from the workspace.
Controlling LEDs using MATLAB and Arduino
In this tutorial, we will blink an LED connected to Arduino using MATLAB software.
Required Components
● Arduino development board
● Resistance
● Light Emitting Diode
● USB cable for Arduino
Circuit Schematic

How to use
1. Start MATLAB.
2. Connect your Arduino to your PC.
3. Make the circuit as shown in the schematic.
4. Open your .m code.
5. Save and run.
6. The LED starts blinking.
7. After blinking 5 times, the LED turns off.

The code is very simple, the complete code is listed at the end of this article, copy it and save in a file with .m extension. You can go through the code and customize it according to your requirements.
Code
The complete code used in this article is as follows:
- % create an arduino object
- a = arduino();
- % start the loop to blink led for 5 seconds
- for i=1:5
- writeDigitalPin(a, ‘D10’, 1);
- pause(0.5);
- writeDigitalPin(a, ‘D10’, 0);
- pause(0.5);
- end
- % end communication with arduino
- clear a
Copy code
|