Control of LED brightness using Arduino Matlab | applied electronics engineering

# Control of LED brightness using Arduino Matlab

By Applied Electronics - Tuesday, January 31, 2017 No Comments
Here we show how to control a LED brightness connected to Arduino using Matlab. We will use potentiometer which will be connected to the analog pin 2 or A2. Changing the potentiometer will send different voltage to the analog pin. The internal ADC of the micrcontroller will convert the analog to digital values. This digital values will be send to PWM digital pin 5 or D5. To this pin is connected a LED grounded via resistor of 220Ohm. Thus by changing the potentiometer value we will be changing the voltage that is applied to the LED. Hence in effect the LED brightness will change. In this case a 10KOhm POT was used. At the end there is a video showing all that is done in this tutorial.

The schematic is shown below.

As you can see the Potentiometer of 10KOhm is connected to A2 analog pin and the LED and resistor is connected to the D5 or the digital pin 5 which is a PWM pin.

Once the circuit is connected, we write the matlab code to read in the analog signal from A2 and send the same to the digital PWM pin D5.

Following is the matlab code to do this.

% control of LED brightness using potentiometer

clear a

a = arduino();

t = 200;

while t > 0
writePWMVoltage(a, 'D5', v)

t = t-1;
pause(0.1);
end


First the clear a statement was used to clear the a object created down the program code. This was done to ensure that the object a is deleted before we run the program code next time. Otherwise you may get error that the com port is not available or that another object is using the com port.

a = arduino() creates an Arduino object. This is mandatory.

t =200 is the time or the period to run the while loop down below.

The while loop starts, where t is compared to value 0. If t is greater than 0 then the while code executes.

The command lin v = readVoltage(a, 'A2') reads the analog signal from the A2 analog pin and stores in variable v which stands for voltage. Then the next line writePWMVoltage(a, 'D5',v) sends the voltage v to the D5 pin. In both the functions we have to specify which arduino object is being used which in this case is a.

t = t-1 decrements the value of t which is initially 200. In the next loop t is 199 and is compared to 0 and since 199 is greater than 0, the while loop statements are executed. The t is again decremented to 198 and again compared to 0. This goes on until the value of t is 1 in which case then next value t-1 is 0 and in this case 0 is not greater than 0 therefore the while stops and the program is terminated.

The pause(0.1) creates a delay of 0.1 second between the sending of the PWM signal.

#### Video demonstration of Control of LED brightness using Arduino Matlab

The following video shows the LED brightness changes as the potentiometer is changed.

So in this way you can control the LED brightness using Arduino and Matlab. Other tutorials are here in this blog which teaches the very basic of ardiuno matlab programming, the basic of PWM using arduino and matlab and using the writePWMVoltage() function basics which has been used in this tutorial.