/**
Assumes a 10K@25℃ NTC thermistor connected in series with a 10K resistor.
*/
#include <SD.h>
#define CS_PIN 10
String filename = "Arduino_Project.txt";
File curFile;
void setup() {
Serial.begin(9600);
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}
}
void loop() {
// Turn an analog signal into a temperature measurement
int tempValue = analogRead(A0);
float temperature = 1 / (log(1 / (1023. / tempValue - 1)) / 3950 + 1.0 / 298.15) - 273.15;
Serial.print("The current temperature is: ");
Serial.print(temperature);
Serial.println(" C");
// Save the temperature reading to the SD card
curFile = SD.open(filename, FILE_WRITE);
if (curFile) {
curFile.println(temperature);
curFile.close();
} else {
Serial.println("error writing to file");
}
// Read all logged temperatures and average them
curFile = SD.open(filename, FILE_READ);
if (curFile) {
float tempSum = 0.0;
int tempCount = 0;
while (curFile.available()) {
tempSum += curFile.readStringUntil('\n').toFloat();
tempCount++;
}
curFile.close();
Serial.print("The average temperature measured is: ");
Serial.print(tempSum/tempCount);
Serial.println(" C \n");
} else {
Serial.println("error reading from file");
}
delay(2000);
}