const uint8_t OUTPUT_PIN = A0;
// サーミスタの BETA パラメータ
const float BETA = 3950;
void setup() {
Serial.begin(9600);
}
float convertToTemperatureCelsius(int analogValue) {
// [ ( 1 1 )]
// analogValue = (1023. - analogValue) * exp[BETA * (-------------------- - --------)]
// [ ( temperatureCelsius 298.15 )]
//
// を temperatureCelsius について解き,
// 単位を [K] から [℃] に変換
//
// 298.15[K] = 25[℃]
float temperatureCelsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
return temperatureCelsius;
}
void loop() {
int analogValue = analogRead(OUTPUT_PIN);
float temperatureCelsius = convertToTemperatureCelsius(analogValue);
Serial.print("温度: ");
Serial.print(temperatureCelsius);
Serial.println(" [℃]");
delay(1000);
}