Control an LED using MATLAB support package

Control an LED using MATLAB support package

LEDs are small, powerful lights that are used in many different applications. We will work on blinking an led.

Components required

  • Arduino UNO
  • Breadboard
  • Led
  • 1k ohm resistor
  • jumper wires

Introducing the LED

A diode is a one-way electric current valve, and a light-emitting diode (LED) emits light when current passes through it.  Since an LED is a one-way current valve, you have to make sure to connect it the right way for it to work as intended.

An LED has two terminals: the anode and the cathode. The anode lead is labeled with the plus-sign (+) in the part drawing, and it is the wide part of the triangle in the schematic symbol.  The cathode lead is the pin labeled with a minus-sign (-), and it is the line across the point of the triangle in the schematic symbol.

When you build an LED circuit, you will have to make sure the anode and cathode leads are connected to the circuit properly. You can tell them apart by the shape of the LED’s plastic case. Look closely at the case—it’s mostly round, but there is a small flat spot right near one of the leads, and that tells you it’s the cathode. Also note that the LED’s leads are different lengths. Usually, the shorter lead is connected to the cathode.

Hardware setup

Connect an led to digital pin 11 on the arduino hardware through a 1k ohm resistor.

Create an Arduino object

a  = arduino();

If you have more than one Arduino board connected, specify the port and board type.

a = arduino('com20', 'uno');

Turn LED on and off

Write value 1 or true to digital pin 11 turns on the LED and write a value of 0 or false turns it off. Execute the following command at the MATLAB prompt to turn the LED off and on.

 writeDigitalPin(a, 'D11', 0);
 writeDigitalPin(a, 'D11', 1);

Configure the LED to blink at a period of 1 second.

for i = 1:10
      writeDigitalPin(a, 'D11', 0);
      writeDigitalPin(a, 'D11', 1);

Clean up

Once the connection is no longer needed, clear the arduino object.

 clear a

We hope this tutorial helped you in learning how to Control an LEd using MATLAB support package. You may also want to take a look at our tutorial on Control Servo Motor using MATLAB.

If you liked this article, then please subscribe to our YouTube Channel for MATLAB & Simulink’s Video Tutorials. You can also find us on Twitter and Facebook .

Senior undergraduate student from Shri Mata Vaishno Devi Unversity, Katra.


  1. […] MATLAB Support Packages for Arduino Hardware lets you use MATLAB to communicate with your board through USB cable. The support packages […]

Leave a Reply