forked from jnsbyr/esp8266-intexsbh20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNTCThermometer.cpp
112 lines (100 loc) · 2.57 KB
/
NTCThermometer.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
/*
* project: generic
*
* file: NTCThermometer.cpp
*
* encoding: UTF-8
* created: 13th March 2021
*
* Copyright (C) 2021 Jens B.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include "NTCThermometer.h"
#include <Esp.h>
// set ADC to read from input pin A0
ADC_MODE(ADC_TOUT)
/**
* @param refResistance resistance between NTC and GND [Ohm]
* @param refVoltage voltage at NTC [V]
* @param adcScale inverted voltage divider relation of analog input (Wemos D1 mini: 320/100)
*/
void NTCThermometer::setup(unsigned int refResistance, float refVoltage, float adcScale)
{
this->refResistance = refResistance;
this->refVoltage = refVoltage;
this->adcScale = adcScale/1024; // -> [V/digit]
}
/**
* read analog input multiple times and return average [digits]
*
* @return average digits
*/
float NTCThermometer::analogReadMultiple()
{
long sum = 0;
unsigned int count = 0;
for (unsigned int i=0; i<CONSECUTIVE_SAMPLES; i++)
{
int sample = analogRead(A0);
if (sample >= 0)
{
sum += sample;
count++;
}
}
if (count == 0)
{
count = 1;
}
return (float)sum/count;
}
/**
* measure resistance R of a voltage divider:
* Vref - R (NTC, var) - Rref - GND
*
* return resistance [Ohm]
*/
int NTCThermometer::getResistance()
{
return round((refVoltage/(adcScale*analogReadMultiple()) - 1)*refResistance);
}
/**
* sample current 10k NTC temperature [°C] and calculate block moving average
*
* see Arduino Playground: "Reading a Thermistor"
*
* @return temperature [°C]
*/
float NTCThermometer::getTemperature()
{
// calculate current temperature and
// update temperature history ring buffer
float x = log(getResistance());
history[historyHead++] = 1.0f / (0.001129148f + 0.000234125f*x + 0.0000000876741f*x*x*x) - 273.15f;
historyHead %= HISTORY_DEPTH;
if (historyDepth < HISTORY_DEPTH)
{
historyDepth++;
}
// calculate block moving average
float sum = 0;
for (unsigned int i=0; i<historyDepth; i++)
{
sum += history[i];
}
return sum/historyDepth;
}