// Carlos Alvarado-Lopez // ENGR 103 Final Project, Thermistor temperature collecting project // For my final project, I wanted to collect the temperature in my dorm room and decided to use a thermistor. This code runs the thermistor and makes readings every second, then // when requested can output the collected 10-minute averages, the highest and lowest overall temperature, and the overall average. //Values of the thermistor used throughout the code as well as assigning the pin to the thermistor const int THERMISTOR_PIN = 35; const int REFERENCE_VOLTAGE = 3300; const int NOMINAL_RESISTANCE = 10000; const int NOMINAL_TEMPERATURE = 25; const int TEMPERATURE_COEFFICIENT = 3950; //Assigns the variables of previousMillis used as a time reference point and the interval being 600000 milliseconds which equals 10 minutes unsigned long previousMillis = 0; const unsigned long interval = 600000; //Declares different variables and arrays used throughout the program int readings[600]; //Sets integer array of size 600 int currentIndex = 0; //Keeps track of the current index float temperatureSum = 0; //Accumulates the sum of all temperature readings int readingCount = 0; //Keeps track of temperature readings that have been collected so far float highestTemperature = -273.15; float lowestTemperature = 1000; float overallSum = 0; int overallCount = 0; //Keeps track of number of temperature readings over time int averageCount = 0; float tenMinuteAverages[144]; //Stores the 10-minute averages int averageIndex = 0; void setup() { Serial.begin(115200); pinMode(THERMISTOR_PIN, INPUT); //Sets the pin mode with for loop starts each element to 0. Goes from 0 to 599 for (int i = 0; i < 600; i++) { readings[i] = 0; } } void loop() { // Check if serial data from thermistor is available if (Serial.available()) { char command = Serial.read(); // If "a" is typed, perform calculations and output results on the serial monitor if (command == 'a') { if (readingCount > 0) { // Calculate the overall average temperature float overallAverage = overallSum / overallCount; // Outputs all the 10-minute averages collected so far Serial.println("10-Minute Averages:"); for (int i = 0; i < averageCount; i++) { Serial.print("Average "); Serial.print(i + 1); Serial.print(": "); Serial.print(tenMinuteAverages[i], 2); Serial.println(" °C"); } // Outputs highest temperature Serial.print("Highest Temperature: "); Serial.print(highestTemperature, 2); Serial.println(" °C"); // Outputs lowest temperature Serial.print("Lowest Temperature: "); Serial.print(lowestTemperature, 2); Serial.println(" °C"); // Output overall average temperature Serial.print("Overall Average Temperature: "); Serial.print(overallAverage, 2); Serial.println(" °C"); } else { Serial.println("No temperature readings available."); } } } // Read the ADC value and calculate the temperature using steinhart int adcValue = analogRead(THERMISTOR_PIN); float voltage = (adcValue / 4095.0) * REFERENCE_VOLTAGE; float resistance = ((REFERENCE_VOLTAGE - voltage) * NOMINAL_RESISTANCE) / voltage; float steinhart = 1.0 / (1.0 / (NOMINAL_TEMPERATURE + 273.15) + (1.0 / TEMPERATURE_COEFFICIENT) * log(resistance / NOMINAL_RESISTANCE)); steinhart -= 273.15; // Stores the current temperature reading in the array readings[currentIndex] = steinhart; // Updates the current index for the circular buffer meant to store data currentIndex = (currentIndex + 1) % 600; // Update the sum and count for the current interval temperatureSum += steinhart; readingCount++; // Update the overall sum and count overallSum += steinhart; overallCount++; // Update the highest and lowest temperatures if (steinhart > highestTemperature) { highestTemperature = steinhart; } if (steinhart < lowestTemperature) { lowestTemperature = steinhart; } // Check if the interval for calculating the average has passed unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // Update the previous time // Calculate the average temperature for the current interval float averageTemperature = temperatureSum / readingCount; // Store the average in the array of 10-minute averages tenMinuteAverages[averageIndex] = averageTemperature; averageIndex++; // Reset the temperature sum and reading count for the next interval temperatureSum = 0; readingCount = 0; // Increment the count of 10-minute averages collected averageCount++; } // Print the current temperature value per second Serial.print("Temperature: "); Serial.print(steinhart, 2); Serial.println(" °C"); delay(1000); // Delay for 1 second }