Displaying the Raspberry Pi Pico’s on-board temperature on an SPI LCD

I got my new Raspberry Pi Pico, and I’ve been very impressed by it. I can see it easily becoming my go-to board. The Pico has an on-board temperature sensor. I thought it would be a great idea to display the reading on an LCD with a SPI interface.

The code is pretty straightforward and self-explanatory:

# Display on-board temperature on a SPI LCD

import struct
import machine
import utime

def get_temp():
    sensor_temp = machine.ADC(4)
    conversion_factor = 3.3 / (65535)
    reading = sensor_temp.read_u16() * conversion_factor
    temperature = 27 - (reading - 0.706)/0.001721
    print(temperature)
    return temperature

spi_miso=machine.Pin(16) # physical pin 21
spi_sck=machine.Pin(18) # physical pin 24
spi_mosi=machine.Pin(19) # physical pin 25

rs = machine.Pin(20, machine.Pin.OUT) # physical pin 26
cs = machine.Pin(17, machine.Pin.OUT) # physical pin 22
cs.value(1)

def digitalWrite(pin, val): pin.value(1 if val else 0 )

LOW = 0
HIGH = 1


spi=machine.SPI(0,baudrate=100000,sck=spi_sck, mosi=spi_mosi, miso=spi_miso)

def sendByte(rs_val, val):
    digitalWrite(rs, rs_val);
    digitalWrite(cs, LOW);
    #spi_write_blocking(SPI_PORT, &val, 1);
    #spi.write(struct.pack('b', val))
    spi.write(struct.pack('B', val))
    digitalWrite(cs, HIGH);  
    utime.sleep(0.060);
    
contrast = 0x70  | 0b1000 #; // from 0x7C
display = 0b1111 #; // ori 0x0F
cmds = [0x39, 0x1D, 0x50, 0x6C, contrast , 0x38, display, 0x01, 0x06]
for c in cmds:
    sendByte(0, c)

msg = ["H", "E", "L", "L", "O"]
#msg = b'HELLO'
def sendStr(msg):
    for b in msg:  sendByte(1, ord(b))
    

def once():
    sendByte(0, 0x01) # clear display
    txt = "{deg:.1f}C    ".format(deg=get_temp())
    sendStr(txt)
    utime.sleep(5)
    
once()    

while True: once()
    

The code is available here.

Happy hacking!

About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to Displaying the Raspberry Pi Pico’s on-board temperature on an SPI LCD

  1. LA says:

    Hi, nice.
    I realy like the R(Pi)2co
    I would check the real chip voltage, mine is lower then 3,3V. I use 3,15 as reference voltage.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s