#include "Arduino.h" #include "HermitCrab.h" #include "NTC_10K.h" #include "Config.h" const float resistance[] = { 3360850.37, // -40°C 1973470.32, // -35°C 1179560.43, // -30°C 718858.73, // -25°C 445267.47, // -20°C 281046.96, // -15°C 180321.36, // -10°C 117081.11, // -5°C 77147.90, // 0°C 51471.97, // 5°C 34838.43, // 10°C 23847.63, // 15°C 16594.38, // 20°C 12307.39, // 21°C 11739.87, // 22°C 11203.64, // 23°C 10696.86, // 24°C 10217.84, // 25°C 9527.52, // 26°C 9076.66, // 27°C 8656.02, // 28°C 8263.81, // 29°C 7897.84, // 30°C 5868.86, // 35°C 4383.72, // 40°C 3315.12, // 45°C 2527.73, // 50°C 1942.15, // 55°C 1505.11, // 60°C 1174.71, // 65°C 926.23, // 70°C 735.99, // 75°C 588.91, // 80°C 474.43, // 85°C 384.48, // 90°C 313.88, // 95°C 258.87, // 100°C 215.64, // 105°C 181.10, // 110°C 153.01, // 115°C 129.77, // 120°C 110.27, // 125°C 94.03, // 130°C 80.13, // 135°C 68.18, // 140°C 57.99, // 145°C 49.30 // 150°C }; const int16_t temp_C[] = { -40, -35, -30, -25, -20, -15, -10, -5, 0, 5, 10, 15, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150 }; NTC_10K ntc; void NTC_10K::setup(bool bNegativePolarity) { m_bNegativePolarity = bNegativePolarity; _vRef = 3.3f; _RESO = 4095; for (int i = 0; i < 16; i++) temps[i] = 0; temp_idx = 0; temp_sum = 0; pinMode(PIN_NTC, INPUT); // Set PIN_NTC as input analogReadResolution(12); // Set ADC resolution to 12 bits (0-4095) analogSetAttenuation(ADC_11db); // Set attenuation for full-scale 3.3V } void NTC_10K::readSensor() { float Vin; int16_t temp; static int16_t lastTemp = 0; int adcValue = analogRead(PIN_NTC); // Read ADC value from PIN_NTC // Calculate the input voltage from the ADC reading Vin = (float)adcValue * _vRef / _RESO; // Calculate the resistance of the thermistor // Calculate the resistance of the thermistor (adjusted for inverted ADC behavior) float r; if (m_bNegativePolarity) { // NTC is connected to Negative r = (Vin / (_vRef - Vin)) * rRef; } else { // NTC is connected to Positive r = ((_vRef - Vin) / Vin) * rRef; } // Find the index of the resistance in the table where r is between resistance[i-1] and resistance[i] int i = 0; while (i < sizeof(resistance) / sizeof(resistance[0]) - 1 && resistance[i] > r) { i++; } // If r is out of range, return the closest extreme temperature if (i == 0 || i == sizeof(resistance) / sizeof(resistance[0]) - 1) { if (lastTemp != 0) temp = lastTemp; else temp = 0; } else { // Interpolate between resistance[i-1] and resistance[i] float m = (temp_C[i] - temp_C[i - 1]) / (resistance[i] - resistance[i - 1]); // Slope float b = temp_C[i - 1] - (m * resistance[i - 1]); // Intercept temp = (int16_t) roundf((m * r + b) * 10.0f); } // Return the temperature as an integer scaled by 10 (e.g., 25.3°C => 253) temp_sum -= temps[temp_idx]; temps[temp_idx++] = temp; temp_idx &= NTC_MASK; temp_sum += temp; lastTemp = temp; m_nTemp = temp_sum / NTC_COUNT; }