This is some demonstration sample code showing use of the LCD1602 as an LCD display for the Raspberry Pi Pico W. The code is explained in the video above. It will prompt a user for his name, and then display a greeting on the LCD.
1 2 3 4 5 6 7 8 9 10 11 | from lcd1602 import LCD import utime as time lcd=LCD() while True: myName=input('What is Your Name? ') lcd.clear() greeting1='Hello '+myName greeting2='Welcome to My Pi' lcd.write(0,0,greeting1) lcd.write(0,1,greeting2) |
Below is the library for the Sunfounder Kepler Kit LCD1602 display. It allows the LCD display to operate with the Raspberry Pi Pico W. The code should be copied and pasted into Thonny, and then saved to your Raspberry Pi Pico W, to the same folder that contains you Python code. It MUST be saved with file name lcd1602.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | import machine import time class LCD(): def __init__(self, addr=None, blen=1): sda = machine.Pin(6) scl = machine.Pin(7) self.bus = machine.I2C(1,sda=sda, scl=scl, freq=400000) #print(self.bus.scan()) self.addr = self.scanAddress(addr) self.blen = blen self.send_command(0x33) # Must initialize to 8-line mode at first time.sleep(0.005) self.send_command(0x32) # Then initialize to 4-line mode time.sleep(0.005) self.send_command(0x28) # 2 Lines & 5*7 dots time.sleep(0.005) self.send_command(0x0C) # Enable display without cursor time.sleep(0.005) self.send_command(0x01) # Clear Screen self.bus.writeto(self.addr, bytearray([0x08])) def scanAddress(self, addr): devices = self.bus.scan() if len(devices) == 0: raise Exception("No LCD found") if addr is not None: if addr in devices: return addr else: raise Exception(f"LCD at 0x{addr:2X} not found") elif 0x27 in devices: return 0x27 elif 0x3F in devices: return 0x3F else: raise Exception("No LCD found") def write_word(self, data): temp = data if self.blen == 1: temp |= 0x08 else: temp &= 0xF7 self.bus.writeto(self.addr, bytearray([temp])) def send_command(self, cmd): # Send bit7-4 firstly buf = cmd & 0xF0 buf |= 0x04 # RS = 0, RW = 0, EN = 1 self.write_word(buf) time.sleep(0.002) buf &= 0xFB # Make EN = 0 self.write_word(buf) # Send bit3-0 secondly buf = (cmd & 0x0F) << 4 buf |= 0x04 # RS = 0, RW = 0, EN = 1 self.write_word(buf) time.sleep(0.002) buf &= 0xFB # Make EN = 0 self.write_word(buf) def send_data(self, data): # Send bit7-4 firstly buf = data & 0xF0 buf |= 0x05 # RS = 1, RW = 0, EN = 1 self.write_word(buf) time.sleep(0.002) buf &= 0xFB # Make EN = 0 self.write_word(buf) # Send bit3-0 secondly buf = (data & 0x0F) << 4 buf |= 0x05 # RS = 1, RW = 0, EN = 1 self.write_word(buf) time.sleep(0.002) buf &= 0xFB # Make EN = 0 self.write_word(buf) def clear(self): self.send_command(0x01) # Clear Screen def openlight(self): # Enable the backlight self.bus.writeto(self.addr,bytearray([0x08])) # self.bus.close() def write(self, x, y, str): if x < 0: x = 0 if x > 15: x = 15 if y < 0: y = 0 if y > 1: y = 1 # Move cursor addr = 0x80 + 0x40 * y + x self.send_command(addr) for chr in str: self.send_data(ord(chr)) def message(self, text): #print("message: %s"%text) for char in text: if char == '\n': self.send_command(0xC0) # next line else: self.send_data(ord(char)) |