LCD 1602 - Library

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
RajaRamesh
Posts: 51
Joined: Wed May 08, 2019 10:54 am

Re: LCD 1602 - Library

Post by RajaRamesh » Sat Jun 15, 2019 12:30 pm

RajaRamesh wrote:
Wed Jun 12, 2019 4:28 am
dhylands wrote:
Tue Jun 11, 2019 5:23 pm
I recorded a video here: https://www.youtube.com/watch?v=KiR-r8mXBCo
Thank you dhylands...i will try and get back to you if have any questions.
Hi dhylands, i am trying below code to display text with GpioLcd. but text was not displaying on lcd screen so i stopped going further. do i need to change the pins in below code or do i need to change slc and sda pins. currently i am running below code after connecting slc =Pin(5) sda=Pin(4) from i2c to esp8266.

i used lcd.clear() to clear the screen but it is not happening.

from machine import Pin
from nodemcu_gpio_lcd import GpioLcd
lcd = GpioLcd(rs_pin=Pin(16),enable_pin=Pin(5),d4_pin=Pin(4),d5_pin=Pin(0),d6_pin=Pin(2),d7_pin=Pin(14),num_lines=2, num_columns=16)
lcd.putstr("Hello")

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: LCD 1602 - Library

Post by dhylands » Sat Jun 15, 2019 4:04 pm

You need to use the i2c version and just extract the code that does the scrolling. The GPIO version doesn't work with LCDs that have i2c backpacks.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: LCD 1602 - Library

Post by dhylands » Sat Jun 15, 2019 4:07 pm

In particular, this code:

Code: Select all

    msg = 'This is a scrolling test. '
    if (len(msg) < lcd.num_columns):
        msg += ' ' * (lcd.num_columns - len(msg))
    for i in range(len(msg)):
        len2 = min(lcd.num_columns, len(msg) - i)
        len1 = lcd.num_columns - len2
        msg2 = ''
        if len2 > 0:
            msg2 += msg[i:i+len2]
        if len1 > 0:
            msg2 += msg[0:len1]
        lcd.move_to(0, 0)
        lcd.putstr(msg2)
        delay(250)

User avatar
vdb_peter
Posts: 8
Joined: Mon Feb 11, 2019 12:51 am
Location: Melbourne

Re: LCD 1602 - Library

Post by vdb_peter » Thu Jun 27, 2019 3:41 am

Hi Dave- 1st of all, thanks for your great work here- got it working on my 8266 & 1602 (with level shifter) without any problems at all.

Can you explain the syntax of putstr(), please? I'm tripping up on the syntax you use in your Test script: lcd.putstr("%7d" % (ticks_ms() // 1000)). Please explain the use of "%". If I substitute the "(ticks_ms() // 1000))" with a variable that holds a number, it prints, but I don't understand what the ""%7d" %" means. (And I couldn't sufficiently decipher lcd_api.py to work it out myself, sorry).

Thanks

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: LCD 1602 - Library

Post by mcauser » Thu Jun 27, 2019 3:48 am

This one explains the string formatting: https://pyformat.info/

Code: Select all

lcd.putstr("%7d" % (ticks_ms() // 1000))
can also be written as:

Code: Select all

lcd.putstr("{:7d}".format(ticks_ms() // 1000))
%7d means convert the int to a left space padded string 7 characters wide.
ie.

Code: Select all

123 -> "    123"
12345 -> "  12345"
Last edited by mcauser on Thu Jun 27, 2019 5:41 am, edited 1 time in total.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: LCD 1602 - Library

Post by dhylands » Thu Jun 27, 2019 5:06 am

Just a couple minor corrections:

That would be '{:7d}'.format(args) - Use a colon and not a percent with the 7d.

Code: Select all

>>> '{:7d}'.format(123)
'    123'
And '%7d' % 123 produces space padding on the left.

Code: Select all

>>> '%7d' % 123
'    123'

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: LCD 1602 - Library

Post by mcauser » Thu Jun 27, 2019 5:43 am

Ooops, thanks Dave!

RajaRamesh
Posts: 51
Joined: Wed May 08, 2019 10:54 am

Re: LCD 1602 - Library

Post by RajaRamesh » Mon Jul 01, 2019 7:57 am

dhylands wrote:
Sat Jun 15, 2019 4:07 pm
In particular, this code:

Code: Select all

    msg = 'This is a scrolling test. '
    if (len(msg) < lcd.num_columns):
        msg += ' ' * (lcd.num_columns - len(msg))
    for i in range(len(msg)):
        len2 = min(lcd.num_columns, len(msg) - i)
        len1 = lcd.num_columns - len2
        msg2 = ''
        if len2 > 0:
            msg2 += msg[i:i+len2]
        if len1 > 0:
            msg2 += msg[0:len1]
        lcd.move_to(0, 0)
        lcd.putstr(msg2)
        delay(250)
Hi Dave, i have updated i2c version with above code with small correction in for loop. i am getting error when for loop is outside of if statement.so i have mentioned the for loop inside if statement and the text is scrolling on LCD screen in 1st row. i have updated lcd.putstr as lcd.putstr(msg2\nmsg2) to scroll the text on 1st and 2nd row. but it was not working. do we need to have an if statement on lcd.row_num to scroll on 2nd row?

vitalis
Posts: 8
Joined: Fri Nov 29, 2019 6:04 am
Location: Finland

Re: LCD 1602 - Library

Post by vitalis » Sat Nov 30, 2019 7:37 pm

I just got my I2C 20x4 LCD working with esp32. I've used level shifter to adjust 3.3v and 5 v between devices.
A question: is it doable to redirect Micropython REPL to 20x4 LCD?

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: LCD 1602 - Library

Post by Christian Walther » Sat Nov 30, 2019 9:17 pm

Apparently yes, see viewtopic.php?t=7083&p=40307

Post Reply