Guest User

Arduino temperature controller

a guest
Jul 6th, 2013
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. /* Monitor average temperature with 1x 1% 10K thermistor,
  2. cycle relay based on temperature set point to control compressor,
  3. and circulating fan, display set point and current temperature */
  4.  
  5. // Set the nominal thermistor resistance, temp in C, and beta coef.
  6. #define thermNomR 10000
  7. #define thermNomT 25
  8. #define beta 3950
  9.  
  10. // Set the value of the resistor connected to ground
  11. #define gndRes 9910
  12.  
  13. // Number of samples to average and create array for iteration later
  14. #define numSamples 10
  15. int samples[numSamples];
  16.  
  17. // Define thermistor pin
  18. #define thermistor A0
  19.  
  20. // Set Digital Pin Assignments
  21. #define relay 3
  22. #define fan 4
  23. // #define tempUp 5
  24. // #define tempDown 6
  25.  
  26. // Setup the controller
  27. void setup()  {
  28.   Serial.begin(9600);
  29.   pinMode(relay, OUTPUT);
  30.   pinMode(fan, OUTPUT);
  31.   //pinMode(tempUp, INPUT);
  32.   //pinMode(tempDown, INPUT);
  33.  
  34.   //analogReference(external);
  35. }
  36.  
  37. // Control system programming
  38. void loop()  {
  39.   byte i;
  40.   float avg;
  41.  
  42.   // Take thermistor samples with slight delay
  43.   for (i = 0; i < numSamples; i++){
  44.     samples[i] = analogRead(thermistor);
  45.     delay(1000);
  46.   }
  47.  
  48.   // Calculate average thermistor value
  49.   avg = 0;
  50.   for (i = 0; i < numSamples; i++){
  51.     avg += samples[i];
  52.   }
  53.  
  54.   avg /= numSamples;
  55.  
  56.   // Convert ADC value to resistance
  57.   float thermistorResistance = gndRes * ((1023/avg) - 1);
  58.  
  59.   // Convert to resistance to temperature by Steinhart method
  60.   float steinhart;
  61.   steinhart = thermistorResistance / thermNomR;
  62.   steinhart = log(steinhart);
  63.   steinhart /= beta;
  64.   steinhart += 1.0 / (thermNomT + 273.15);
  65.   steinhart = 1.0 / steinhart;
  66.   steinhart -= 273.15;                     // Convert to C
  67.  
  68.   float temperature = (1.8 * steinhart) + 32;  // Convert temperature to F
  69.  
  70.   delay(100);        //let sensors stabilize
  71.  
  72.   // Uncomment for testing through serial console
  73.   /*
  74.   Serial.print("Average value on A0 ");
  75.   Serial.println(avg);
  76.  
  77.   float thermistorVoltage = avg * (5.0 / 1023.0);
  78.   Serial.print("Volts: ");
  79.   Serial.println(thermistorVoltage);
  80.  
  81.   Serial.print("Resistance: ");
  82.   Serial.println(thermistorResistance);
  83.  
  84.   Serial.print("Temp F: ");
  85.   Serial.println(temperature);
  86.  
  87.   Serial.print("Temp C: ");
  88.   Serial.println(steinhart);
  89.   */
  90.  
  91.   // Compare calculated values to set point and turn relay/fan on or off accordingly
  92.   if (temperature < 38) {
  93.     digitalWrite(relay, LOW);
  94.     //digitalWrite(fan, LOW);
  95.     i = 0;
  96.     for (i = 0; i < 3 * numSamples; i++) {
  97.     delay(60000);
  98.     /*
  99.     Serial.print("iteration :");
  100.     Serial.println(i + 1);
  101.     Serial.print("Temp F: ");
  102.     Serial.println(temperature);
  103.     */
  104.     }
  105.   }
  106.  
  107.   else {
  108.     digitalWrite(relay, HIGH);
  109.     //digitalWrite(fan, HIGH);
  110.   }  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment