Display Temperature Sensor Value from Raspberry Pi Pico on an LCD

In this article we connect an Pico Sense hat to our Raspberry Pi Pico

We will then use the on board LCD to display the temperature

Parts Required

I used a Pico sense hat which has 4 sensors

Name Link
Pico Raspberry Pi Pico Development Board

Pico Sense hat Raspberry Pi Pico with Pico Sense HAT Multi Sensor Humidity, Air Quality, Color, Pressure Sensor Sense HAT for Pico with Inbuilt 1.14” LCD Display
Connecting cables Aliexpress product link

Lysee 3D Printer Parts & Accessories – AHT20 Temperature and Humidity Sensor Module DHT11 Upgrade I2C XD Humidity Sensor Probe – (Color: Green)

Ebay link

 

Schematic/Connection

I used a sense hat from sb components which simply plugs into your Pico

 

Code Example

I used Thonny and this example is written in Micropython

The first part is a library for the 1.14″ LCD screen

You need to upload this to your Raspberry Pi Pico

Go to File > Save as…
Select the Raspberry Pi Pico
Name your file as Lcd1_14driver.py and press the OK button
And that’s it. The library was uploaded to your board. To make sure that it was uploaded successfully, go to File > Save as… and select the Raspberry Pi Pico device. Your file should be listed there:
After uploading the library to your board, you can use the library in your code by importing the library.

from machine import Pin,SPI,PWM
import framebuf
import time

DC = 8
RST = 12
MOSI = 11
SCK = 10
CS = 9


class Lcd1_14(framebuf.FrameBuffer):
    def __init__(self):
        self.width = 240
        self.height = 135
        
        self.cs = Pin(CS,Pin.OUT)
        self.rst = Pin(RST,Pin.OUT)
        
        self.cs(1)
        self.spi = SPI(1)
        self.spi = SPI(1,1000_000)
        self.spi = SPI(1,10000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
        self.dc = Pin(DC,Pin.OUT)
        self.dc(1)
        self.buffer = bytearray(self.height * self.width * 2)
        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)  #Red Green Blue (16-bit, 5+6+5) color format
        self.lcd_init_display()
        
        self.red   =   0x07E0
        self.green =   0x001f
        self.blue  =   0xf800
        self.white =   0xffff
        
    def lcd_write_cmd(self, cmd):
        self.cs(1)
        self.dc(0)
        self.cs(0)
        self.spi.write(bytearray([cmd]))
        self.cs(1)

    def lcd_write_data(self, buf):
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(bytearray([buf]))
        self.cs(1)

    def lcd_init_display(self):
        """Initialize dispaly"""  
        self.rst(1)
        self.rst(0)
        self.rst(1)
        
        self.lcd_write_cmd(0x36)
        self.lcd_write_data(0x70)

        self.lcd_write_cmd(0x3A) 
        self.lcd_write_data(0x05)

        self.lcd_write_cmd(0xB2)
        self.lcd_write_data(0x0C)
        self.lcd_write_data(0x0C)
        self.lcd_write_data(0x00)
        self.lcd_write_data(0x33)
        self.lcd_write_data(0x33)

        self.lcd_write_cmd(0xB7)
        self.lcd_write_data(0x35) 

        self.lcd_write_cmd(0xBB)
        self.lcd_write_data(0x19)

        self.lcd_write_cmd(0xC0)
        self.lcd_write_data(0x2C)

        self.lcd_write_cmd(0xC2)
        self.lcd_write_data(0x01)

        self.lcd_write_cmd(0xC3)
        self.lcd_write_data(0x12)   

        self.lcd_write_cmd(0xC4)
        self.lcd_write_data(0x20)

        self.lcd_write_cmd(0xC6)
        self.lcd_write_data(0x0F) 

        self.lcd_write_cmd(0xD0)
        self.lcd_write_data(0xA4)
        self.lcd_write_data(0xA1)

        self.lcd_write_cmd(0xE0)
        self.lcd_write_data(0xD0)
        self.lcd_write_data(0x04)
        self.lcd_write_data(0x0D)
        self.lcd_write_data(0x11)
        self.lcd_write_data(0x13)
        self.lcd_write_data(0x2B)
        self.lcd_write_data(0x3F)
        self.lcd_write_data(0x54)
        self.lcd_write_data(0x4C)
        self.lcd_write_data(0x18)
        self.lcd_write_data(0x0D)
        self.lcd_write_data(0x0B)
        self.lcd_write_data(0x1F)
        self.lcd_write_data(0x23)

        self.lcd_write_cmd(0xE1)
        self.lcd_write_data(0xD0)
        self.lcd_write_data(0x04)
        self.lcd_write_data(0x0C)
        self.lcd_write_data(0x11)
        self.lcd_write_data(0x13)
        self.lcd_write_data(0x2C)
        self.lcd_write_data(0x3F)
        self.lcd_write_data(0x44)
        self.lcd_write_data(0x51)
        self.lcd_write_data(0x2F)
        self.lcd_write_data(0x1F)
        self.lcd_write_data(0x1F)
        self.lcd_write_data(0x20)
        self.lcd_write_data(0x23)
        
        self.lcd_write_cmd(0x21)

        self.lcd_write_cmd(0x11)

        self.lcd_write_cmd(0x29)

    def lcd_show(self):
        self.lcd_write_cmd(0x2A)
        self.lcd_write_data(0x00)
        self.lcd_write_data(0x28)
        self.lcd_write_data(0x01)
        self.lcd_write_data(0x17)
        
        self.lcd_write_cmd(0x2B)
        self.lcd_write_data(0x00)
        self.lcd_write_data(0x35)
        self.lcd_write_data(0x00)
        self.lcd_write_data(0xBB)
        
        self.lcd_write_cmd(0x2C)
        
        self.cs(1)
        self.dc(1)
        self.cs(0)
        self.spi.write(self.buffer)
        self.cs(1

You can download from the bottom of this article

The Raspberry Pi Pico has internal Temperature Sensor connected to one of a few special pins called ADCs or Analog-to-Digital Converters.

The temperature sensor works by delivering a voltage to the ADC4 pin that is proportional to the temperature.

If you look at the datasheet, a temperature of 27 degrees Celsius delivers a voltage of 0.706 V. With each additional degree the voltage reduces by 1.721 mV or 0.001721 V. The first step in converting the 16-bit temperature is to convert it back to a voltage, which is done based on the 3.3 V maximum voltage used by the Pico board.

temperature = 27 – (reading – 0.706)/0.001721

With this conversion, the temperature value holds a value between 0 and 3.3. We again have to do the second conversion which brings the temperature to the Celsius scale.

fahrenheit_degrees = celsius_degrees * 9 / 5 + 32

If you want to convert the temperature value from degree Celcius to Fahrenheit, you can use this mathematical equation.

After uploading the library to the Raspberry Pi Pico, copy the following code to the main.py.

This will print the temperate as a raw value into the shell every 0.2 seconds and also display the value on the LCD

from machine import I2C, Pin, SPI
import Lcd1_14driver#lcd driver
import time

sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)

LCD = Lcd1_14driver.Lcd1_14()
LCD.fill(LCD.white) 


i2c = I2C(1,scl=Pin(7), sda=Pin(6), freq=40000)#all sensor connected through I2C
    
while True:
    reading = sensor_temp.read_u16() * conversion_factor
    temperature = 27 - (reading - 0.706)/0.001721
    print(temperature)
    LCD.text("Pi PICO on-board temperature",5,5,LCD.red)
    LCD.text("TEMPERATURE :",10,65,LCD.red)
    LCD.text(str(temperature), 120,65,LCD.blue)# print on tft screen
    LCD.lcd_show()
    time.sleep(0.2)
    LCD.text(str(temperature), 120,65,LCD.blue)# print on tft screen

Output

Here is what I saw in Thonny REPL window

>>> %Run -c $EDITOR_CONTENT
18.61781
20.95853
20.95853
21.42667

Links

Lcd1_14driver

Related posts

A Raspberry Pi Pico and KY-001 Temperature Sensor

A Raspberry Pi Pico and KY-006 Passive Buzzer

A Raspberry Pi Pico and KY-012 Active Buzzer

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More