---
title: Problem set 4
heading: "Problem set 4: sensors, the ADC's input, fixed point and filters"
eyebrow: C12 · Problem set 4
type: problems
---

::: hook
These problems use lessons 16 to 19: reading sensors, the ADC's input, fixed point, and filtering in firmware. The ADC is 12 bits, 0 to 4095 for 0 to 3.3 V, and samples onto 10 pF through 1 kΩ. The thermistor is 10 kΩ at 25 °C with B = 3950 K, from 3.3 V to the pin, with 10 kΩ from the pin to ground.
:::

::: pool
pick: 12

::: check
kind: parts
given: T = 0 | 10 | 40 | 60

## The thermistor is at {T} °C.

::: part
answer: 10*exp(3950*(1/(T + 273.15) - 1/298.15))
unit: kΩ
tolerance: 0.5%

### What is its resistance?

::: misses
- [any] $R_0 e^{B(1/T - 1/T_0)}$ with T in kelvins.
:::
:::

::: part
answer: 3.3*10/(10*exp(3950*(1/(T + 273.15) - 1/298.15)) + 10)
unit: V
tolerance: 0.5%

### What voltage reaches the pin?

::: misses
- [3.3*10*exp(3950*(1/(T + 273.15) - 1/298.15))/(10*exp(3950*(1/(T + 273.15) - 1/298.15)) + 10)] The pin is across the fixed 10 kΩ, not the thermistor.
- [any] $3.3 \times 10/(R + 10)$.
:::
:::

::: part
answer: round(4095*10/(10*exp(3950*(1/(T + 273.15) - 1/298.15)) + 10))
tolerance: 0

### What code does the ADC give?

::: misses
- [any] $V \times 4095/3.3$, rounded.
:::
:::
:::

::: check
kind: parts
given: T = 0 | 25 | 60

## Near {T} °C, compare the thermistor's codes at {T} − 0.5 and {T} + 0.5 °C.

::: part
answer: 4095*10/(10*exp(3950*(1/(T + 0.5 + 273.15) - 1/298.15)) + 10) - 4095*10/(10*exp(3950*(1/(T - 0.5 + 273.15) - 1/298.15)) + 10)
unit: codes/°C
tolerance: 1%

### How many codes per degree is the sensor worth there?

::: misses
- [any] The code difference over that one degree.
:::
:::

::: part
answer: 1/(4095*10/(10*exp(3950*(1/(T + 0.5 + 273.15) - 1/298.15)) + 10) - 4095*10/(10*exp(3950*(1/(T - 0.5 + 273.15) - 1/298.15)) + 10))
unit: °C
tolerance: 1%

### What temperature step is one code?

::: misses
- [any] The reciprocal.
:::
:::
:::

::: check
kind: parts
given: V = 3.7 | 4.2 | 7.4; R1 = 100 | 200; R2 = 100 | 200

## A battery of {V} V is measured through {R1} kΩ over {R2} kΩ.

::: part
answer: round(V*R2/(R1 + R2)/3.3*4095)
tolerance: 0

### What code does the ADC read (if the pin stays under 3.3 V)?

::: misses
- [any] Pin voltage × 4095/3.3.
:::
:::

::: part
answer: V/(R1 + R2)*1000*8766/1000
unit: mAh
tolerance: 0.5%

### How much charge does the divider draw in a year (8766 hours)?

::: misses
- [any] $V/(R_1 + R_2)$ × 8766 h.
:::
:::

::: part
answer: (R1*R2/(R1 + R2) + 1)*1000*10e-12*ln(8192)*1e6
unit: µs
tolerance: 0.5%

### What acquisition time settles a full-scale change to half a code?

::: misses
- [any] $(R_1 \parallel R_2 + 1\text{ kΩ}) \times 10\text{ pF} \times \ln 8192$.
:::
:::
:::

::: check
kind: parts
given: R = 5 | 20 | 50; t = 1 | 3 | 10

## A scanned ADC switches between channels; channel A has a {R} kΩ source, and the acquisition time is {t} µs.

::: part
answer: 4096*exp(-t*1e-6/((R + 1)*1000*10e-12))
unit: codes
tolerance: 1%

### After a full-scale jump from the previous channel, how far off is channel A's reading?

::: misses
- [any] $4096\,e^{-t/\tau}$ with $\tau = (R_s + 1\text{ kΩ}) \times 10$ pF.
:::
:::

::: part
answer: (R + 1)*1000*10e-12*ln(8192)*1e6
unit: µs
tolerance: 0.5%

### What acquisition time would bring it within half a code?

::: misses
- [any] $9.01\tau$.
:::
:::
:::

::: check
kind: parts
given: e = 1 | 2 | 3; T = 25 | 50

## The 3.3 V rail, which is also the ADC's reference, is {e}% low. One sensor is the ratiometric thermistor divider; another is a 10 mV/°C sensor whose output doesn't depend on the rail, reading {T} °C.

::: part
answer: 0
unit: °C
tolerance: 0

### How far off does the thermistor read?

::: misses
- [any] Its output and the reference scale together.
:::
:::

::: part
answer: T*(1/(1 - e/100) - 1)
unit: °C
tolerance: 1%

### How far off does the 10 mV/°C sensor read, and in which direction (positive for high)?

::: misses
- [-T*e/100] A low reference makes every code larger: the reading is high.
- [any] Reading $= T/(1 - e)$.
:::
:::
:::

::: check
kind: parts
given: a = 1.5 | 2.75 | -3.25; b = 0.125 | 100.5

## {a} and {b} are multiplied in Q16.16 on a 32-bit processor.

::: part
answer: ceil(log(abs(round(a*65536)*round(b*65536)) + 1)/log(2)) + 1
unit: bits
tolerance: 0

### How many bits, including the sign, does the product of their stored integers need?

::: misses
- [any] $\lceil\log_2(|p| + 1)\rceil$ plus a sign bit.
:::
:::

::: part
answer: step(abs(round(a*65536)*round(b*65536)) - 2^31)
tolerance: 0

### Does that product overflow a signed 32-bit integer (1 for yes)?

::: misses
- [any] Compare its size with $2^{31}$.
:::
:::

::: part
answer: a*b
tolerance: 0.01%

### What value does the product represent after the shift?

::: misses
- [any] The true product.
:::
:::
:::

::: check
kind: parts
given: h = 0.1 | 0.3 | 0.47; q = 8 | 12 | 15

## A filter coefficient {h} is stored with {q} fraction bits.

::: part
answer: round(h*2^q)
tolerance: 0

### What integer stores it?

::: misses
- [any] $h \cdot 2^q$, rounded.
:::
:::

::: part
answer: abs(round(h*2^q)/2^q - h)
tolerance: 2%

### How far off is the stored value?

::: misses
- [any] Stored value over $2^q$, less {h}.
:::
:::
:::

::: check
kind: parts
given: c = 1000 | 2483 | 3999

## Firmware converts ADC code {c} to millivolts with `mv = code * 3300 / 4095` in 32-bit integers.

::: part
answer: floor(c*3300/4095)
unit: mV
tolerance: 0

### What does it compute?

::: misses
- [round(c*3300/4095)] Integer division truncates.
- [any] Multiply first, then divide, truncating.
:::
:::

::: part
answer: c*3300/4095 - floor(c*3300/4095)
unit: mV
tolerance: 1%

### How much does truncation lose?

::: misses
- [any] The exact value less the truncated one.
:::
:::

::: part
answer: floor(c/4095)*3300
unit: mV
tolerance: 0

### What does `code / 4095 * 3300` give instead?

::: misses
- [floor(c*3300/4095)] Dividing first truncates {c}/4095 to 0.
- [any] $c/4095$ truncates to 0 in integers.
:::
:::
:::

::: check
kind: parts
given: f = 500 | 1000 | 2000; h = 50 | 100

## Readings arrive at {f} Hz and carry hum at {h} Hz.

::: part
answer: f/h
unit: samples
tolerance: 0

### What moving-average length nulls the hum?

::: misses
- [any] $f_s/f_\text{hum}$.
:::
:::

::: part
answer: f/h*4095
tolerance: 0

### What is the largest running sum of 12-bit readings it can hold?

::: misses
- [any] Length × 4095.
:::
:::

::: part
answer: (f/h - 1)/2/f*1000
unit: ms
tolerance: 0.5%

### What delay does it add?

::: misses
- [any] $(N - 1)/2$ samples.
:::
:::
:::

::: check
kind: parts
given: k = 2 | 3 | 5; s = 3 | 8; S = 45 | 23

## A thermistor reading has {s} codes of rms noise, and near this temperature one degree is worth {S} codes. An exponential smoother uses $\alpha = 2^{-\given{k}}$.

::: part
answer: s*sqrt(2^(-k)/(2 - 2^(-k)))
unit: codes
tolerance: 0.5%

### How much noise survives the smoother?

::: misses
- [any] $\sigma\sqrt{\alpha/(2 - \alpha)}$.
:::
:::

::: part
answer: s*sqrt(2^(-k)/(2 - 2^(-k)))/S
unit: °C
tolerance: 0.5%

### What is that in degrees?

::: misses
- [any] Codes ÷ codes per degree.
:::
:::
:::

::: check
kind: parts
given: b = 1 | 2 | 3; r = 1000 | 10000

## A 12-bit ADC sampling at {r} Hz is oversampled and averaged, with enough noise to dither it, to gain {b} bits.

::: part
answer: 4^b
tolerance: 0

### How many samples must each output average?

::: misses
- [2^b] Each bit needs four times as many samples, since noise falls as $\sqrt N$.
- [any] $4^b$.
:::
:::

::: part
answer: r/4^b
unit: Hz
tolerance: 0.5%

### What output rate is left?

::: misses
- [any] Input rate ÷ samples per output.
:::
:::
:::

::: check
kind: parts
given: n = 3 | 5; g = 1 | 2

## A median-of-{n} filter runs on readings that contain isolated spikes, never more than {g} in a row.

::: part
answer: step((n - 1)/2 - g + 0.5)
tolerance: 0

### Does it remove every spike (1 for yes)?

::: misses
- [any] A median rejects runs of up to $(n - 1)/2$ outliers.
:::
:::

::: part
answer: (n - 1)/2
unit: samples
tolerance: 0

### What delay does it add to a clean step?

::: misses
- [any] The middle of {n} samples is $(n - 1)/2$ back.
:::
:::
:::

::: check
kind: parts
given: N = 16 | 64; s = 4 | 10; f = 100 | 1000

## A {N}-sample moving average runs at {f} Hz on readings with {s} codes of rms noise.

::: part
answer: s/sqrt(N)
unit: codes
tolerance: 0.5%

### How much noise remains?

::: misses
- [any] $\sigma/\sqrt N$.
:::
:::

::: part
answer: 2/(N + 1)
tolerance: 0.5%

### What smoother α leaves the same noise, given that a smoother passes $\sqrt{\alpha/(2 - \alpha)}$ of it?

::: misses
- [1/N] Solve $\alpha/(2 - \alpha) = 1/N$.
- [any] $\alpha = 2/(N + 1)$.
:::
:::

::: part
answer: -1/ln(1 - 2/(N + 1))
unit: samples
tolerance: 0.5%

### What is that smoother's time constant?

::: misses
- [any] $-1/\ln(1 - \alpha)$.
:::
:::
:::

::: check
kind: parts
given: L = 33 | 65 | 129; s = 128 | 256

## A thermistor table covers codes 0 to 4096 every {s} codes with 16-bit entries; a second table covers the same range with {L} entries.

::: part
answer: (4096/s + 1)*2
unit: bytes
tolerance: 0

### How much flash does the first table take?

::: misses
- [any] $4096/s + 1$ entries of 2 bytes.
:::
:::

::: part
answer: ((4096/s)/(L - 1))^2
tolerance: 0.5%

### By roughly what factor is the second table's interpolation error smaller (or larger, below 1)?

::: misses
- [any] The error goes as the square of the step.
:::
:::
:::
::: check
kind: parts
given: R = 4.7 | 10 | 22; C = 100 | 470; fs = 1000 | 10000

## A {R} kΩ source with {C} nF at the ADC pin feeds samples taken at {fs} Hz.

::: part
answer: 1/(2*pi*R*1000*C*1e-9)
unit: Hz
tolerance: 0.5%

### What is the filter's corner frequency?

::: misses
- [any] $1/(2\pi RC)$.
:::
:::

::: part
answer: -10*log(1 + (fs/2*2*pi*R*1000*C*1e-9)^2)
unit: dB
tolerance: 0.5%

### What gain does it have at half the sample rate, where aliasing begins?

::: misses
- [any] $-10\log_{10}(1 + (f/f_c)^2)$ at $f = f_s/2$.
:::
:::
:::
:::
