Waveshare 1602 LCD

I have a raspberry pi pico, using thonny 3.1.4 and i have a LCD 16x2 screen. White I2C 16x02 LCD Display (White on Blue, 3/5V) | Waveshare | Core Electronics Australia

for the life of me i cannot get this thing to work. any tips. what library etc

Thanks alot
Ben

1 Like

Hi Ben,

Just before jumping into code, would it be possible to send through a photo of how you have everything connected?

For micropython:
Down the bottom of the Waveshare Wiki there is a demo file, download that and move the LCD1602.py file to the Pico, then you’ll be able to run the test.py file to get some text to display.

If you hit any snags let us know and we can get back to you!
Liam

I have a RGB one of these working with a Pico, cannot find where I got the library as the current links don’t work anymore. I modified the library from the original to get it to work, unsure why.
Waveshare can be hit and miss sometimes.

Regards
Jim

EDIT: Use the file from the Demo link, @Liam mentioned. The problem with the library is the I2C pins are defined in the library, they should be passed to the library. The code below shows how to pass the I2C pin definitions. As you have a non RGB version use the library from the Demo link and change it if you don’t want to use pins 4 & 5.

EDIT 2: Found where I got the file from. Needed to search for LCD1602 RGB rather than RGB1602.

Library: Save as RGB1602_01.py

# -*- coding: utf-8 -*-
import time
from machine import Pin,I2C

#Device I2C Arress
LCD_ADDRESS   =  (0x7c>>1)
RGB_ADDRESS   =  (0xc0>>1)

#color define

REG_RED    =     0x04
REG_GREEN  =     0x03
REG_BLUE   =     0x02
REG_MODE1  =     0x00
REG_MODE2  =     0x01
REG_OUTPUT =     0x08
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80

#flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00

#flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00

#flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00

#flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x8DOTS = 0x00

class RGB1602():
    def __init__(self,  col=16, row=1, i2c=None): 
        if isinstance(i2c, I2C) is False:
            print("RGB1602 requires a valid i2c device")
            raise TypeError
        self.RGB1602_I2C = i2c
        
        self._row = row
        self._col = col

        self._showfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
        self.begin(self._row,self._col)

        
    def command(self,cmd):
        self.RGB1602_I2C.writeto_mem(LCD_ADDRESS, 0x80, chr(cmd))

    def write(self,data):
        self.RGB1602_I2C.writeto_mem(LCD_ADDRESS, 0x40, chr(data))
    
    def setReg(self,reg,data):
        self.RGB1602_I2C.writeto_mem(RGB_ADDRESS, reg, chr(data))


    def setRGB(self,r,g,b):
        self.setReg(REG_RED,r)
        self.setReg(REG_GREEN,g)
        self.setReg(REG_BLUE,b)

    def setCursor(self,col,row):
        if(row == 0):
            col|=0x80
        else:
            col|=0xc0;
        self.RGB1602_I2C.writeto(LCD_ADDRESS, bytearray([0x80,col]))

    def clear(self):
        self.command(LCD_CLEARDISPLAY)
        time.sleep(0.002)
        
    def blank(self):
        self.setRGB(0,0,0);

    def printout(self,arg):
        if(isinstance(arg,int)):
            arg=str(arg)

        for x in bytearray(arg,'utf-8'):
            self.write(x)

    def display(self):
        self._showcontrol |= LCD_DISPLAYON 
        self.command(LCD_DISPLAYCONTROL | self._showcontrol)
 
    def begin(self,cols,lines):
        if (lines > 1):
            self._showfunction |= LCD_2LINE 
        self._numlines = lines 
        self._currline = 0 
        time.sleep(0.05)

# Send function set command sequence
        self.command(LCD_FUNCTIONSET | self._showfunction)
#delayMicroseconds(4500);  # wait more than 4.1ms
        time.sleep(0.005)
# second try
        self.command(LCD_FUNCTIONSET | self._showfunction);
#delayMicroseconds(150);
        time.sleep(0.005)
# third go
        self.command(LCD_FUNCTIONSET | self._showfunction)
# finally, set # lines, font size, etc.
        self.command(LCD_FUNCTIONSET | self._showfunction)
# turn the display on with no cursor or blinking default
        self._showcontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF 
        self.display()
# clear it off
        self.clear()
# Initialize to default text direction (for romance languages)
        self._showmode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT 
# set the entry mode
        self.command(LCD_ENTRYMODESET | self._showmode);
# backlight init
        self.setReg(REG_MODE1, 0)
# set LEDs controllable by both PWM and GRPPWM registers
        self.setReg(REG_OUTPUT, 0xFF)
# set MODE2 values
# 0010 0000 -> 0x20  (DMBLNK to 1, ie blinky mode)
        self.setReg(REG_MODE2, 0x20)
        self.setColorWhite()

    def setColorWhite(self):
        self.setRGB(255, 255, 255)
    def setColorRed(self):
        self.setRGB(255,0,0)
    def setColorGreen(self):
        self.setRGB(0,255,0)
    def setColorBlue(self):
        self.setRGB(0,0,255)

Example:

from machine import I2C, Pin, RTC
import RGB1602_01
i2c = I2C(id=0, sda=Pin(8), scl=Pin(9), freq=400000)
lcd=RGB1602_01.RGB1602(16,2,i2c=i2c)

lcd.setColorWhite()
lcd.clear()
lcd.setCursor(0, 0)
lcd.printout('Hello')
lcd.setCursor(0, 1)
lcd.printout('World')
1 Like

Thanks a lot for your reply’s I’ll give that a go.
Legend :ok_hand:

Hi James
I too have a Waveshare 1602 display with all the fancy colour changing backlight but could never get it to work properly. Sometimes it would display “Welcome” on the top line and gibberish on the second.

I was using Arduino UNO R3 and wave share Arduino demos.

I got the impression that it was trying to print the wrong ASC codes so I tried printing using ASCI code numbers with no joy. I think from memory I tried 2 different codes with no luck.

Someone suggested I try the DFRobot library but I put it aside in the too hard basket and have not got around to it yet. One day soon perhaps.
Cheers Bob

Probably because they go to market before proper trials just to beat the opposition.
It is the only Waveshare device I have and will remain so until they get sorted. Too much trouble evident on this forum for this little black duck.

1 Like

I have a spare Waveshare 1602; will hook it up with a UNO R3 and try the Arduino demos.
Post here my results. Might find a problem or two.

Regards
Jim

EDIT: Tested this quicker than I thought I would.
Wiring: Note the colour code is different on the cable that came with the display.
VCC = Green, GND = Purple.

Downloaded the file below.

Unzipped the file.
Created a Arduino IDE sketch called LCD_RGB_16x2
Copied the unzipped files to the folder created by the Arduino IDE.

Waveshare_LCD1602_RGB.h
Waveshare_LCD1602_RGB.cpp

Restarted Arduino IDE.
Copied the contents of the example Direction file to the sketch file LCD_RGB_16x2 in Arduino IDE
Compiled and downloaded the sketch to a UNO R3.
It ran as expected.

Hope you can get it going
Cheers
Jim

Hi Jim
Thanks for that. I don’t recollect having “Waveshare_LCD1602_RGB.cpp” anywhere. Will have to look into it. Will print your reply if you don’t mind to save looking for it after.
Cheers Bob

1 Like