Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Monitor average temperature with 1x 1% 10K thermistor,
- cycle relay based on temperature set point to control compressor,
- and circulating fan, display set point and current temperature */
- // Set the nominal thermistor resistance, temp in C, and beta coef.
- #define thermNomR 10000
- #define thermNomT 25
- #define beta 3950
- // Set the value of the resistor connected to ground
- #define gndRes 9910
- // Number of samples to average and create array for iteration later
- #define numSamples 10
- int samples[numSamples];
- // Define thermistor pin
- #define thermistor A0
- // Set Digital Pin Assignments
- #define relay 3
- #define fan 4
- // #define tempUp 5
- // #define tempDown 6
- // Setup the controller
- void setup() {
- Serial.begin(9600);
- pinMode(relay, OUTPUT);
- pinMode(fan, OUTPUT);
- //pinMode(tempUp, INPUT);
- //pinMode(tempDown, INPUT);
- //analogReference(external);
- }
- // Control system programming
- void loop() {
- byte i;
- float avg;
- // Take thermistor samples with slight delay
- for (i = 0; i < numSamples; i++){
- samples[i] = analogRead(thermistor);
- delay(1000);
- }
- // Calculate average thermistor value
- avg = 0;
- for (i = 0; i < numSamples; i++){
- avg += samples[i];
- }
- avg /= numSamples;
- // Convert ADC value to resistance
- float thermistorResistance = gndRes * ((1023/avg) - 1);
- // Convert to resistance to temperature by Steinhart method
- float steinhart;
- steinhart = thermistorResistance / thermNomR;
- steinhart = log(steinhart);
- steinhart /= beta;
- steinhart += 1.0 / (thermNomT + 273.15);
- steinhart = 1.0 / steinhart;
- steinhart -= 273.15; // Convert to C
- float temperature = (1.8 * steinhart) + 32; // Convert temperature to F
- delay(100); //let sensors stabilize
- // Uncomment for testing through serial console
- /*
- Serial.print("Average value on A0 ");
- Serial.println(avg);
- float thermistorVoltage = avg * (5.0 / 1023.0);
- Serial.print("Volts: ");
- Serial.println(thermistorVoltage);
- Serial.print("Resistance: ");
- Serial.println(thermistorResistance);
- Serial.print("Temp F: ");
- Serial.println(temperature);
- Serial.print("Temp C: ");
- Serial.println(steinhart);
- */
- // Compare calculated values to set point and turn relay/fan on or off accordingly
- if (temperature < 38) {
- digitalWrite(relay, LOW);
- //digitalWrite(fan, LOW);
- i = 0;
- for (i = 0; i < 3 * numSamples; i++) {
- delay(60000);
- /*
- Serial.print("iteration :");
- Serial.println(i + 1);
- Serial.print("Temp F: ");
- Serial.println(temperature);
- */
- }
- }
- else {
- digitalWrite(relay, HIGH);
- //digitalWrite(fan, HIGH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment