MATLAB Examples

Characterize an LED using live current and voltage measurements

This example uses MATLAB® to connect to an ADALM1000 source-measurement unit, configure it, and make current and voltage measurements to characterize an LED.



Using functionality in toolboxes such as Data Acquisition Toolbox™ and Instrument Control Toolbox™, MATLAB can connect to, configure, and control hardware to setup experiments and retrieve data. You can use the measured data for signal processing, visualization, and algorithm design using the rich set of functions in MATLAB and its toolboxes.

Discover supported data acquisition devices connected to your system

ans = 

Data acquisition devices:

index   Vendor    Device ID                      Description                      
----- ----------- --------- ------------------------------------------------------
1     directsound Audio0    DirectSound Primary Sound Capture Driver
2     directsound Audio1    DirectSound Microphone (Realtek High Definition Audio)
3     directsound Audio2    DirectSound HP 4120 Microphone (HP 4120)
4     directsound Audio3    DirectSound Primary Sound Driver
5     directsound Audio4    DirectSound Speaker/HP (Realtek High Definition Audio)
6     directsound Audio5    DirectSound HP 4120 (HP 4120)
7     adi         SMU1      Analog Devices Inc. ADALM1000

Create a session using the ADALM1000 device.

ADISession = daq.createSession('adi')
ADISession = 

Data acquisition session using Analog Devices Inc. hardware:
   Will run for 1 second (100000 scans) at 100000 scans/second.
   No channels have been added.

Add voltage source and current measurement channels

The ADALM1000 device is capable of sourcing voltage and measuring current drawn on a specified channel. Set up the device in this mode.

% Add an analog output channel with device ID SMU1 and channel ID A,
% and set its measurement type to Voltage.

% Add an analog input channel with device ID SMU1 and channel ID A,
% and set its measurement type to Current.

% Confirm the configuration of the session
ADISession = 

Data acquisition session using Analog Devices Inc. hardware:
   No data queued.  Will run at 100000 scans/second.
   Number of channels: 2
      index Type Device Channel   MeasurementType        Range       Name
      ----- ---- ------ ------- ------------------- ---------------- ----
      1     ao   SMU1   A       Voltage (SingleEnd) 0 to +5.0 Volts
      2     ai   SMU1   A       Current             -0.20 to +0.20 A

Blink attached LED five times

Connect an LED between channel A and ground on the ADALM1000. Remember to connect the LED in series with a 330 Ohm resistor to limit the maximum current through the LED.

for iLoop = 1:5
    % Turn on LED by sending an output of 5V
    % Turn on LED by sending an output of 0V

Characterize the LED

To understand the LED's I-V characteristics, sweep a range of voltage values from 0V to 5V and measure the current. The aggregate of all measurements provides data to graph the current across the LED at the specific output voltage.

v = linspace(0,5,250);

for iLoop=1:length(v)
    i(iLoop) = inputSingleScan(ADISession);

Plot the I-V curve of the LED and estimate a mathematical model for the LED

When you have the measured data, you can visualize it. You can also calculate a mathematical model that approximates the behavior of the LED within the range of the measurements.

% Plot the measured data
plot(v, i, 'LineWidth', 2);
hold on;
grid on;
ylabel('I (Amperes)');
xlabel('V (Volts)');
title({'I-V characteristic curve of LED';'and fifth-order polynomial approximation'});

% Fit the data using a fifth-order polynomial and overlay the acquired data
% with the model of the LED approximated by a fifth order polynomial
approxPoly = polyfit(v, i, 5);

% Plot the approximated data
plot(v, polyval(approxPoly, v), '-k', 'Linewidth', 1);

Calculate the voltage at which the LED turns on

Based on the fifth order polynomial approximation, we can find a first order approximation that represents the linearly increasing portion of the curve. The voltage at which the LED turns on is approximately where this line intersects the voltage axis.

% Find the line that passes through the linear portion of the signal
normErr = -1;
errThreshold = 0.001;
numPointsForLine = numel(v) - 10;
while (numPointsForLine > 0) && (normErr < errThreshold)
    approximation = polyval(approxPoly,v(numPointsForLine:end));
    [linearPoly, errorStruct] = polyfit(v(numPointsForLine:end), approximation, 1);
    numPointsForLine = numPointsForLine - 5;
    normErr = errorStruct.normr;

% Evaluate the linear polynomial over the range of measurements. The value
% where this intersects the horizontal line representing any leakage
% current is the voltage at which the LED turns on
LEDThreshold = 1.2;
leakageCurrent = mean(i(v<LEDThreshold));
linearIV = polyval(linearPoly,v);
minIndex = sum(linearIV<leakageCurrent);

% Plot the linear portion of the curve

% Circle the approximate voltage at which the LED turns on

title(sprintf('Calculated voltage at which LED turns on: %1.2fV',v(minIndex)));

Turn the LED off and clear the session

clear ADISession

Additional information

This example shows how to use MATLAB to configure and control data acquisition hardware to setup experiments, measure live data, and visualize the data retrieved from the hardware.

For more information on accessing your hardware from MATLAB, refer to the Hardware Support Catalog.