Matlab Arduino to Control an Led using Serial communication

(Last Updated On: September 8, 2021)

 

Controlling led using Arduino and MATLAB GUI:

 

Matlab Arduino to Control an Led using Serial communication- As a beginner, the very first project that almost everyone starts with is to control an LED using Arduino, because it’s simple to understand how to turn ON and turn OFF any IO pin. In this tutorial, we will do the same thing that is we will control an LED but this time we will use the GUI application designed in MATLAB. GUI stands for Graphical User Interface. Unlike other programming languages like “VB.net, Visual C++, C#, Python, etc” in Matlab we can also design GUIs. Another best thing is you don’t need the Arduino code, the Matlab GUI will do everything. You gonna love this, you don’t even need to write a single line of code in Arduino. All you need is to connect the LED with the Arduino and then connect the Arduino with the Computer or Laptop and you are good to go.

MATLAB ARDUINO

But, initially, you will need to install the Matlab support package for Arduino hardware, because without it you won’t be able to control the Arduino board. You can also read my article on how to install the Matlab software and how to start using it.

The reason I am starting with the LED is that an LED can be easily arranged. If you don’t have the LED then you can use the Arduino’s onboard LED connected with the digital pin 13. Anyways, in this tutorial, we will connect the LED with pin 3 of the Arduino. If you are able to control an LED then you can control a Transistor, Mosfet, or a relay, etc, and this way you can control higher loads. For easy understanding I will add only two button, one button will be used to turn ON the LED and the other button will be used to turn OFF the LED. Without any further delay let’s get started!!!



Amazon Links:

Arduino Uno

LEDs:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

LED interfacing with Arduino:

MATLAB ARDUINO

The Anode side of the LED is connected with pin 3 of the Arduino through a 220-ohm current limiting resistor. You can also use a 330-ohm resistor. Read more about the current limiting resistor. The cathode side of the LED is connected with the ground. That’s all about the connection. Serioulsy this is the simplest cirucit that we can start with.




Matlab GUI Designing for Arduino:

Once your hardware is setup. Then Open the Matlab Software and to make the GUI application simply type guide in the command window and press enter this will open the untitled.fig. This is your form where you can add buttons, labels, check boxes etc.

MATLAB ARDUINO

GUI window will appear, now we will create two buttons which will be used to turn ON and turn OFF the LED. So we will click the push button and drag to the size.

MATLAB ARDUINO

Now click on the button and a new window will open, which will show the properties of the button, in which we can change the color of the button and give a title to the button. As this button will be used to turn ON the LED, we will give it title ON and select the color green for this button and in the Tag section we will write on.

MATLAB ARDUINO



Similarly, we will insert another push button which we gonna be using for turning OFF the LED. We can insert another push button or just copy the ON button and paste it. Now we will change the color of the button to red and change the title to OFF and also change the Tag to off.

MATLAB ARDUINO

Now, to add a label simply click on the label “static text” and click on the GUI, then go ahead resize the label, write the text you want to display. In my case I will start with the “LED CONTROL” text.  You can add multiple labels as per your requirement.

MATLAB ARDUINO

Now we will save the project, go to the file menu > click on the save and select the location where you want to save the file.

MATLAB ARDUINO



Now give the name ledcontrol to the project and click on the save button.

MATLAB ARDUINO

After saving the file the code will open

MATLAB ARDUINO

Now we have to declare all the functions that we are going to use so before that we need to declare some variables that we are going to use.

A function called clear all and global “a” so this is the variable I am going to use for my Arduino.

Now we will declare the function, when you press the push button “ON” what tasks to do and type that global a and when you press the ON button the LED has to turn ON. for that we need to write a function called writeDigitalPin my digital pin in the “a” comma that is the this is available to be used for Arduino. So that is a reason I am using a comma the pin that I connected my LED is D3 comma and a value is 1.


Now when we will click on the turn off button the led will turn off now for the off button we will write the function

Now the code is completed and we will check our program first we will save the code and then click on the run button. When we will click on the run button the led will turn ON and when we click on the turn off the led will turn OFF.

MATLAB ARDUINO

Copy the following code, or you can download the ledcontrol.fig and ledcontrol.m files.

Download Matlabe Gui and Code



Arduino Matlab GUI Complete code:

Final Testing:

Now all you need is to connect the Arduino with the Laptop or Computer and run the GUI by clicking on the play button. The Matlab will automatically detect the comport and as I said earlier you don’t need the Arduino code. Anyway, when you have run your Matlab GUI, you should be able to control the LED using the two push buttons ON and OFF. Watch the demonstration video given below. In the video I am controlling the onboard led connected with pin13 of the Arduino.


Demonstration:

 

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

%d bloggers like this: