From 9984b3d376c49c98c6f8e716be95f6552b39bb17 Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Thu, 12 Oct 2023 17:38:11 +0100 Subject: [PATCH] add TwoSensorsAAR.ino --- Lab_1/TwoSensorsAAR.c | 8 +- Lab_1/TwoSensorsAAR.ino | 191 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+), 4 deletions(-) create mode 100644 Lab_1/TwoSensorsAAR.ino diff --git a/Lab_1/TwoSensorsAAR.c b/Lab_1/TwoSensorsAAR.c index 89cd289..0c35bdc 100644 --- a/Lab_1/TwoSensorsAAR.c +++ b/Lab_1/TwoSensorsAAR.c @@ -17,16 +17,16 @@ int main() { float v_ref = 5, e_rc, e_comp; float thermistor_temp, thermocouple_temp; - int thermistor_pin, thermocouple_pin; // User input for pins A0 and A1 + int thermistor_val, thermocouple_val; // User input for pins A0 and A1 printf("Enter values for thermistor pin, thermocouple pin: "); - scanf("%d %d", &thermistor_pin, &thermocouple_pin); + scanf("%d %d", &thermistor_val, &thermocouple_val); // Calculate thermistor temperature in degrees C ( Part b, i,ii,iii & v) - thermistor_temp = kelvin_to_c(resistance_to_temperature(voltage_to_resistance(adc_to_voltage(v_ref, thermistor_pin)))); + thermistor_temp = kelvin_to_c(resistance_to_temperature(voltage_to_resistance(adc_to_voltage(v_ref, thermistor_val)))); // Calculate thermocouple temperature in degrees C ( Part c, i - iv) - e_rc = 1000*voltage_to_erc(adc_to_voltage(v_ref, thermistor_pin)); // convert to millivolts + e_rc = 1000*voltage_to_erc(adc_to_voltage(v_ref, thermocouple_val)); // convert to millivolts e_comp = NISTdegCtoMilliVoltsKtype(thermistor_temp); thermocouple_temp = NISTmilliVoltsToDegCKtype(e_rc + e_comp); diff --git a/Lab_1/TwoSensorsAAR.ino b/Lab_1/TwoSensorsAAR.ino new file mode 100644 index 0000000..575d948 --- /dev/null +++ b/Lab_1/TwoSensorsAAR.ino @@ -0,0 +1,191 @@ +/* Test program for reading of thermistor, thermocouple and LVDT. + K-type thermocouple functions written by Arthur Jones using + official NIST polynomial data from + https://srdata.nist.gov/its90/download/type_k.tab */ + +#include /* needed for exp() and pow() */ + +/* It is good practice to define things like pins used at the start + so that you avoid hard-coded values (magic numbers) in code */ +#define TCpin A0 +#define ThermistorPin A1 + +/* Similarly, define any constant values e.g. Vref, B, R0 here to avoid + need for "magic numbers" in code */ +#define V_REF 5 + +float adc_to_voltage(int n_adc); +float kelvin_to_c(float k); +float resistance_to_temperature(float r); +float voltage_to_resistance(float v); +float voltage_to_erc(float v); + +void setup() +{ + Serial.begin(9600); +} + +void loop() +{ + float e_rc, e_comp, thermistor_temp, thermocouple_temp; + int thermistor_val, thermocouple_val; + /* Put your code here to read ADCs and convert ADC voltages to + temperatures */ + thermistor_val = analogRead(ThermistorPin); + thermocouple_val = analogRead(TCpin) + // Calculate thermistor temperature in degrees C ( Part b, i,ii,iii & v) + thermistor_temp = kelvin_to_c(resistance_to_temperature(voltage_to_resistance(adc_to_voltage(thermistor_val)))); + + // Calculate thermocouple temperature in degrees C ( Part c, i - iv) + e_rc = 1000*voltage_to_erc(adc_to_voltage(thermocouple_val)); // convert to millivolts + e_comp = NISTdegCtoMilliVoltsKtype(thermistor_temp); + thermocouple_temp = NISTmilliVoltsToDegCKtype(e_rc + e_comp); + + /* Display results. Don't use printf or formatting etc., they don't work on the Arduino. Just use + the serial print statements given here, inserting your own code as needed */ + Serial.print("Thermistor temperature (deg C): "); + Serial.println(thermistor_temp); + Serial.print("Thermocouple temperature with CJC (deg C): "); + Serial.println(thermocouple_temp); + Serial.println("\n"); + delay(1000); +} + + +/* Write a function to convert ADC value to + voltage: put it here and use it in your code above*/ +float adc_to_voltage(int n_adc) { + return (float)n_adc*V_REF/1024.0; +} + + +/* Write a function to convert degrees K to degrees C +Call it from the main() function above */ +float kelvin_to_c(float k) { + return k-273.15; +} + + +float resistance_to_temperature(float r) { + // Define Thermistor constants + float t_0 = 298.15; + float r_0 = 10000; + float b = 3975; + + return 1 / ( (1/t_0) + (1/b)*log(r/r_0)); +} + + +float voltage_to_resistance(float v) { + return 1000*((10.0*3.3/v)-10.0); +} + + +float voltage_to_erc(float v) { + return (v-0.35)/54.4; +} + + +/* Under no circumstances change any of the following code, it is fine as it is */ +float NISTdegCtoMilliVoltsKtype(float tempDegC) +/* returns EMF in millivolts */ +{ + int i; + float milliVolts = 0; + if(tempDegC >= -170 && tempDegC < 0) + { + const float coeffs[11] = + { + 0.000000000000E+00, + 0.394501280250E-01, + 0.236223735980E-04, + -0.328589067840E-06, + -0.499048287770E-08, + -0.675090591730E-10, + -0.574103274280E-12, + -0.310888728940E-14, + -0.104516093650E-16, + -0.198892668780E-19, + -0.163226974860E-22 + }; + for (i=0; i<=10; i++) + { + milliVolts += coeffs[i] * pow(tempDegC,i); + } + } + else if(tempDegC >= 0 && tempDegC <= 1372) + { + const float coeffs[10] = + { + -0.176004136860E-01, + 0.389212049750E-01, + 0.185587700320E-04, + -0.994575928740E-07, + 0.318409457190E-09, + -0.560728448890E-12, + 0.560750590590E-15, + -0.320207200030E-18, + 0.971511471520E-22, + -0.121047212750E-25 + }; + const float a0 = 0.118597600000E+00; + const float a1 = -0.118343200000E-03; + const float a2 = 0.126968600000E+03; + + for (i=0; i<=9; i++) + { + milliVolts += coeffs[i] * pow(tempDegC,i); + } + + milliVolts += a0*exp(a1*(tempDegC - a2)*(tempDegC - a2)); + } + else + { + milliVolts = 99E9; + } + return milliVolts; +} + + +float NISTmilliVoltsToDegCKtype(float tcEMFmV) +// returns temperature in deg C. +{ + + int i, j; + float tempDegC = 0; + const float coeffs[11][3] = + { + {0.0000000E+00, 0.000000E+00, -1.318058E+02}, + {2.5173462E+01, 2.508355E+01, 4.830222E+01}, + {-1.1662878E+00, 7.860106E-02, -1.646031E+00}, + {-1.0833638E+00, -2.503131E-01, 5.464731E-02}, + {-8.9773540E-01, 8.315270E-02, -9.650715E-04}, + {-3.7342377E-01, -1.228034E-02, 8.802193E-06}, + {-8.6632643E-02, 9.804036E-04, -3.110810E-08}, + {-1.0450598E-02, -4.413030E-05, 0.000000E+00}, + {-5.1920577E-04, 1.057734E-06, 0.000000E+00}, + {0.0000000E+00, -1.052755E-08, 0.000000E+00} + }; + if(tcEMFmV >=-5.891 && tcEMFmV <=0 ) + { + j=0; + } + else if (tcEMFmV > 0 && tcEMFmV <=20.644 ) + { + j=1; + } + else if (tcEMFmV > 20.644 && tcEMFmV <=54.886 ) + { + j=2; + } + else + { + return 99E9; + } + + for (i=0; i<=9; i++) + { + tempDegC += coeffs[i][j] * pow(tcEMFmV,i); + } + return tempDegC; +}