from machine import Pin, I2C
import time
import lcd_api # Library for LCD API
import pico_i2c_lcd # Library for I2C LCD control on Pico
# I2C setup for LCD
I2C_ADDR = 0x27 # I2C address of the LCD (change if different)
I2C_NUM_ROWS = 2 # Number of rows on the LCD
I2C_NUM_COLS = 16 # Number of columns on the LCD
# Define ADC pin for temperature sensor
adcpin = 4
sensor = machine.ADC(adcpin)
# Function to read temperature from the sensor
def ReadTemperature():
adc_value = sensor.read_u16() # Read raw ADC value
volt = (3.3 / 65535) * adc_value # Convert ADC value to voltage
temperature = 27 - (volt - 0.706) / 0.001721 # Convert voltage to temperature (Celsius)
return round(temperature, 1) # Round to 1 decimal place
# Initialize I2C communication (I2C0, SDA on GP4, SCL on GP5, 400kHz frequency)
i2c = I2C(0, sda=Pin(4), scl=Pin(1), freq=400000)
# Create LCD object using the I2C connection and LCD parameters
lcd = pico_i2c_lcd.I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
# Read temperature from the sensor
temperature = ReadTemperature()
# Define heart shape (5x8 pixel matrix)
heart = [
0b00000, # .....
0b01010, # .X.X.
0b11111, # XXXXX
0b11111, # XXXXX
0b01110, # .XXX.
0b00100, # ..X..
0b00000, # .....
0b00000 # .....
]
# Load custom character into CGRAM at position 0 -> "x00" where the heart shape is stored
lcd.custom_char(0, heart) # stores the heart at CGRAM position 0 (x00)
# Print to the LCD display (uses custom character)
lcd.putstr("I \x00 Pico\n") # Displays "I ♥ Pico"
#time.sleep(2) # Wait for 2 seconds
#lcd.clear() # Clear the LCD
# Display temperature with degree symbol on LCD
lcd.putstr(f"Temp: {temperature} " + chr(223) + "C")
# Print to the console (this is what should appear in the LCD Display)
print("I ♥ Pico")
print(f"Temp: {temperature} °C")