---
title: "Project: a thermistor curve for firmware"
heading: A series a microcontroller can afford, with a guaranteed error
eyebrow: A6 · Lesson 53 · Project: a thermistor curve for firmware
xp: 100
---

::: hook
A thermostat's microcontroller reads a thermistor's resistance and must report the temperature to 0.1 °C between 0 and 70 °C. The exact formula, $\frac1T = \frac{1}{T_0} + \frac{1}{B}\ln\frac{R}{R_0}$, needs a logarithm and a division, which a small processor without a floating-point unit computes slowly. In this project you'll replace the division with a geometric series whose error you can bound exactly, find how many terms the spec needs, compute the logarithm itself from a fast series, and count the multiplications the result costs.
:::

::: play
- The curve is the error of the truncated series for temperature, in °C, across 0 to 70 °C, for the number of terms you choose.
- Each extra term cuts the worst error by about a factor of 7.6 here: the series is geometric with ratio up to 0.131.
- Find the fewest terms that keep the whole curve inside ±0.1 °C.
:::

```sim plot
view: graph
x: T = 0..70 °C
y: err = -0.5..0.5
slider: n = 1..7 step 1 = 3
plot: -(1 - 298.15/(T + 273.15))^(n + 1)*(T + 273.15) | samples: 300 | label: series error
marker: err = 0.1 | label: +0.1
marker: err = -0.1 | label: -0.1
```

::: predict
## The series has ratio $q = -\frac{uT_0}{B}$ with $|q| \le 0.131$ over the range, and its error after $n$ terms is $T_0\frac{|q|^{n+1}}{1 - |q|}$ at worst. How many terms give 0.1 °C?

- [ ] 2
- [ ] 3
- [x] 4
- [ ] 10

::: reveal
Four. With $T_0$ = 298.15 K: three terms give $298.15 \times \frac{0.131^4}{0.869} = 0.101$ °C, just over the spec; four give $298.15 \times \frac{0.131^5}{0.869} = 0.013$ °C. Because the tail is exactly geometric, the bound is sharp: it's met at the hot end of the range, 70 °C, where $q$ is largest.
:::
:::

::: nameit
## The series

Let $u = \ln(R/R_0)$. Then $T = \frac{1}{1/T_0 + u/B} = \frac{T_0}{1 + uT_0/B}$, a geometric series in $q = -\frac{uT_0}{B}$:

$$T = T_0(1 + q + q^2 + q^3 + \cdots), \qquad |q| < 1$$

Truncating after the $q^n$ term leaves exactly $T_0\frac{q^{n+1}}{1 - q}$, so the worst error over a range is set by the largest $|q|$ there.

## The logarithm

Firmware computes $\ln x$ from $y = \frac{x - 1}{x + 1}$:

$$\ln x = 2\left(y + \frac{y^3}{3} + \frac{y^5}{5} + \cdots\right)$$

which converges for every $x > 0$ and fast when $x$ is near 1 (here $R/R_0$ runs from about 0.25 to 3.3).

## Cost

Evaluating $1 + q + q^2 + \cdots + q^n$ by Horner's rule, $1 + q(1 + q(1 + \cdots))$, takes $n$ multiplications and $n$ additions, with no division.
:::

::: example
## A thermistor ($R_0$ = 10 kΩ, $B$ = 3950 K) reads 25.0 kΩ. What temperature does the exact formula give, and what do two terms of the series give?

::: solution
1. **Find $u$.**
   $$\begin{aligned} u &= \ln 2.5 \\ &= 0.9163 \end{aligned}$$
2. **Use the exact formula.**
   $$\begin{aligned} \frac1T &= \frac{1}{298.15} + \frac{0.9163}{3950} \\ &= 0.003\,586\,0 \end{aligned}$$
   so $T = 278.86$ K, or 5.71 °C.
3. **Find $q$.**
   $$\begin{aligned} q &= -\frac{0.9163 \times 298.15}{3950} \\ &= -0.069\,16 \end{aligned}$$
4. **Sum two terms of the series.**
   $$\begin{aligned} T &\approx 298.15(1 - 0.069\,16 \\ &\qquad + 0.004\,78) \\ &= 278.96\ \text{K} \end{aligned}$$
   or 5.81 °C.
5. **Compare with the exact value.** The series is 0.09 °C high, which matches the truncation error:
   $$298.15 \times \frac{0.0692^3}{1 + 0.0692} = 0.092\ \text{°C}$$

**Answer:** the exact formula gives 278.86 K (5.71 °C), and two terms of the series give 278.96 K (5.81 °C), 0.09 °C high.
:::
:::

::: example
## How many terms of the $y$ series give $\ln 3$ to $10^{-12}$?

::: solution
1. **Find $y$.** For $x = 3$:
   $$\begin{aligned} y &= \frac{2}{4} \\ &= 0.5 \end{aligned}$$
2. **Count the terms.** The terms are
   $$\frac{2 \cdot 0.5^{2k+1}}{2k + 1}$$
   and they fall below $10^{-12}$ after 18 terms.
3. **Try $x = 0.2$.** There $y = -\frac23$, and it takes 30.
4. **Reduce the range.** Range reduction (dividing $x$ by a power of 2 first and adding multiples of $\ln 2$) keeps $|y|$ small and the term count low.

**Answer:** 18 terms give $\ln 3$ to $10^{-12}$.
:::
:::

::: check
given: R = 5 | 15 | 30
answer: 1/(1/298.15 + ln(R/10)/3950) - 273.15
unit: °C
tolerance: 0.2

## A thermistor ($R_0$ = 10 kΩ at 25 °C, $B$ = 3950 K) reads {R} kΩ. What is its temperature?

::: right
Right. {answer}.
:::

::: misses
- [1/(1/298.15 + log(R/10)/3950) - 273.15] Use the natural log.
- [any] Not yet. $\frac1T = \frac{1}{T_0} + \frac{\ln(R/R_0)}{B}$, then subtract 273.15.
:::
:::

::: check
given: q = 0.05 | 0.1 | 0.131; n = 2 | 3 | 4
answer: 298.15*q^(n + 1)/(1 - q)
unit: °C
tolerance: 0.5%

## The series is truncated after the $q^{\given{n}}$ term, with $|q|$ = {q}. What is the worst error?

::: right
Right. $T_0\frac{|q|^{n+1}}{1 - |q|}$ = {answer}.
:::

::: misses
- [298.15*q^n] The first term left out is $q^{n+1}$, and the rest of the tail adds the factor $\frac{1}{1 - q}$.
- [any] Not yet. Sum the geometric tail from $q^{n+1}$.
:::
:::

::: check
kind: python

## Write `T_exact(R)` (°C, from the Beta formula with $R_0$ = 10 000 Ω, $B$ = 3950 K, $T_0$ = 298.15 K) and `T_series(R, n)`, the geometric series in $q = -uT_0/B$ through $q^n$, also in °C. Set `err3` and `err4` to the largest error of `T_series` with 3 and 4 terms over 0 to 70 °C, checking every whole degree.

```python starter
import math

def T_exact(R):
    return 0

def T_series(R, n):
    return 0

err3 = err4 = 0
print(err3, err4)
```

```python solution
import math

B, T0, R0 = 3950, 298.15, 10000

def T_exact(R):
    return 1 / (1 / T0 + math.log(R / R0) / B) - 273.15

def T_series(R, n):
    q = -math.log(R / R0) * T0 / B
    return T0 * sum(q**k for k in range(n + 1)) - 273.15

Rs = [R0 * math.exp(B * (1 / (T + 273.15) - 1 / T0)) for T in range(0, 71)]
err3 = max(abs(T_series(R, 3) - T_exact(R)) for R in Rs)
err4 = max(abs(T_series(R, 4) - T_exact(R)) for R in Rs)
print(err3, err4)
```

```python tests
import math
assert abs(T_exact(10000) - 25) < 1e-9 and abs(T_exact(25000) - 5.712) < 0.01, "At R0 it reads 25 C; at 25 kohm, 5.71 C."
assert 0.1 < err3 < 0.105, f"Three terms miss the 0.1 C spec, just: worst error 0.1015 C at 70 C; got {err3:.4f}."
assert err4 < 0.0135, f"Four terms meet it: worst error 0.0133 C; got {err4:.4f}."
```

::: right
Right. Three terms miss the spec by a hair at 70 °C; four meet it with a margin of seven.
:::
:::

::: check
kind: python

## Write `bound(qmax, n)`, the geometric tail bound $T_0\frac{q_{\max}^{n+1}}{1 - q_{\max}}$. Set `qmax` to the largest $|q|$ over 0 to 70 °C, and `n_needed` to the smallest `n` with `bound(qmax, n) < 0.1`.

```python starter
import math

def bound(qmax, n):
    return 0

qmax = 0
n_needed = 0
print(qmax, n_needed)
```

```python solution
import math

B, T0, R0 = 3950, 298.15, 10000

def bound(qmax, n):
    return T0 * qmax**(n + 1) / (1 - qmax)

qmax = max(abs(B * (1 / (T + 273.15) - 1 / T0) * T0 / B) for T in range(0, 71))
n_needed = 0
while bound(qmax, n_needed) >= 0.1:
    n_needed += 1
print(qmax, n_needed)
```

```python tests
assert abs(qmax - 0.13114) < 1e-4, f"The largest |q| is 0.1311, at 70 C; got {qmax}."
assert n_needed == 4, f"The bound calls for 4 terms; got {n_needed}."
assert abs(bound(qmax, 3) - 0.1015) < 1e-3, "With 3 terms the bound is 0.1015 C, matching the measured worst error."
```

::: right
Right. The bound predicts the same answer as the brute-force scan, without evaluating the formula at all.
:::
:::

::: check
kind: python

## Write `ln_series(x, tol=1e-12)` returning `(value, terms)`: $2\sum\frac{y^{2k+1}}{2k + 1}$ with $y = \frac{x - 1}{x + 1}$, stopping when a term's size drops below `tol`. Check it on $\ln 3$ and $\ln 0.2$.

```python starter
import math

def ln_series(x, tol=1e-12):
    return math.log(x), 0

print(ln_series(3.0), ln_series(0.2))
```

```python solution
import math

def ln_series(x, tol=1e-12):
    y = (x - 1) / (x + 1)
    total, term, k = 0.0, y, 0
    while abs(2 * term / (2 * k + 1)) > tol:
        total += 2 * term / (2 * k + 1)
        term *= y * y
        k += 1
    return total, k

print(ln_series(3.0), ln_series(0.2))
```

```python tests
import math
v, k = ln_series(3.0)
assert abs(v - math.log(3)) < 1e-11 and k == 18, f"ln 3 should take 18 terms to 1e-12; got {k} terms, value {v}."
v, k = ln_series(0.2)
assert abs(v - math.log(0.2)) < 1e-11 and k == 30, f"ln 0.2 should take 30 terms; got {k}."
v, k = ln_series(1.1)
assert abs(v - math.log(1.1)) < 1e-12 and k <= 6, "Near 1 it should need very few terms."
```

::: right
Right. Near $x = 1$ the series converges in a handful of terms, which is why firmware scales its input close to 1 first.
:::
:::

::: check
kind: python

## Write `horner_geometric(q, n)` evaluating $1 + q + \cdots + q^n$ by Horner's rule, $1 + q(1 + q(\cdots))$, and returning `(value, mults)`, the result and the number of multiplications. Check it against the direct sum.

```python starter
def horner_geometric(q, n):
    return sum(q**k for k in range(n + 1)), 0

print(horner_geometric(-0.069, 4))
```

```python solution
def horner_geometric(q, n):
    v, mults = 1.0, 0
    for _ in range(n):
        v = 1 + q * v
        mults += 1
    return v, mults

print(horner_geometric(-0.069, 4))
```

```python tests
for q in (-0.13, -0.069, 0.05, 0.12):
    for n in (1, 3, 4, 7):
        v, m = horner_geometric(q, n)
        assert abs(v - sum(q**k for k in range(n + 1))) < 1e-15, f"Horner should equal the direct sum for q = {q}, n = {n}."
        assert m == n, f"Horner needs exactly n = {n} multiplications; got {m}."
```

::: right
Right. Four multiplications and four additions replace a division: a good trade on a processor without a divider.
:::
:::

::: check
kind: python

## Put it together: write `T_firmware(R)` that computes $u = \ln(R/R_0)$ with your `ln_series`, forms $q = -uT_0/B$, evaluates $1 + q + \cdots + q^4$ by Horner's rule, and returns the temperature in °C. Set `worst` to its largest error against the exact formula over 0 to 70 °C (every whole degree).

```python starter
import math

B, T0, R0 = 3950, 298.15, 10000

def T_firmware(R):
    return 25

worst = 0
print(worst)
```

```python solution
import math

B, T0, R0 = 3950, 298.15, 10000

def ln_series(x, tol=1e-12):
    y = (x - 1) / (x + 1)
    total, term, k = 0.0, y, 0
    while abs(2 * term / (2 * k + 1)) > tol:
        total += 2 * term / (2 * k + 1)
        term *= y * y
        k += 1
    return total

def T_firmware(R):
    q = -ln_series(R / R0) * T0 / B
    v = 1.0
    for _ in range(4):
        v = 1 + q * v
    return T0 * v - 273.15

exact = lambda R: 1 / (1 / T0 + math.log(R / R0) / B) - 273.15
Rs = [R0 * math.exp(B * (1 / (T + 273.15) - 1 / T0)) for T in range(0, 71)]
worst = max(abs(T_firmware(R) - exact(R)) for R in Rs)
print(worst)
```

```python tests
assert worst < 0.014, f"With four terms the pipeline should stay within 0.014 C; got {worst:.4f}."
assert abs(T_firmware(10000) - 25) < 1e-9, "At R0 it should read exactly 25 C."
assert abs(T_firmware(25000) - 5.712) < 0.02, f"At 25 kohm it should read about 5.71 C; got {T_firmware(25000)}."
```

::: right
Right. A logarithm by series, four multiply-adds and no division: within 0.014 °C over the whole range.
:::
:::

::: explain
## The series for $T$ is geometric, so its error after $n$ terms is known exactly. Why is that rarer, and more valuable, than it sounds?

::: model
Most Taylor series come with only a remainder bound: some unknown point $c$ where a higher derivative is evaluated, so the true error is somewhere below the bound, often far below, and you design with margin you may not need. A geometric series is different: its tail is itself a geometric series, which sums in closed form, so the error after $n$ terms is $\frac{q^{n+1}}{1 - q}$ times the leading factor, exactly, not just at most. That lets you pick the smallest number of terms that meets the specification with certainty, and know where in the range the worst case occurs (the largest $|q|$, here the hottest temperature). For firmware, every term saved is a multiplication saved on every reading, so an exact error is worth having.
:::
:::

::: practice

::: check
given: R = 2..50
answer: 1/(1/298.15 + ln(R/10)/3950) - 273.15
unit: °C
tolerance: 0.2

## Thermistor reads {R} kΩ: temperature in °C?

::: misses
- [any] Beta formula.
:::
:::

::: check
given: q = 0.01..0.3; n = 1..8 int
answer: 298.15*q^(n + 1)/(1 - q)
tolerance: 0.5%

## Geometric tail bound: $|q|$ = {q}, through $q^{\given{n}}$?

::: misses
- [any] $T_0\frac{q^{n+1}}{1 - q}$.
:::
:::
:::
