LED_Voltage_vs_Current_Curve.m - MAE221/Thermodynamics-Lab GitHub Wiki
%Enter your Photon name%
name = 'PHOTON_NAME';
%Enter the unique access token for your Photon%
atoken = 'ACCESS_TOKEN';
%Enter the serial port for your Photon. If you have trouble finding the port or connecting to the serial, leave this blank.
port = 'PORT';
% Enter the input and output ports for your device. NOTE: A3 and A6 are the only analog ports that can output a DAC analog signal.
% A4 and A5 output a PWM analog signal. Ask Mike why this matters.
outputPin = 'ANALOG_OUTPUT';
inputPin = 'ANALOG_INPUT';
% Enter true if you are using a serial connection and false if you are using a cloud connection. If you having issues, just put false.
isUsingSerial = true;
% Some initial conditions. Feel free the change them around
startVoltage = 0;
endVoltage = 3.3;
incrementVoltage = 0.2;
resistance = 470;
%Note: If you have difficulty accessing your serial port, do not include it
% (ie do not put anything inside the variable port string))
g = Photon(name, atoken, port);
if isUsingSerial
g.disconnect;
end
%%
% helpful var set
for i = 0:7
msg = sprintf("D%0.0f = 'D%0.0f';", i,i);
eval(msg);
end
for i = 0:7
msg = sprintf("A%0.0f = 'A%0.0f';", i,i);
eval(msg);
end
%%
disp("Beginning data acquisition. This will take several minutes...");
% Sets up for loop to collect all the data
k=1;
arrayLength = ceil((endVoltage-startVoltage)/incrementVoltage);
voltagesOut = zeros(arrayLength, 1);
voltagesIn = zeros(arrayLength, 5);
% Loops through, increments the voltage out and reads in the voltage in
for i=startVoltage:incrementVoltage:endVoltage
g.analogWrite(outputPin, i);
voltagesOut(k) = i;
for j=1:5
voltagesIn(k,j) = g.analogRead(inputPin);
end
k=k+1;
end
% Ensures that you have an endVoltage datapoint (3.3)
if voltagesOut(end) <= endVoltage
g.analogWrite(outputPin, endVoltage);
voltagesOut(k) = endVoltage;
for j=1:5
voltagesIn(k,j) = g.analogRead(inputPin);
end
end
%%
disp("Data acquisition complete. Creating figures...");
% Creates figures
averageVoltagesIn = mean(voltagesIn, 2);
current = averageVoltagesIn / resistance * 1000;
figure;
plot(voltagesOut, current)
xlabel("Voltage Out (V)");
ylabel("Current at LED (mA)");
title("Voltage Out vs Current at LED");
figure;
plot(voltagesOut, averageVoltagesIn)
xlabel("Voltage Out (V)");
ylabel("Voltage at LED (V)");
title("Voltage Out vs Voltage at LED")
%%
clear g
instrreset