Skip to content

Commit

Permalink
correcting adc temp calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
fikin committed Sep 30, 2023
1 parent 15ed990 commit 0c59f12
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
30 changes: 26 additions & 4 deletions lua_modules/adc-ntc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,37 @@ Schematic is:

The default configuration is defined for:

- VCC=5V
- VccR1=5V
- R1=330k
- Rntc=50k
- NodeMCU board

$Vntc = VCC * Rntc / ( Rntc + R1 )$
_Note: stock ESP8266 chip accepts 1V in A0 input. NodeMCU boards are shipped with built-in voltage divider and accept 3.3V._

$Adc = 1023 * Vntc / 3.3$
Since resistor and voltage values are not perfect, there are some discrepancies between schematic and reality.
There are two way to influence for biased results:

where 1023 is Adc resolution and 3.3V is the A0 input max voltage i.e. NodeMCU board. Plain ESP8266 chips accept 1v max.
- Adjust R1 resistor value in the settings.
- With default configuration, each +15k => +1C.
- Use `correction factor`.
- Correction factor is ADC value added to A0 reading before Vntc is being calculated.
- With default configuration, +9 => +1C.

Perform temperature calibration or observe long term and adjust these if you see fit.

$Vstep = VccA0 / 1023$

$AdcValue = A0 + correction_factor$

$Vntc = Vstep * AdvValue$

$Rntc = R1 * Vntc / ( VccR1 - Vntc )$

$TempK = A + B * ln(Rntc) + C * ln(Rntc)^3$

$TempC = TempK - 273.15$

With default configuration following values could be expected:

| Temp (C) | Rntc (kOhm) | Vntc (V) | Adc (0-1024) |
| -------- | ----------- | -------- | ------------ |
Expand Down
5 changes: 3 additions & 2 deletions lua_modules/adc-ntc/fs/fs-adc-ntc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"AdcCorr": 1.09,
"Vcc": 3.3,
"AdcCorr": 0,
"VccA0": 3.3,
"VccR1": 5,
"R1": 330000,
"A": 9.656249129e-4,
"B": 2.106952165e-4,
Expand Down
10 changes: 6 additions & 4 deletions lua_modules/adc-ntc/lua/adc-ntc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ local adc = require("adc")

---@class adcNtc_cfg
---@field AdcCorr integer correction factor for read ADC values. 1 means no correction.
---@field Vcc integer max voltage at A0 input i.e 3.3V for NodeMCU and 1V for ESP8266
---@field VccA0 integer max voltage at A0 input i.e 3.3V for NodeMCU and 1V for ESP8266
---@field VccR1 integer voltage at R1 i.e 5V or 3.3V
---@field R1 integer resistor 1 in Kohm
---@field A number Steinhart-Hart model coefficient
---@field B number Steinhart-Hart model coefficient
Expand All @@ -36,9 +37,10 @@ end
local function main(onReadCb)
package.loaded[modname] = nil

local AdcValue = readAvg(20) * cfg.AdcCorr
local Vntc = cfg.Vcc * AdcValue / 1023
local Rntc = cfg.R1 * Vntc / (cfg.Vcc - Vntc)
local Vstep = cfg.VccA0 / 1023
local AdcValue = readAvg(20) + cfg.AdcCorr
local Vntc = Vstep * AdcValue
local Rntc = cfg.R1 * Vntc / (cfg.VccR1 - Vntc)
local LogRntc = math.log(Rntc)
local bVal = cfg.B * LogRntc
local cVal = cfg.C * LogRntc * LogRntc * LogRntc
Expand Down
14 changes: 8 additions & 6 deletions lua_modules/web-portal/fs/wifi-portal.html
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,18 @@ <h3 class="collapsible">Temp Sensor (ds18b20)</h3>
<h3 class="collapsible">Temp Sensor (adc NTC)</h3>
<div class="collapsible-content grid-container">
<label for="adcNtc_AdcCorr" class="grid-item"
>Read ADC-values correction factor<br />
use this to long term tune temp calculation<br />
start with 1 (no correction) and adjust<br />
>Read ADC-values correction factor.<br />
Use this to long term tune temp calculation.<br />
Start with 0 (no correction) increase/decrease<br />
after comparing room temp and sensor results:</label
>
<input id="adcNtc_AdcCorr" class="cfgValue grid-item" type="number" />
<input id="adcNtc_AdcCorr" class="cfgValue grid-item" type="number" step="1" />
<label for="adcNtc_R1" class="grid-item">Resistor 1 (ohm):</label>
<input id="adcNtc_R1" class="cfgValue grid-item" type="number" min="1" />
<label for="adcNtc_Vcc" class="grid-item">VCC (V):</label>
<input id="adcNtc_Vcc" class="cfgValue grid-item" type="number" min="1" />
<label for="adcNtc_VccA0" class="grid-item">VCC at A0 (V)<br />i.e 3.3 (NodeMCU) or 1 (ESP):</label>
<input id="adcNtc_VccA0" class="cfgValue grid-item" type="number" min="1" />
<label for="adcNtc_VccR1" class="grid-item">VCC at R1 (V)<br />likely 3.3 or 5:</label>
<input id="adcNtc_VccR1" class="cfgValue grid-item" type="number" min="1" />
<label for="adcNtc_A" class="grid-item">Steinhart-Hart model coefficient A:</label>
<input id="adcNtc_A" class="cfgValue grid-item" type="number" min="1" />
<label for="adcNtc_B" class="grid-item">Steinhart-Hart model coefficient B:</label>
Expand Down

0 comments on commit 0c59f12

Please sign in to comment.