// // A Program for reading the on-board temperature sensor. // Prints a calibrated temperature in degrees Fahrenheit. // enum { SAMPLES_TO_AVERAGE = 500 }; double temperature( void ) { // // Get the factory calibration values from the chip // volatile unsigned int *CAL_ADC_15T30_PTR = (unsigned int *) (0x10DA+0x08); volatile unsigned int *CAL_ADC_15T85_PTR = (unsigned int *) (0x10DA+0x0A); int cal30 = *CAL_ADC_15T30_PTR; int cal85 = *CAL_ADC_15T85_PTR; long int average = 0; for( int a = 0; a < SAMPLES_TO_AVERAGE; a++ ) { average += analogRead( TEMPSENSOR ) * 100L; delay( 20 ); } average /= SAMPLES_TO_AVERAGE; long int temp_minus_30_C = (average - cal30 * 100L) * 1000L; int x_distance = cal85 - cal30; long int counts_per_degree_C = (x_distance * 1000L) / (85L-30L); long int counts_per_degree_F = (x_distance * 1000L) / (185L-86L); // long int degrees_C = temp_minus_30_C / counts_per_degree_C + 30*100L; long int degrees_F = temp_minus_30_C / counts_per_degree_F + 86*100L; return degrees_F / 100.0; } void setup() { analogReference( INTERNAL1V5 ); analogRead( TEMPSENSOR ); // Throw away the first reading -- it is usually bogus Serial.begin( 9600 ); } void loop() { double temp = temperature(); Serial.print( "The temperature is " ); Serial.print( temp ); Serial.println( " degrees Fahrenheit" ); delay( 10 ); }