C12 · Lesson 16 · Reading sensors with the ADC
From a voltage to a number to a temperature
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.
- 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
celsiusand match it to the formulas below.
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?
Rise. The thermistor is on top, from 3.3 V to the pin, and the fixed resistor below, so : a smaller 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 ( = 3588 Ω) it's 3014.
Name it
Codes and volts
This board's ADC is 12 bits over 0 to 3.3 V:
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 from 3.3 V to the pin and a fixed to ground give
and the Beta equation turns resistance into temperature (in kelvin):
with the resistance at = 298.15 K (25 °C). Here = 10 kΩ and B = 3950 K.
Noise
Averaging N readings reduces random noise by (C11). Reading many samples and averaging is the simplest filter in firmware.
Worked example
ADC1 reads 2482. What's the voltage, the thermistor's resistance, and the temperature?
Show the solution
- Code to volts.
- Volts to resistance.
- Resistance to temperature. Put in the Beta equation:
- Invert it.
Answer 2.000 V, 6.50 kΩ and 35.0 °C.
Worked example
What code does 1.0 V give, and how much does one step of the code represent?
Show the solution
- Scale the voltage to a code. so 1241.
- One step.
Answer 1.0 V gives code 1241, and one step is 0.806 mV.
Worked example
An ADC reading has 4 codes of rms noise. How much does averaging 16 readings help, and what is one code in volts?
Show the solution
- Average 16 readings. Averaging 16 independent readings cuts the noise by , to 1 code rms.
- One code in volts.
- 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
What code does the 12-bit ADC report for 1 V?
Check
The ADC reads 1000. What voltage is that?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
A thermistor from 3.3 V to the pin, with 10 kΩ to ground, gives 1.2 V. What's the thermistor's resistance?
Write units the way you normally would: 1.5 kΩ, 1500 Ω and 0.0015 MΩ are the same answer.
Check
A thermistor with R₀ = 10 kΩ at 25 °C and B = 3950 K measures 6.5 kΩ. What's its temperature?
Check
Single readings scatter by 4 codes (standard deviation). What's the scatter of an average of 16?
Check
Each conversion takes 10 µs. If a program averages 16 conversions per reading and does nothing else, how many readings a second can it make?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
A potentiometer on ADC0 sits at some voltage. Print its raw code, then its voltage in millivolts, rounded to a whole number.
MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.
Check
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 on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
The 12-bit ADC (0 to 4095 for 0 to 3.3 V) measures 2.2 V.
(a)
What code does it give (nearest)?
(b)
How many millivolts is one code?
Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.
Check
A thermistor from 3.3 V to the pin, with 10 kΩ from the pin to ground, gives code 2048.
(a)
What is the pin voltage?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
(b)
What is the thermistor's resistance?
Explain why
Why can a program compare the ADC's code to a precomputed threshold instead of converting every reading to degrees?
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
Keep practicing
12 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.