Pico W room temperature on an I2C LCD
MicroPython on a Pico W: a 10k thermistor divider on GP28 gives the temperature, and an I2C LCD1602 on GP6 and GP7 shows it every second. The whole build, a Raspberry Pi Pico W and 3 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
- Voltage dividers with a thermistor
- Converting resistance to temperature with the Beta equation
- Reading an analog pin on the Pico W
- Showing text on an I2C LCD
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Raspberry Pi Pico W
- Parts: 6
How it works
A 10k NTC thermistor and a 10k resistor form a voltage divider on 3.3 V. The resistor is on the 3.3 V side and the thermistor goes to ground. The Pico W reads the middle point on GP28. As the room warms, the thermistor's resistance drops, and so does the voltage.
Every second, the program turns that voltage back into resistance and then into degrees Celsius. It shows the result on the second line of a 16x2 LCD under the words Room temperature, and prints it to the console too. The LCD talks over I2C on GP6 and GP7.
Parts list
6 parts, plus the jumper wires. They retail for about $10.20; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × Character LCD with I2C backpackIn Claw Machine Kit, Vending Machine Kit, Rhythm Game Kit.
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Thermistor, 10k NTC
- 1 × Power, 5 V
- 1 × Power, 3.3 V
Kits that can build it
These 2 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 |
|---|---|
| Character LCD with I2C backpack pin SDA | Raspberry Pi Pico W pin GP6 |
| Character LCD with I2C backpack pin SCL | Raspberry Pi Pico W pin GP7 |
| Resistor, 10k Ω pin 2 | Raspberry Pi Pico W pin GP28 |
| Thermistor, 10k NTC pin 1 | Raspberry Pi Pico W pin GP28 |
| Character LCD with I2C backpack pin GND | Ground |
| Character LCD with I2C backpack pin VCC | 5 V |
| 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.
# Room temperature meter: the thermistor divider on GP28 gives the
# temperature, and the LCD1602 on I2C1 (SDA GP6, SCL GP7) shows it.
from machine import ADC, I2C, Pin
from lcd1602 import LCD1602
import math
import time
sensor = ADC(28)
lcd = LCD1602(I2C(1, sda=Pin(6), scl=Pin(7), freq=400000))
def celsius():
v = sensor.read_u16() / 65535 * 3.3
r = 10000 * v / (3.3 - v)
return 1 / (1 / 298.15 + math.log(r / 10000) / 3950) - 273.15
while True:
c = celsius()
lcd.show("Room temperature", "%.1f C" % c)
print("%.1f C" % c)
time.sleep(1)
# lcd1602.py: a 16x2 character LCD on a PCF8574 I2C backpack.
# The backpack's P0 is RS, P2 is E, P3 is the backlight and P4 to P7 are
# the display's D4 to D7, so every byte is sent as two 4-bit halves.
import time
class LCD1602:
def __init__(self, i2c, addr=0x27):
self.i2c = i2c
self.addr = addr
self.light = 0x08
time.sleep_ms(50)
for half in (0x30, 0x30, 0x30, 0x20): # wake up, then 4-bit mode
self._half(half, 0)
time.sleep_ms(5)
self.command(0x28) # two lines, 5x8 dots
self.command(0x0C) # display on, no cursor
self.command(0x06) # move right after a character
self.clear()
def _send(self, bits):
self.i2c.writeto(self.addr, bytes([bits | self.light]))
def _half(self, bits, rs):
self._send(bits | rs | 0x04) # E high...
self._send(bits | rs) # ...and low: the LCD reads it
def _byte(self, value, rs):
self._half(value & 0xF0, rs)
self._half((value << 4) & 0xF0, rs)
def command(self, value):
self._byte(value, 0)
if value < 4:
time.sleep_ms(2) # clear and home are slow
def clear(self):
self.command(0x01)
def move_to(self, col, row):
self.command(0x80 | (col + 0x40 * row))
def write(self, text):
for ch in text:
self._byte(ord(ch), 1)
def show(self, line1, line2=""):
self.move_to(0, 0)
self.write("%-16s" % line1[:16])
self.move_to(0, 1)
self.write("%-16s" % line2[:16])
Code walkthrough
The key functions of the sketch, and what each one does.
celsius main.py
First it reads the ADC as a 16-bit number and scales it to a voltage between 0 and 3.3. Then it works out the thermistor's resistance from the divider formula with the 10000 ohm resistor. Last, the Beta equation (3950, with 25 degrees C as 298.15 K for 10k) turns resistance into kelvin, and subtracting 273.15 gives Celsius.
def celsius():
v = sensor.read_u16() / 65535 * 3.3
r = 10000 * v / (3.3 - v)
return 1 / (1 / 298.15 + math.log(r / 10000) / 3950) - 273.15Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The temperature is far off, or reads absurdly high or low. This project
Check the divider: the 10k resistor goes to 3.3 V, the thermistor to ground, and GP28 to the point where they meet. - The LCD stays blank. This project
Check SDA goes to GP6 and SCL to GP7, and that the backpack's VCC gets 5 V and GND is grounded. - The screen stays blank or shows only a row of blocks. Character LCD with I2C backpack
The I2C address is usually 0x27 or 0x3F. Run an I2C scanner to find yours. Then turn the contrast trimmer on the backpack slowly until text appears. - The backlight is on but there is no text, or text is garbled. Character LCD with I2C backpack
Wire SDA and SCL to the right pins (A4 and A5 on an Uno), power it from 5 V, and call lcd.init() and the backlight call in setup. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.