---
title: Reading sensors with the ADC
heading: From a voltage to a number to a temperature
eyebrow: C12 · Lesson 16 · Reading sensors with the ADC
xp: 40
---

::: hook
Most sensors produce a voltage: a potentiometer's wiper, a thermistor in a divider, a light sensor, a microphone. The microcontroller's **analog-to-digital converter** turns that voltage into a number, here a 12-bit code from 0 to 4095 for 0 to 3.3 V. Getting from the code back to a physical quantity is the program's job: scale the code to volts, then use the sensor's equation to turn volts into degrees, lux or position. C11 covered converters in general; this lesson is about using one from code.
:::

::: play
- A potentiometer is on ADC0 and a 10 kΩ thermistor (in a divider with a 10 kΩ resistor) on ADC1. The program prints the pot's voltage and the temperature five times.
- Move the potentiometer slider and the thermistor's temperature slider: each change runs the program again. Check that the printed temperature follows the slider.
- The temperature conversion inverts the divider to get the thermistor's resistance, then applies the Beta equation. Read through `celsius` and match it to the formulas below.
:::
:::

```sim board
lang: micropython
attach: pot ADC0 = 1.2 V
attach: thermistor ADC1 | temp 25
run: 1 s
code:
from machine import ADC
import time, math

pot = ADC(0)
therm = ADC(1)

def volts(adc):
    return adc.read() * 3.3 / 4095        # 12 bits: 0..4095 for 0..3.3 V

def celsius(v):
    # thermistor from 3.3 V to the pin, 10 kΩ from the pin to ground
    r = 10000 * (3.3 - v) / v              # the thermistor's resistance
    t = 1 / (1 / 298.15 + math.log(r / 10000) / 3950)
    return t - 273.15

for i in range(5):
    print('pot %.3f V, temperature %.1f C' % (volts(pot), celsius(volts(therm))))
    time.sleep_ms(200)
```

::: predict
## The thermistor is 10 kΩ at 25 °C, in the play's divider with a 10 kΩ resistor. As it warms, its resistance falls. Does ADC1's reading rise or fall?

- [x] Rise
- [ ] Fall
- [ ] Stay at 2048
- [ ] It depends on the reference voltage

::: reveal
Rise. The thermistor is on top, from 3.3 V to the pin, and the fixed resistor below, so $V = 3.3 \cdot 10\text{k}/(10\text{k} + R_T)$: a smaller $R_T$ means a larger share of the 3.3 V across the fixed resistor. At 25 °C the two are equal and the code is 2048; at 50 °C ($R_T$ = 3588 Ω) it's 3014.
:::
:::

::: nameit
## Codes and volts

This board's ADC is 12 bits over 0 to 3.3 V:

$$\text{code} = \text{round}\left(\frac{V}{3.3}\times 4095\right), \qquad V = \text{code}\times\frac{3.3}{4095}$$

One step is about 0.81 mV. A conversion takes 10 µs.

| | |
|---|---|
| C | `ADC_CTRL = ch \| ADC_START;` wait for `ADC_DONE` in `ADC_STATUS`, clear it, read `ADC_DATA` |
| MicroPython | `ADC(ch).read()` (0 to 4095), `read_u16()` (scaled to 16 bits), `read_uv()` (microvolts) |

## A thermistor in a divider

An NTC thermistor $R_T$ from 3.3 V to the pin and a fixed $R_f$ to ground give

$$V = 3.3\,\frac{R_f}{R_f + R_T}, \qquad R_T = R_f\,\frac{3.3 - V}{V}$$

and the **Beta equation** turns resistance into temperature (in kelvin):

$$\frac{1}{T} = \frac{1}{T_0} + \frac{1}{B}\ln\frac{R_T}{R_0}$$

with $R_0$ the resistance at $T_0$ = 298.15 K (25 °C). Here $R_0$ = 10 kΩ and B = 3950 K.

## Noise

Averaging N readings reduces random noise by $\sqrt N$ (C11). Reading many samples and averaging is the simplest filter in firmware.
:::

::: example
## ADC1 reads 2482. What's the voltage, the thermistor's resistance, and the temperature?

::: solution
1. **Code to volts.**
   $$\begin{aligned} V &= 2482 \times \frac{3.3}{4095}\ \text{V} \\ &= 2.000\ \text{V} \end{aligned}$$
2. **Volts to resistance.**
   $$\begin{aligned} R_T &= 10\ \text{k}\Omega \times \frac{3.3 - 2.0}{2.0} \\ &= 6.50\ \text{k}\Omega \end{aligned}$$
3. **Resistance to temperature.** Put $R_T/R_0 = 0.65$ in the Beta equation:
   $$\begin{aligned} \frac{1}{T} &= \frac{1}{298.15} + \frac{\ln 0.65}{3950} \\ &= 0.0032450\ \text{K}^{-1} \end{aligned}$$
4. **Invert it.**
   $$\begin{aligned} T &= 308.17\ \text{K} \\ &= 35.0\ \text{°C} \end{aligned}$$

**Answer:** 2.000 V, 6.50 kΩ and 35.0 °C.
:::
:::

::: example
## What code does 1.0 V give, and how much does one step of the code represent?

::: solution
1. **Scale the voltage to a code.**
   $$\frac{1.0}{3.3} \times 4095 = 1240.9$$
   so 1241.
2. **One step.**
   $$\frac{3.3\ \text{V}}{4095} = 0.806\ \text{mV}$$

**Answer:** 1.0 V gives code 1241, and one step is 0.806 mV.
:::
:::

::: example
## An ADC reading has 4 codes of rms noise. How much does averaging 16 readings help, and what is one code in volts?

::: solution
1. **Average 16 readings.** Averaging 16 independent readings cuts the noise by $\sqrt{16} = 4$, to 1 code rms.
2. **One code in volts.**
   $$\frac{3.3\ \text{V}}{4095} = 0.806\ \text{mV}$$
3. **The cost.** Averaging costs time: at one reading every 10 µs, 16 take 160 µs.

**Answer:** averaging 16 readings cuts the noise fourfold, to 1 code rms, and one code is 0.806 mV.
:::
:::

::: check
given: V = 0.25 | 1 | 1.65 | 2.7 | 3.1
answer: round(V/3.3*4095)
tolerance: 0

## What code does the 12-bit ADC report for {V} V?

::: right
Right. $V/3.3 \times 4095$, rounded: {answer}.
:::

::: misses
- [any] Not yet. Scale by 4095/3.3 and round.
:::
:::

::: check
given: c = 512 | 1000 | 2048 | 3500
answer: c*3.3/4095
unit: V
tolerance: 0.2%

## The ADC reads {c}. What voltage is that?

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

::: misses
- [any] Not yet. code × 3.3/4095.
:::
:::

::: check
given: V = 0.8 | 1.2 | 2.2 | 2.9; Rf = 10 | 4.7
answer: Rf*(3.3 - V)/V
unit: kΩ
tolerance: 0.5%

## A thermistor from 3.3 V to the pin, with {Rf} kΩ to ground, gives {V} V. What's the thermistor's resistance?

::: right
Right. $R_f(3.3 - V)/V$ = {answer} kΩ.
:::

::: misses
- [Rf*V/(3.3 - V)] That's the divider the other way up: the thermistor is on top here.
- [any] Not yet. $R_T = R_f(3.3 - V)/V$.
:::
:::

::: check
given: R = 3.588 | 6.5 | 15 | 33.62
answer: 1/(1/298.15 + ln(R/10)/3950) - 273.15
unit: °C
tolerance: 0.5%

## A thermistor with R₀ = 10 kΩ at 25 °C and B = 3950 K measures {R} kΩ. What's its temperature?

::: right
Right. The Beta equation gives {answer} °C.
:::

::: misses
- [1/(1/298.15 + log(R/10)/3950) - 273.15] Use the natural logarithm.
- [any] Not yet. $1/T = 1/T_0 + \ln(R/R_0)/B$, in kelvin.
:::
:::

::: check
given: N = 4 | 16 | 64; s = 4 | 12
answer: s/sqrt(N)
tolerance: 0.5%

## Single readings scatter by {s} codes (standard deviation). What's the scatter of an average of {N}?

::: right
Right. $s/\sqrt N$ = {answer} codes.
:::

::: misses
- [s/N] Averaging reduces noise as the square root of the count.
- [any] Not yet. $s/\sqrt N$.
:::
:::

::: check
given: n = 1 | 16 | 64
answer: 1000000/(n*10)
unit: Hz
tolerance: 0.5%

## Each conversion takes 10 µs. If a program averages {n} conversions per reading and does nothing else, how many readings a second can it make?

::: right
Right. {answer} readings per second.
:::

::: misses
- [any] Not yet. One reading is {n} × 10 µs.
:::
:::

::: check
kind: micropython
given: V = 0.4 | 1.5 | 2.6
attach: pot ADC0 = {V} V
run: 0.1
expect: print = round(V/3.3*4095), round(round(V/3.3*4095)*3300/4095)

## A potentiometer on ADC0 sits at some voltage. Print its raw code, then its voltage in millivolts, rounded to a whole number.

```micropython starter
from machine import ADC

pot = ADC(0)
```

```micropython solution
from machine import ADC

pot = ADC(0)
code = pot.read()
print(code)
print(round(code * 3300 / 4095))
```

::: right
Right. The code is the ADC's number; the millivolts come from scaling it.
:::
:::

::: check
kind: c
attach: led P0; thermistor ADC1 | temp 20 + 40*t
run: 0.5
expect: first(P0) = 0.25 s ± 2%; level(P0) = 1

## The thermistor on ADC1 warms steadily from 20 °C to 40 °C over half a second. It reads 2048 at 25 °C and more when warmer; 30 °C is a code of 2270. Keep checking it, and light the LED on P0 once it's above 30 °C.

```c starter
#include "board.h"

int main(void) {
    GPIO_DIR = 1 << 0;
    return 0;
}
```

```c solution
#include "board.h"

int read_adc(int ch) {
    ADC_CTRL = ch | ADC_START;
    while (!(ADC_STATUS & ADC_DONE)) ;
    ADC_STATUS = ADC_DONE;
    return ADC_DATA;
}

int main(void) {
    GPIO_DIR = 1 << 0;
    while (1) {
        if (read_adc(1) > 2270) GPIO_SET = 1 << 0;
        else GPIO_CLR = 1 << 0;
    }
}
```

::: right
Right. Comparing codes avoids converting to degrees at all: the threshold was converted once, in advance.
:::
:::

::: check
kind: parts
given: V = 0.5 | 2.2 | 2.9

## The 12-bit ADC (0 to 4095 for 0 to 3.3 V) measures {V} V.

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

### What code does it give (nearest)?

::: misses
- [round(V/3.3*4096)] The full scale is 4095 codes for 3.3 V here.
- [any] $V \times 4095/3.3$, rounded.
:::
:::

::: part
answer: 3.3/4095*1000
unit: mV
tolerance: 0.5%

### How many millivolts is one code?

::: misses
- [any] 3.3 V/4095.
:::
:::
:::

::: check
kind: parts
given: c = 1000 | 2048 | 3100

## A thermistor from 3.3 V to the pin, with 10 kΩ from the pin to ground, gives code {c}.

::: part
answer: c*3.3/4095
unit: V
tolerance: 0.5%

### What is the pin voltage?

::: misses
- [any] Code × 3.3/4095.
:::
:::

::: part
answer: 10*(3.3 - c*3.3/4095)/(c*3.3/4095)
unit: kΩ
tolerance: 0.5%

### What is the thermistor's resistance?

::: misses
- [any] $10\text{ kΩ} \times (3.3 - V)/V$.
:::
:::
:::

::: explain
## Why can a program compare the ADC's code to a precomputed threshold instead of converting every reading to degrees?

::: model
The code rises steadily with temperature (for this divider, a warmer thermistor always gives a larger code), so "warmer than 30 °C" and "code above the code that 30 °C produces" are the same test. Working out that threshold code once, in advance or at startup, replaces a logarithm and several divisions on every reading with a single integer comparison. That saves time and code, and on a chip without floating-point hardware it avoids slow software floating point entirely; conversion to degrees is only needed when the number has to be shown to a person.
:::
:::

::: practice

::: check
given: V = 0.1 | 0.25 | 0.5 | 1 | 1.2 | 2 | 2.5 | 3
answer: round(V/3.3*4095)
tolerance: 0

## Code for {V} V?

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

::: check
given: V = 0.8 | 1 | 1.2 | 1.5 | 1.65 | 2 | 2.5; Rf = 10
answer: Rf*(3.3 - V)/V
unit: kΩ
tolerance: 0.5%

## Thermistor resistance at {V} V with {Rf} kΩ below?

::: misses
- [any] $R_f(3.3 - V)/V$.
:::
:::

::: check
given: R = 2.2 | 3.3 | 4.7 | 5 | 6.8 | 15 | 20
answer: 1/(1/298.15 + ln(R/10)/3950) - 273.15
unit: °C
tolerance: 0.5%

## Temperature at {R} kΩ (10 kΩ, B 3950)?

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