Hello, Dejan Nedelkovski here from HowToMechatronics.com. This will be a step by step video tutorial for controlling the Arduino board using MATLAB GUI or Graphic User Interface. Will make a MATLAB GUI with two buttons for turning ON and OFF the LED and an axis for plotting the analog input from the photocell. Here's
the circuit schematic of this example so we have one analog
input coming from the photocell and one digital output for the LED This is the basic principle of
communication between the Arduino and the MATLAB that we are going
to use for this example. In order to unable to
communication we will have to install the Arduino
Input/Output Package library if you haven't already done that I would
highly recommend watching my step by step
video tutorial for installing and enabling the communication between the Arduino board
and MATLAB. You can click here to watch that the tutorial first or if you
have already done that now we will start building the MATLAB
GUI We can create MATLAB Graphic User Interface by clicking here,
New Graphic User Interface. We'll choose blank GUI and here is the GUI editor. First we'll set an axis where we will show the analog inputs Okay. Now we will need two push buttons for
controlling the LED Here's one button for turning ON and another button for turning OFF the LED. We'll rename them later Okay, and a button for reading the analog inputs which will depend from this edit text Here in this edit text we will set how many samples we will read from the analog inputs. Okay, now we have to modified these buttons by double
clicking them Here's the pop-up, the properties for his
bottom First we will renamed this here, the string so this is what it appears on the bottom I'll name it Turn ON and also I will change
the name of this here, the Tag, which will show up later in the script when we will modifie the code of the script of this GUI I will name this one as well, Turn ON button Okay. Here we can see that now the button
says Turn ON and the other button will be Turn OFF. This one as well Turn OFF button This bottom I will rename it read read button. Okay. and I will name it this edit text as
well. I will name it edit text samples Okay. So now we have to save this. I will name it example one and here we can see that a script is automatically
generated and now we can modify this script in order to enable the Arduino board to communicate to this MATLAB GUI. First thing we need to do is to create an variable, a global variable
which will enable the serial communication of the Arduino board and as well define the pin number 8 for the LED as an output We will do that here in this opening function. We will do that like this We will write global a. So this a variable will use this function Arduino And we will set the comport you should check on what comport is your Arduino connected Mine is on number 4 so I will have it like this. and now we will define the pin mode for the pin number 8 and it will be output Okay, now, it is always good to make sure
that the port is always clear previously so that's why we
will use above these lines we will use this clear function so will make sure that the variable is clear as well as the port using this function. Okay. So this will make sure that the port is clean and the variable is clean before we start the new ones. OK, next is the we'll give, we'll enable the buttons to do something when we'll
press them So that will be turning ON and turning OFF the LED Here's the function for the button Turn ON. So here we will first restate the global variable and we will make a digital write So when this button is pressed will make
a digital write to pin number 8 and we'll set it on high state and that will turn on the LED. And here the callback for the Turn OFF bottom is
this function. We'll do it the same here So here we'll write a low state or zero for turning off the LED when this button
is pressed. Okey, next is the read button but
before we modify that function we have to modify here this function. That's the edit text function callback. So we need to handle the value that we will
enter into the edot text box and put it into a variable. We'll do that like this. We'll put it into a variable I will name it data1 and using this get function we will put what we will enter in the
text box as a String in this data1 variable. But now we have to convert it into a
double so that we can use it later. We do that like
this. We'll put it into a variable, I'll name it like this, xSamples. So String to Double and also we need to write one more line in
order this whole function to work. Okay so now let's modify this function for the read button. First I will again restate the variable a as well as another k variable that I
will use for the loop function. And I need a variable x Okay so first we'll make a for loop for reading the analog inputs. So this for loop we'll start at 1 and it will increase by 1and it will go up to the value that to enter in the text box and that will be this xSamples value which is double that we defined in the previous step. Okay so now in this very b we will read the analog input from pin number 0 and we'll store the reads into a array and now we will plot that array will be automatically attached to the axis that we created in the GUI editor So we will make just some tuning of the plot of the axis and we'll set a grid on for better visualization and here we will also set size of the axis We will go from zero value to the value that we will enter as xSamples and for the Y axis it will go from 0 to
500 this is the value that I was getting
from the photo-resistor. So they range from 0 to 500, that's why I set these numbers. And we will make a little delay here and we will end the for loop. Now one more
time let's see how this example works.