MATLAB and Arduino

Introduction of both the Environments
MATLAB
MATLAB is a abbreviation of Matrix Laboratory. MATLAB is a multipurpose tools for a range of applications, including deep learning and machine learning, signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology.
Arduino
Now coming to Arduino, Arduino is a great beginner friendly prototyping environment. It publishes both hardware and software for the microcontrollers. Few of its flagship product comprises of the Arduino UNO and many more.
Steps to connect MATLAB with Arduino
Steps to achieve the connection
STEP 1: Navigate to the Home page in the MATLAB start page.
STEP2: Click on Add-Ons and scroll down to Get Add on
STEP3: On search on title bar search for MATLAB Support Package for Arduino
STEP4: Accept all the necessary packages and download and Install all. The process will take around 15 minutes to complete and after that we are good to go.
INITIAL SETUP PROCEDURE
After the installation of the MATLAB Support Package for Arduino. We have to connect the Arduino Development Board with the PC and note the port to which the Arduino is connected to for checking the port it is active to follow the steps given below
STEP1: Click on Start and search for Device Manager.
STEP2: There will be a ports tab as soon as you click there will be the name of the device with its port number.

The COM4 port is the given port in the my case so I will be using COM4 port in my MATLAB code. The com ports may vary so keep that in mind.
Code Setup
Setting up the Arduino Variable if there is only one Arduino device connected then we can start by typing the given command
>> % If only 1 device is connected then we can directly use without specifying the port number and type
>>
>> a = arduino()
>>
>> % If there are many devices we have to specify the com port also like this
>>
>> a = arduino('com4','uno')
>>
>> % Here the first argument is the port number and the second is the name of the board
If it does not throws up any error and the variable stored in the variable window then we can assume that we have completed the setup and we are good to go.
Mini project 1 (Blinking LED)
First create a file named blink.m and open the blink.m file in MATLAB editor and paste the below code and run blink.m file.
a = arduino('com4', 'uno');for i=1:10000writeDigitalPin(a, 'D13', 1);pause(0.5);writeDigitalPin(a, 'D13', 0);pause(0.5);endclear a
Circuit Connection

Now I will be explaining the code firstly creating the Arduino object as said in the previous setup
“writeDigitalPin(a, ‘pin number’, 0 or 1) - This function takes in 3 parameters firstly the Arduino class variable followed by pin number for example D5, D13 etc. , then the state of the pin which is either Digital High(1) or Digital Low(0)”
STEP1 : Creating a Arduino class
STEP2: Defining a for loop to loop the blinking function in my case I used 1000 times to loop so the blinking will happen 1000 times.
STEP3 : The LED needs to switch on so we need to use the command writeDigitalPin(a, ‘D13’, 1) by this the pin 13 is put to Digital High then pausing the loop to see a visible change so pause(0.5)
STEP4 : The LED needs to switch off to complete the blinking cycle so we need to use the command writeDigitalPin(a, ‘D13’, 0) by this the pin 13 is put to Digital Low then pausing the loop to see a visible change so pause(0.5)
STEP5 : The loops run for 1000 times and the led blinking stops.
Mini project 2 (Temperature Data Graph using LDR sensor)
First create a file named ldr_sensor.m and open the ldr_sensor.m file in MATLAB editor and paste the below code and run ldr_sensor.m file.
a = arduino('com4','uno');
array = zeros(1,1000); %initializing the arrayfor i=1:1000: read_value = readVoltage(a,"A0"); array(i) = read_value;
endx = 1:1000;plot(array,x);
xlabel("Time");
ylabel("Value")
Here The readVoltage function acts as analogRead function of Arduino it reads value from 1 to 1023 in arduino but here we will directly get the value of the voltage generated in the arduino and reads it as a double datatype.
Circuit Connection

Output Graph observed in the output

Conclusion
There are many more possibilities that are there in the both learning Arduino and MATLAB. In my opinion there is a huge scope of learning both these amazing platforms. These projects can also be done using Simulink which I will be telling about in the future blog posts.
Until then keep learning keep exploring………………