// which analog pin to connect
#define THERMISTORPIN A1 // this is pin #7
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000
// the temperature where you are going to start calibration
#define START_TEMP 50
int samples[NUMSAMPLES];
int outPin0 = 0; // Actual pin on chip is #5 Red
int outPin1 = 1; // Actual pin on chip is #6 Green
void setup(void) {
pinMode(outPin0, OUTPUT); // Red
pinMode(outPin1, OUTPUT); // Green
}
void loop(void) {
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
float calcnum;
calcnum = average / THERMISTORNOMINAL; // (R/Ro)
calcnum = log(calcnum); // ln(R/Ro)
calcnum /= BCOEFFICIENT; // 1/B * ln(R/Ro)
calcnum += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
calcnum = 1.0 / calcnum; // Invert
calcnum -= 273.15; // convert to C
float calcnumF;
// Convert to Fahrenheit
calcnumF = (9 / 5) * calcnum + 32;
for (int i=0;i<=10;i+=1){
// Green LED is the counter and independent of red
analogWrite(outPin1, 255);
delay(100);
/* I began with 60 (my room temp was about 74)
You need to start somewhere, change this to suit
it may take a number of tries.
What happens is that the green LED is the counter, and
you count the number of times it blinks until the red LED
comes on. When that comes on, you can disconnect the power
from the gadget. Subtract the number of green LED counts
from START_TEMP (see definitions at the top). That will be
the ambient temperature of the room. You have calibrated
the red LED.
*/
if (calcnumF > START_TEMP - i) // count down until it is not hot
{
// Red LED
analogWrite(outPin0, 255);
delay(100);
// analogWrite(outPin0, 0);
delay(10);
} // end if calcnumF
analogWrite(outPin1, 0); // turn off green pin
delay(100);
} // end for
exit(0); // just quit; red LED is probably on at this point
// green LED should be off
} // end loop