-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy paththermistor.cpp
executable file
·129 lines (116 loc) · 3.46 KB
/
thermistor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Copyright (c) 2014 panStamp <[email protected]>
*
* This file is part of the panStamp project.
*
* panStamp is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* any later version.
*
* panStamp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with panStamp; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* Author: Daniel Berenguer
* Creation date: 11/05/2014
*
* This library simplifies the use of NTC thermistors by using Steinhart
* simplified formula.
* http://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation
*
* Based on procedure explained by Adafruit:
* https://learn.adafruit.com/thermistor/using-a-thermistor
*
* This library works only with the following circuit topology
*
* Vcc---NTC---ADC---SERIES_RESISTOR---GND
*/
#include "thermistor.h"
#include "HardwareSerial.h"
// Temperature for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// Number of ADC samples
#define NUMSAMPLES 5
// ADC resolution
#if defined(PANSTAMP_NRG) || defined(ESP_PLATFORM)
#define ADC_RESOLUTION 0xFFF
#else
#define ADC_RESOLUTION 1023
#endif
#define VERBOSE_SENSOR_ENABLED 1
/**
* THERMISTOR
*
* Class constructor
*
* @param adcPin Analog pin where the thermistor is connected
* @param nomRes Nominal resistance at 25 degrees Celsius
* @param bCoef beta coefficient of the thermistor
* @param serialRes Value of the serial resistor
*/
THERMISTOR::THERMISTOR(uint8_t adcPin, uint16_t nomRes, uint16_t bCoef, uint16_t serialRes)
{
analogPin = adcPin;
nominalResistance = nomRes;
bCoefficient = bCoef;
serialResistance = serialRes;
}
/**
* read
*
* Read temperature from thermistor
*
* @return temperature in 0.1 ºC
*/
int THERMISTOR::read(void)
{
uint8_t i;
uint16_t sample;
float average = 0;
#ifndef ESP_PLATFORM
analogReference(DEFAULT);
#endif
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++)
{
sample = analogRead(analogPin);
average += sample;
delay(10);
}
average /= NUMSAMPLES;
#ifdef VERBOSE_SENSOR_ENABLED
Serial.print("Average analog reading ");
Serial.println(average);
#endif
// convert the value to resistance
average = ADC_RESOLUTION / average - 1;
average = serialResistance * average;
#ifdef VERBOSE_SENSOR_ENABLED
Serial.print("Thermistor resistance ");
Serial.println(average);
#endif
float steinhart;
steinhart = average / nominalResistance; // (R/Ro)
#ifdef PANSTAMP_NRG
steinhart = logf(steinhart); // ln(R/Ro)
#else
steinhart = log(steinhart); // ln(R/Ro)
#endif
steinhart /= bCoefficient; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
#ifdef VERBOSE_SENSOR_ENABLED
Serial.print("Temperature ");
Serial.print(steinhart);
Serial.println(" *C");
#endif
return (int)(steinhart * 10);
}