HydroCar_IVCurve.m - MAE221/Thermodynamics-Lab GitHub Wiki
clear all
close all
clc
% Enter your Photon name%
name = '';
% Enter the unique access token for your Photon%
atoken = '';
% Enter the serial port for your Photon. If you have trouble finding the port or connecting to the serial, leave this blank.
port = '';
% Enter the input ports for your device.
voltPin = 'A0';
% 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;
%%
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
%% Voltage Curve
numResistors=15;
numTests=5;
for i=1:numResistors
resist(i,1)=input('Enter resistance value for new circuit: ');
for I=1:numTests
voltIn(i,I)=g.analogRead(voltPin);
end
voltInMean(i,1)=mean(voltIn(i,:))
currentMean(i,1)=voltInMean(i)/resist(i)
end
%% IV Curve and Polyfit
circArray=cat(2,resist,voltInMean,currentMean);
[~,idx] = sort(circArray(:,1)); % sort just the first row
sortedCircArray = circArray(idx,:); % sort the whole matrix using the sort indices
voltSort=sortedCircArray(:,2);
currentSort=sortedCircArray(:,3);
f=fit(voltSort,currentSort,'exp2')
%Fit function: f=a*exp(b*x)+c*exp(d*x)
coeffVal_f=coeffvalues(f)
save('IVcurveParams.mat','sortedCircArray'); %Saving the R, I, V values from test
save('IVcurve_fitCoeff.mat','coeffVal_f'); %Saving the coefficients of the fitted function
figure;
hold on
plot(f,voltSort,currentSort)
hold off
ylabel('Current(A)');
xlabel('Voltage (V)');