Thermocouple.m - MAE221/Thermodynamics-Lab GitHub Wiki
clear all
close all
clc
% Enter your photon name
name = 'NAME';
% Enter the unique access token for your Photon
atoken = 'TOKEN';
% Enter the port your Photon is connected to. If you have trouble with
% this, leave it blank and the Photon will connect via the Cloud (though
% much slower than the port)
port = 'Port';
pin = 'A1';
g = Photon(name, atoken, port);
% 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;
if isUsingSerial
g.disconnect;
end
%% OUR CODE
numTestTemps=3; %Number of tested temperatures
numTests=10; %Number of tests per temperature
tempVolts=zeros(numTests,numTestTemps); %initializing the matrix
for I=1:numTestTemps
for i=1:numTests
disp('Taking voltage measurements');
tempVolts(i,I)=g.analogRead(pin);
%Output will be formatted as a matrix. You can look at the values
%in the Matlab Workspace. Each column is for a different
%temperature case and each row represents a different measurement
end
disp('Code is paused. Press enter when you are ready to continue. You should wait a few minutes in between temperature cases.');
pause;
end
%%
resistor=100;
gain=(1+100e3/resistor);
tempVoltsAvg=mean(tempVolts,1); %Taking the average of all the rows in the same column
tempVoltsCorr=tempVoltsAvg/gain; %Correcting the raw voltage for the gain of the system
dialTemps=([0,0,0]-32)*(5/9); %fahrenheit to celsius
%Useful place to store dial temp information
tempPhoton=zeros(1,numTestTemps);%Initializing matrix
for I=1:numTestTemps
tempPhoton(I)=volt2temp(tempVoltsCorr(I)*1000); %the input to this function is in mV
%Converting gain-compensated raw voltage data into temperature
end
%% YOUR CODE HERE
% Write some codes for creating the plots needed for Exercise 2 of the worksheet.
% You will need to plot the calibration curve, the curves for the maximum and
% minimum range of its error, and a scatter plot of the table calibrated data.
% All of it must be on the same plot. Don't forget to include
% a legend, title, axis labels, and appropriate units.
%% FUNCTION CODE
% For this function, give it an input voltage ranging from 0-9.288mV and it
% will output the temperature corresponding to that voltage in degrees
% Celsius.
function temperature = volt2temp(voltage)
% Error is between -0.02C and 0.015C for this equation.
% Equation and coefficients from:
% http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/temperature-measurement/thermocouple/type-t-calibration-table
if voltage < -0.5
disp("Voltage should not be negative")
return;
end
% coefficients for thermocouple equation
v = voltage;
T0 = 135;
v0 = 5.95886;
p1 = 20.325591;
p2 = 3.3013079;
p3 = 0.12638462;
p4 = -0.00082883695;
q1 = 0.17595577;
q2 = 0.0079740521;
q3 = 0;
temperature = T0 + ((v-v0)*(p1+(v-v0)*(p2+(v-v0)*(p3+p4*(v-v0)))))/(1+(v-v0)*(q1+(v-v0)*(q2+q3*(v-v0))));
return;
end