MicroPython thermometer: a thermistor on GPIO 36, on an ESP32-DevKitC V4
MicroPython on an ESP32: a 10k NTC thermistor in a divider on GPIO 36, turned into degrees Celsius with the beta equation every second. The whole build, an ESP32-DevKitC V4 and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $10.20. On Mokxi they're free.
What you'll learn
- How a voltage divider works
- NTC thermistors and temperature
- Reading an analog pin in MicroPython
- Converting resistance to temperature with the beta equation
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: ESP32-DevKitC V4
- Parts: 4
How it works
A 10k NTC thermistor is a resistor whose value drops as it warms up. Here it sits in a voltage divider with a fixed 10k resistor from 3.3 V. GPIO 36 reads the middle of the divider. At room temperature that is near 1.65 V.
The program reads the ADC once a second and turns the reading into volts. It then works out the thermistor's resistance from the divider formula, and uses the beta equation (B = 3950, 10k at 25 C) to get a temperature. It prints the raw reading, the volts and the Celsius value.
Parts list
4 parts, plus the jumper wires. They retail for about $10.20; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Thermistor, 10k NTC
- 1 × Power, 3.3 V
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 10k Ω pin 2 | ESP32-DevKitC V4 pin VP |
| Thermistor, 10k NTC pin 1 | ESP32-DevKitC V4 pin VP |
| Resistor, 10k Ω pin 1 | 3.3 V |
| Thermistor, 10k NTC pin 2 | Ground |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Thermometer: a 10k NTC thermistor under a 10k from 3.3 V, read on
# GPIO 36. Its resistance gives the temperature by the beta equation
# (B = 3950, 10k at 25 C).
from machine import ADC, Pin
import math
import time
adc = ADC(Pin(36))
# 6 dB: the ADC's straight range, 0 to about 2.2 V. At 11 dB an ESP32's raw
# reading bends near the top, which would put the temperature off by a few
# degrees. At room temperature this divider sits near 1.65 V.
adc.atten(ADC.ATTN_6DB)
adc.width(ADC.WIDTH_12BIT) # 0 to 4095
while True:
value = adc.read()
volts = value / 4095 * 2.2
ohms = 10000 * volts / (3.3 - volts) # the thermistor
kelvin = 1 / (1 / (273.15 + 25) + math.log(ohms / 10000) / 3950)
print("ADC %d, %.2f V, %.1f C" % (value, volts, kelvin - 273.15))
time.sleep(1)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The temperature is off by a few degrees This project
The code sets the ADC to 6 dB so its readings stay straight. Keep that setting. - Readings are far from room temperature This project
Check the divider: 3.3 V, then the 10k resistor, then GPIO 36 and the thermistor, then ground. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The temperature reads far off or jumps around. Thermistor
Use a fixed resistor equal to the thermistor's 10k at 25 C in the divider. Convert with the Steinhart-Hart or Beta equation (Beta is about 3950 for common parts), not a straight line. - The reading goes the wrong way, or self-heating shifts it. Thermistor
An NTC lowers its resistance as it warms, so the divider voltage moves with the wiring side. Check which side the thermistor is on. Keep current small, with 3.3 V or 5 V through 10k, so it does not self-heat. - The board is not found, or upload fails with a connecting message. ESP32-DevKitC V4
Use a USB cable that carries data, not charge only. Pick the ESP32 Dev Module board. If it still fails, hold BOOT while uploading starts, then release. - Some pins never read right, or the board will not boot with a wire attached. ESP32-DevKitC V4
GPIO34 to 39 are input only and have no pull-ups. GPIO6 to 11 run the flash, so leave them alone. GPIO0, 2 and 12 are boot pins; avoid pulling them the wrong way at reset.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.