Advertisement
huebi

NTC.ino

Jul 15th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. /*
  2.  *-----------------------------------------------------------------------------
  3.  * Temperature measurement with NTC thermistors and the Teensy 3.2 board.
  4.  * This is the schema how the NTC is connected to the Teensy.
  5.  *
  6.  * <AREF (3.3V)>-----[Series Resistor]-----+-----[NTC]-------<Ground>
  7.  *                                         |
  8.  *                                         |
  9.  *                                 {Analog Pin A11}
  10.  *
  11.  * Author: huebi
  12.  *-----------------------------------------------------------------------------
  13.  */
  14.  
  15. #include <math.h>
  16. // Enumerating the three major temperature scales
  17. enum {
  18.     T_KELVIN=0,
  19.     T_CELSIUS,
  20.     T_FAHRENHEIT
  21. };
  22.  
  23. // manufacturer data for the thermistor
  24. const float B = 3470.0;     // Thermistor "B" parameter.
  25. const float T0 = 298.15;    // nominal temperature in K. In most cases 25 Celsius
  26. const float R0 = 5000.0;    // nominal resistance in Ohm at the nominal temperature
  27.  
  28. // settings for the electric schema
  29. const int AnalogPin = 11;   // Analog Pin 10 is connected to the NTC
  30. const int RS = 4990;        // Series resistor of the NTC in Ohm
  31.  
  32. #define NTCM_HP_5k AnalogPin,B,T0,R0,RS // B,T0,R0
  33. const float AbsZero = -273.15;  // Absolute zero point of temperature in Celsius
  34. const int ADresBits = 14;   // The resolution of the A/D converter to 14 bits
  35. const int ADsamples = 16;   // Number of samples approximated for each mesurement
  36. float ADmaxRead;        // The resolution of the A/D converter in steps
  37.  
  38. float Temperature(int OutputUnit,int AnalogPin,float B,float T0,float R0,float RS)
  39. {
  40.     float R;    // Actuall NTC resistance
  41.     float T;    // Actuall NTC temperature
  42.     int ADread; // A/D Converter input
  43.  
  44.     ADread = analogRead(AnalogPin);
  45.     R = RS * ADread / ADmaxRead / (1 - ADread / ADmaxRead);
  46.  
  47.     T = T0 * B / (B + T0 * log(R / R0));
  48.  
  49.     switch(OutputUnit) {
  50.         case T_CELSIUS :
  51.             T = T + AbsZero;
  52.         break;
  53.         case T_FAHRENHEIT :
  54.             T=9.0f*(T + AbsZero)/5.0f+32.0f;
  55.         break;
  56.         default: // T_KELVIN
  57.         break;
  58.     };
  59.  
  60.     return T;
  61. }
  62.  
  63. void setup() {
  64.     ADmaxRead=pow(2,ADresBits)-1;  
  65.     Serial.begin(115200);
  66.     while (!Serial); // Wait for the serial monitor to open to continue
  67.     analogReadRes(ADresBits);
  68.     analogReadAveraging(ADsamples);
  69. }
  70.  
  71. void loop() {
  72.     Serial.print("Temperature: ");
  73.     Serial.print(Temperature(T_CELSIUS,NTCM_HP_5k));
  74.     Serial.println(" Celsius");
  75.     delay(500);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement