NtcThermistor.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "NtcThermistor.h"
  2. // 构造函数
  3. NtcThermistor::NtcThermistor(int pin1, int pin2, int pin3,
  4. float referenceResistance,
  5. float ntcNominalResistance,
  6. float ntcBetaValue,
  7. float referenceTemperature,
  8. int adcResolution) :
  9. _pin1(pin1),
  10. _pin2(pin2),
  11. _pin3(pin3),
  12. _referenceResistance(referenceResistance),
  13. _ntcNominalResistance(ntcNominalResistance),
  14. _ntcBetaValue(ntcBetaValue),
  15. _referenceTemperature(referenceTemperature + 273.15), // 转换为开尔文
  16. _adcResolution(adcResolution) {
  17. // 初始化温度值
  18. _temperature1 = 0.0;
  19. _temperature2 = 0.0;
  20. _temperature3 = 0.0;
  21. // 配置引脚为输入
  22. pinMode(_pin1, INPUT);
  23. pinMode(_pin2, INPUT);
  24. pinMode(_pin3, INPUT);
  25. }
  26. // 读取单个NTC温度(优化版本)
  27. float NtcThermistor::readTemperature(int pin) {
  28. // 多次采样取平均,减少噪声
  29. int adcSum = 0;
  30. for (int i = 0; i < 20; i++) {
  31. adcSum += analogRead(pin);
  32. delayMicroseconds(100);
  33. }
  34. int adcValue = adcSum / 20;
  35. // 调试:打印ADC原始值(方便排查问题)
  36. Serial.print("Pin ");
  37. Serial.print(pin);
  38. Serial.print(" ADC: ");
  39. Serial.print(adcValue);
  40. // 防止除以零错误(放宽范围,避免误判)
  41. if (adcValue < 5 || adcValue > _adcResolution - 5) {
  42. Serial.println(" -> 超出范围");
  43. return -273.15;
  44. }
  45. // 计算NTC电阻(关键公式修正)
  46. // 注意:这里使用(float)强制转换,避免整数除法导致精度丢失
  47. float resistance = _referenceResistance * ((float)_adcResolution / adcValue - 1.0);
  48. Serial.print(" | 电阻: ");
  49. Serial.print(resistance);
  50. Serial.print("Ω");
  51. // 使用B参数方程计算温度(修正计算顺序)
  52. float steinhart;
  53. steinhart = resistance / _ntcNominalResistance; // (R/R0)
  54. steinhart = log(steinhart); // ln(R/R0)
  55. steinhart /= _ntcBetaValue; // 1/B * ln(R/R0)
  56. steinhart += 1.0 / _referenceTemperature; // + 1/T0 (T0为25℃的开尔文温度)
  57. steinhart = 1.0 / steinhart; // 计算得到开尔文温度
  58. float celsius = steinhart - 273.15; // 转换为摄氏度
  59. Serial.print(" | 温度: ");
  60. Serial.println(celsius);
  61. return celsius;
  62. }
  63. // 更新所有温度读数
  64. void NtcThermistor::updateTemperatures() {
  65. _temperature1 = readTemperature(_pin1);
  66. _temperature2 = readTemperature(_pin2);
  67. _temperature3 = readTemperature(_pin3);
  68. }
  69. // 获取温度值
  70. float NtcThermistor::getTemperature1() {
  71. return _temperature1;
  72. }
  73. float NtcThermistor::getTemperature2() {
  74. return _temperature2;
  75. }
  76. float NtcThermistor::getTemperature3() {
  77. return _temperature3;
  78. }
  79. // 获取格式化的温度字符串
  80. String NtcThermistor::getTemperature1String() {
  81. return String(_temperature1, 1) + " °C";
  82. }
  83. String NtcThermistor::getTemperature2String() {
  84. return String(_temperature2, 1) + " °C";
  85. }
  86. String NtcThermistor::getTemperature3String() {
  87. return String(_temperature3, 1) + " °C";
  88. }