Python driving 44780 LCD


91 posts   Page 1 of 4   1, 2, 3, 4
by texy » Sun Jun 10, 2012 12:39 pm
Hi,
with thanks to information in this thread :
viewtopic.php?f=64&t=4934
I have been able to get a LCD module to display messages using python directly from the GPIO
The code is not very clever, and also not very fast - I,m sure that some of you python experts can improve upon it, but it is clear (to me, at least!), and working !
Image

Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#       gpio_play6.py
# Texy 2012

import RPi.GPIO as GPIO
import time

def main():

    GPIO.setmode(GPIO.BCM) # use BCM I/O names
    GPIO.setup(24, GPIO.OUT) # E
    GPIO.setup(4, GPIO.OUT) # RS
    GPIO.setup(18, GPIO.OUT) # DB4
    GPIO.setup(21, GPIO.OUT) # DB5
    GPIO.setup(22, GPIO.OUT) # DB6
    GPIO.setup(23, GPIO.OUT) # DB7
    GPIO.setup(17, GPIO.OUT) # LED
    lcd_cmd(0x33) # $33 8-bit mode   
    lcd_cmd(0x32) # $32 8-bit mode   
    lcd_cmd(0x28) # $28 8-bit mode   
    lcd_cmd(0x0C) # $0C 8-bit mode   
    lcd_cmd(0x06) # $06 8-bit mode   
    lcd_cmd(0x01) # $01 8-bit mode   
    lcd_string("Hello Pi")
    lcd_cmd(0xC0) # next line
    lcd_string("Forum peeps!")


def lcd_cmd(bits):
   bits=bin(bits)
   bits=bits[2:]
   zeros=(8-len(bits))*"0"
   bits=zeros+bits
   GPIO.output(4, False) # RS low
   GPIO.output(18, False)
   GPIO.output(21, False)
   GPIO.output(22, False)
   GPIO.output(23, False)
   if bits[0]=="1" :
      GPIO.output(23, True)
   if bits[1]=="1" :
      GPIO.output(22, True)
   if bits[2]=="1" :
      GPIO.output(21, True)
   if bits[3]=="1" :
      GPIO.output(18, True)
   time.sleep(0.01)
   GPIO.output(24, True) # E high
   time.sleep(0.01)
   GPIO.output(24, False) # E low
   time.sleep(0.01)
   GPIO.output(18, False)
   GPIO.output(21, False)
   GPIO.output(22, False)
   GPIO.output(23, False)
   if bits[4]=="1" :
      GPIO.output(23, True)
   if bits[5]=="1" :
      GPIO.output(22, True)
   if bits[6]=="1" :
      GPIO.output(21, True)
   if bits[7]=="1" :
      GPIO.output(18, True)
   time.sleep(0.01)
   GPIO.output(24, True) # E high
   time.sleep(0.01)
   GPIO.output(24, False) # E low
   time.sleep(0.01)

def lcd_string(message):
   msg_len = len(message)
   for i in range(msg_len):
      lcd_char(message[i])
      
      
      


def lcd_char(bits):
   bits=bin(ord(bits))
   bits=bits[2:]
   zeros=(8-len(bits))*"0"
   bits=zeros+bits
   GPIO.output(4, True) # RS high
   GPIO.output(18, False)
   GPIO.output(21, False)
   GPIO.output(22, False)
   GPIO.output(23, False)
   if bits[0]=="1" :
      GPIO.output(23, True)
   if bits[1]=="1" :
      GPIO.output(22, True)
   if bits[2]=="1" :
      GPIO.output(21, True)
   if bits[3]=="1" :
      GPIO.output(18, True)
   time.sleep(0.01)
   GPIO.output(24, True) # E high
   time.sleep(0.01)
   GPIO.output(24, False) # E low
   time.sleep(0.01)
   GPIO.output(18, False)
   GPIO.output(21, False)
   GPIO.output(22, False)
   GPIO.output(23, False)
   if bits[4]=="1" :
      GPIO.output(23, True)
   if bits[5]=="1" :
      GPIO.output(22, True)
   if bits[6]=="1" :
      GPIO.output(21, True)
   if bits[7]=="1" :
      GPIO.output(18, True)
   time.sleep(0.01)
   GPIO.output(24, True) # E high
   time.sleep(0.01)
   GPIO.output(24, False) # E low
   time.sleep(0.01)

if __name__ == '__main__':
   main()


Please free feel to criticize or suggest improvements.
Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2458
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by sharpapotheosis » Sun Jun 10, 2012 1:54 pm
Instead of having the if elif statements, you could use an array to contain the pins, and cycle through it using a for loop: for example:
Code: Select all
bits = bin(bits)[2:]
pin_numbers = [23,22,21,18]
for i in range(4):
    GPIO.output(pin_numbers[i], bool(bit[i])


I don't know if that's helpful, but it's something I would do ;)
Posts: 58
Joined: Thu May 24, 2012 6:47 pm
by texy » Sun Jun 10, 2012 2:05 pm
That looks like it would save a lot of time and code! I,d still need to sort out leading '0's though would'nt I?

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2458
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by sharpapotheosis » Sun Jun 10, 2012 4:59 pm
Yes, sorry, it was quickly typed :P

Yeah, it needs a 4-bit (or in this chase 4 character) string to process, so do add the leading 0s.

With this method, I don't think you need to reset the pins to false either. As in, all you need to do is set the RS pin, place your data on the data pins and flash the E pin. You can then move on to the next command: there's no need to reset everything.

Also, as an aside, can the LCD have the E pin held for less than 10ms? It probably depends on the particular LCD you're using.
Posts: 58
Joined: Thu May 24, 2012 6:47 pm
by texy » Sun Jun 10, 2012 5:40 pm
Thanks for the confirmation. I have used these LCD for some years now, mainly with picaxe. I usually only toggle E for 1ms without problems. The biggest hurdle is usually getting the initialisation correct.
I battled with the pi for a week as I had the RS and E wires swapped ;-/

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2458
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by lrvick » Sat Jul 07, 2012 11:46 am
texy wrote:Hi,
with thanks to information in this thread :
viewtopic.php?f=64&t=4934
I have been able to get a LCD module to display messages using python directly from the GPIO
The code is not very clever, and also not very fast - I,m sure that some of you python experts can improve upon it, but it is clear (to me, at least!), and working !
Image

Code: Select all
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#       gpio_play6.py
# Texy 2012

import RPi.GPIO as GPIO
import time

def main():

    GPIO.setmode(GPIO.BCM) # use BCM I/O names
    GPIO.setup(24, GPIO.OUT) # E
    GPIO.setup(4, GPIO.OUT) # RS
    GPIO.setup(18, GPIO.OUT) # DB4
    GPIO.setup(21, GPIO.OUT) # DB5
    GPIO.setup(22, GPIO.OUT) # DB6
    GPIO.setup(23, GPIO.OUT) # DB7
    GPIO.setup(17, GPIO.OUT) # LED
    lcd_cmd(0x33) # $33 8-bit mode   
    lcd_cmd(0x32) # $32 8-bit mode   
    lcd_cmd(0x28) # $28 8-bit mode   
    lcd_cmd(0x0C) # $0C 8-bit mode   
    lcd_cmd(0x06) # $06 8-bit mode   
    lcd_cmd(0x01) # $01 8-bit mode   
    lcd_string("Hello Pi")
    lcd_cmd(0xC0) # next line
    lcd_string("Forum peeps!")


def lcd_cmd(bits):
   bits=bin(bits)
   bits=bits[2:]
   zeros=(8-len(bits))*"0"
   bits=zeros+bits
   GPIO.output(4, False) # RS low
   GPIO.output(18, False)
   GPIO.output(21, False)
   GPIO.output(22, False)
   GPIO.output(23, False)
   if bits[0]=="1" :
      GPIO.output(23, True)
   if bits[1]=="1" :
      GPIO.output(22, True)
   if bits[2]=="1" :
      GPIO.output(21, True)
   if bits[3]=="1" :
      GPIO.output(18, True)
   time.sleep(0.01)
   GPIO.output(24, True) # E high
   time.sleep(0.01)
   GPIO.output(24, False) # E low
   time.sleep(0.01)
   GPIO.output(18, False)
   GPIO.output(21, False)
   GPIO.output(22, False)
   GPIO.output(23, False)
   if bits[4]=="1" :
      GPIO.output(23, True)
   if bits[5]=="1" :
      GPIO.output(22, True)
   if bits[6]=="1" :
      GPIO.output(21, True)
   if bits[7]=="1" :
      GPIO.output(18, True)
   time.sleep(0.01)
   GPIO.output(24, True) # E high
   time.sleep(0.01)
   GPIO.output(24, False) # E low
   time.sleep(0.01)

def lcd_string(message):
   msg_len = len(message)
   for i in range(msg_len):
      lcd_char(message[i])
      
      
      


def lcd_char(bits):
   bits=bin(ord(bits))
   bits=bits[2:]
   zeros=(8-len(bits))*"0"
   bits=zeros+bits
   GPIO.output(4, True) # RS high
   GPIO.output(18, False)
   GPIO.output(21, False)
   GPIO.output(22, False)
   GPIO.output(23, False)
   if bits[0]=="1" :
      GPIO.output(23, True)
   if bits[1]=="1" :
      GPIO.output(22, True)
   if bits[2]=="1" :
      GPIO.output(21, True)
   if bits[3]=="1" :
      GPIO.output(18, True)
   time.sleep(0.01)
   GPIO.output(24, True) # E high
   time.sleep(0.01)
   GPIO.output(24, False) # E low
   time.sleep(0.01)
   GPIO.output(18, False)
   GPIO.output(21, False)
   GPIO.output(22, False)
   GPIO.output(23, False)
   if bits[4]=="1" :
      GPIO.output(23, True)
   if bits[5]=="1" :
      GPIO.output(22, True)
   if bits[6]=="1" :
      GPIO.output(21, True)
   if bits[7]=="1" :
      GPIO.output(18, True)
   time.sleep(0.01)
   GPIO.output(24, True) # E high
   time.sleep(0.01)
   GPIO.output(24, False) # E low
   time.sleep(0.01)

if __name__ == '__main__':
   main()


Please free feel to criticize or suggest improvements.
Texy



I forked your code and made it into a simple class setup:

https://github.com/lrvick/raspi-hd44780 ... hd44780.py

Enjoy :-)
Posts: 18
Joined: Wed Jun 20, 2012 4:35 pm
Location: Orlando, FL
by Reider » Tue Jul 10, 2012 11:30 am
A little lost here, meds don`t help. Can a 5v HD44870 compatible 4x20 LCD (Sparkfun Electronics-00256 model) be wired directly to the RasPi and utilize Python? Wondering if avoiding the read status would still make it safe in Python too. I`d like to learn Python and feel more than a little lost with C.

Just not sure how to get started and safely too. At the moment I do have the LCD wired and working with the Arduino but would much prefer a direct option with Python and the RasPi to get started with. Purchased LCD and header from Proto-Pic in the UK. If it`s wired the same as remoraebers` then I can do that, if it`s compatible with my LCD module and Python. Maybe I can convert someones Python program to suit then get it working.

Steve
Posts: 75
Joined: Sun Mar 04, 2012 12:00 pm
by texy » Tue Jul 10, 2012 11:44 am
Yes, the LCD I used above is a 5v device. As long as you do not input a 5v signal into the pi, you should be OK. As you are only writing to the LCD from the Pi, a high at only 3.3volts should still register as a high at the LCD. As for the Read/Write line, tie it to 0volts at the LCD end. If you do need to Read from the LCD, then you'll need a resistor pair to alter the voltage accordingly, but most circuits do not require the LCD to be Read from. The code above should work 'as is', even though you have a 20 x 4 LCD, but I,ve never used one, so can't be sure.
I,ve just noticed the comments for the initialisation codes are all '8-bit mode', which of course is incorrect. Also the code, although working, is now very inefficent and slow - see my Nokia LCD thread for improvements using the wiringpi class, which is considerably quicker.
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2458
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by Reider » Tue Jul 10, 2012 12:42 pm
Thanks Texy, have you any clear pictures of your wiring diagram/schematic? Since I was looking at remoraebers` textual instructions re his setup, with a view to converting to Python usage. Yours has a Python script but no diagram so I need to play it safe with this difference between the 3.3v and 5v. My first experiment either used a dud 16x2 LCD (not given up on it yet, top shows squares, bottom doesn`t, backlight works, may desolder and put a header in like I did with the present one) or I blew part of it, so I don`t want to lose the 20x4 or the RasPi. I notice also that rem didn`t use a resistor to ground so not sure on that or the size to use if I do.

Thanks again,

Steve
Posts: 75
Joined: Sun Mar 04, 2012 12:00 pm
by texy » Tue Jul 10, 2012 1:08 pm
Look at the GPIO pinout on this page :
[url]elinux.org/RPi_Low-level_peripherals[/url]

Then connect the relevant pins to these in the code :
Code: Select all
    GPIO.setup(24, GPIO.OUT) # E
    GPIO.setup(4, GPIO.OUT) # RS
    GPIO.setup(18, GPIO.OUT) # DB4
    GPIO.setup(21, GPIO.OUT) # DB5
    GPIO.setup(22, GPIO.OUT) # DB6
    GPIO.setup(23, GPIO.OUT) # DB7


Note that on the LCD, you'll need to make sure the following lines are tied to 0volts - r/w, DB0, DB1, DB2, DB3

I would keep with the 16 x 2 LCD for now - they are pretty probust, and usually its a connection or initialisation code problem if you are getting those blocks.
Hope this helps,
Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2458
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by texy » Tue Jul 10, 2012 1:10 pm
Oh, and note that I have the LCD's VCC line going to +5v on the Pi's GPIO port, not 3V3.

T.
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2458
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by lrvick » Tue Jul 10, 2012 1:23 pm
In my case I have a 3.3v version of the Hd33780 LCD which I wired up like so:

Image

(excuse the terrible soldering job, I only had a way over-powered iron with a fat tip handy)

To drive it I forked texys code and made it into a class that is a fair amount faster, however it is still using RPi.GPIO which is painfully slow overall (1sec+ to update screen). Will probably port to WiringPi this weekend and make it into a proper python library.

See: https://github.com/lrvick/raspi-hd44780

Also here is an example of using my current class to output simple stats (memory,cpu, ip address):

Code: Select all
from hd44780 import HD44780
from socket import gethostname, gethostbyname_ex
from psutil import cpu_percent, phymem_usage
from time import sleep

lcd = HD44780()

while 1:
    sleep(2)
    cpu = round(cpu_percent(),0)
    mem = round(phymem_usage()[3],0)
    ip = [ip for ip in gethostbyname_ex(gethostname())[2] if not ip.startswith("127.")][:1][0]
    lcd.clear()
    lcd.message('CPU %.0f%% MEM %.0f%%\nIP %s' % (cpu,mem,ip))
Posts: 18
Joined: Wed Jun 20, 2012 4:35 pm
Location: Orlando, FL
by kghunt » Wed Jul 11, 2012 9:34 pm
Did you ever get around to porting your library to wiring pi?
Posts: 360
Joined: Sun Mar 04, 2012 9:28 am
by texy » Thu Jul 12, 2012 6:03 pm
No I didn't, or at least I haven't yet. However, the RPi.GPIO module has been upgraded and is considerably faster, if you use the existing LCD code, and update the RPi.GPIO function, you'll find it will be much improved.

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2458
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by Reider » Sat Jul 14, 2012 10:03 am
Many many thanks guys, especially Texy. I tried this and got it working showing 2 lines out of my 4. Tried the wiringPi which is amazing but I know so little about Linux and C files + compiling that I finally had to admit defeat, my first headache in years. My fault, nobody elses and I don`t like to keep pestering Gordon all the time. I compiled it in Geany but it shows several errors when I build??? Don`t even know if I need to do that. Doublechecked the wiring too, totally lost.

So now I`m back to using this fine example and I said all along I really wanted to use Pi. Now I just need to know how to change it to 4 line display as I will definitely be using that from now on. Information is either low on the ground, hard to find or I`m using the wrong search terms but I`ll get there eventually. If anyone else wires up a 20x4 LCD and uses Python please post what you did and how you programmed it. I know once I get started I`ll be able to introduce a few tricks to it that I picked up in USB LCD Modules. If you see any posts/tutorials on 20x4 LCD and Python please post those too.

Thanks once again Gordon, Texy and all the guys for taking the time to help people and post what they did.

Steve
Posts: 75
Joined: Sun Mar 04, 2012 12:00 pm
by Reider » Sat Jul 14, 2012 1:00 pm
Woo hoo, a Eureka moment!

When using 4 lines eg 20x4 LCD. Each start of the line is addressed by;

Line 1 = 0 (128)
Line 2 = 64 (192)
Line 3 = 20 (148)
Line 4 = 84 (212)

The address + 128, don`t ask and I won`t tell. So we use the codes in brackets as the final number. This means Line 1 = 128, Line 2 = 192, Line 3 = 148, Line 4 = 212. Lines 2 and 3 are not switched or a misprint. I showed the figures before 128 were added simply to explain how they are arrived at for our use.

So here we go and apologies to Texy for reusing his code as an example. At least giving both 16x2 and 20x4 users a shot at it (not tested or amended for 20 characters yet but at least we have a start). Rome wasn`t built in a day.

Code: Select all
#!/usr/bin/python
    # -*- coding: utf-8 -*-
    #
    #       gpio_play6.py
    # Texy 2012

    import RPi.GPIO as GPIO
    import time

    def main():

        GPIO.setmode(GPIO.BCM) # use BCM I/O names
        GPIO.setup(24, GPIO.OUT) # E
        GPIO.setup(4, GPIO.OUT) # RS
        GPIO.setup(18, GPIO.OUT) # DB4
        GPIO.setup(21, GPIO.OUT) # DB5
        GPIO.setup(22, GPIO.OUT) # DB6
        GPIO.setup(23, GPIO.OUT) # DB7
        GPIO.setup(17, GPIO.OUT) # LED
        lcd_cmd(0x33) # $33 8-bit mode   
        lcd_cmd(0x32) # $32 8-bit mode   
        lcd_cmd(0x28) # $28 8-bit mode   
        lcd_cmd(0x0C) # $0C 8-bit mode   
        lcd_cmd(0x06) # $06 8-bit mode   
        lcd_cmd(0x01) # $01 8-bit mode
        lcd_cmd(128) # This is Line 1
        lcd_string("Hello Pi")
        lcd_cmd(192) # This is line 2
        lcd_string("Forum peeps!")
        lcd_cmd(148) # This is line 3
        lcd_string("Nice to")
        lcd_cmd(212) # This is line 4
        lcd_string("meet you all.")

    def lcd_cmd(bits):
       bits=bin(bits)
       bits=bits[2:]
       zeros=(8-len(bits))*"0"
       bits=zeros+bits
       GPIO.output(4, False) # RS low
       GPIO.output(18, False)
       GPIO.output(21, False)
       GPIO.output(22, False)
       GPIO.output(23, False)
       if bits[0]=="1" :
          GPIO.output(23, True)
       if bits[1]=="1" :
          GPIO.output(22, True)
       if bits[2]=="1" :
          GPIO.output(21, True)
       if bits[3]=="1" :
          GPIO.output(18, True)
       time.sleep(0.01)
       GPIO.output(24, True) # E high
       time.sleep(0.01)
       GPIO.output(24, False) # E low
       time.sleep(0.01)
       GPIO.output(18, False)
       GPIO.output(21, False)
       GPIO.output(22, False)
       GPIO.output(23, False)
       if bits[4]=="1" :
          GPIO.output(23, True)
       if bits[5]=="1" :
          GPIO.output(22, True)
       if bits[6]=="1" :
          GPIO.output(21, True)
       if bits[7]=="1" :
          GPIO.output(18, True)
       time.sleep(0.01)
       GPIO.output(24, True) # E high
       time.sleep(0.01)
       GPIO.output(24, False) # E low
       time.sleep(0.01)

    def lcd_string(message):
       msg_len = len(message)
       for i in range(msg_len):
          lcd_char(message[i])
         
         
         


    def lcd_char(bits):
       bits=bin(ord(bits))
       bits=bits[2:]
       zeros=(8-len(bits))*"0"
       bits=zeros+bits
       GPIO.output(4, True) # RS high
       GPIO.output(18, False)
       GPIO.output(21, False)
       GPIO.output(22, False)
       GPIO.output(23, False)
       if bits[0]=="1" :
          GPIO.output(23, True)
       if bits[1]=="1" :
          GPIO.output(22, True)
       if bits[2]=="1" :
          GPIO.output(21, True)
       if bits[3]=="1" :
          GPIO.output(18, True)
       time.sleep(0.01)
       GPIO.output(24, True) # E high
       time.sleep(0.01)
       GPIO.output(24, False) # E low
       time.sleep(0.01)
       GPIO.output(18, False)
       GPIO.output(21, False)
       GPIO.output(22, False)
       GPIO.output(23, False)
       if bits[4]=="1" :
          GPIO.output(23, True)
       if bits[5]=="1" :
          GPIO.output(22, True)
       if bits[6]=="1" :
          GPIO.output(21, True)
       if bits[7]=="1" :
          GPIO.output(18, True)
       time.sleep(0.01)
       GPIO.output(24, True) # E high
       time.sleep(0.01)
       GPIO.output(24, False) # E low
       time.sleep(0.01)

    if __name__ == '__main__':
       main()
Posts: 75
Joined: Sun Mar 04, 2012 12:00 pm
by tailer-gth » Sat Jul 14, 2012 10:12 pm
Hey,

i get it running and its fine. I used the class ;-) Thx lrvick.

But now i want to write the date and time to my LCD, but i dont know how i can do that.
Can anyone help me pls?

Thx
Tailer
Posts: 3
Joined: Sat Jul 14, 2012 10:07 pm
by lrvick » Sat Jul 14, 2012 11:43 pm
tailer-gth wrote:Hey,

i get it running and its fine. I used the class ;-) Thx lrvick.

But now i want to write the date and time to my LCD, but i dont know how i can do that.
Can anyone help me pls?

Thx
Tailer


Here is a very simple clock using my class:

Code: Select all
from hd44780 import HD44780
from datetime import datetime
from time import sleep

lcd = HD44780()

while True:
    sleep(1)
    lcd.clear()
    lcd.message(datetime.now().strftime('  %I : %M : %S \n%a %b %d %Y'))
Posts: 18
Joined: Wed Jun 20, 2012 4:35 pm
Location: Orlando, FL
by Reider » Sun Jul 15, 2012 10:03 am
4 line test for 20x4. I posted ABCDEFGHIJKLMNOPQRST to all four lines and got all four lines back. Not being fully au fait with Python I was concerned there may be a code in there locking it to 16x2 as it was originally devised for that. I may have over written the 4 lines from 2 but not managed to get 20 characters. It seems from this that 20x4 is fully unlocked and all 20 cells in each line are accessible and possible to write to.

My secondary concern was the way lines 1 and 3, 2 and 4 are interconnected. I had visions of entries moving from line 1 to line 3 for example, instead of line 2. This test again has removed those fears and gives us a chassis to start using all 4 lines, firmly in the knowledge that what we use a code for to place a character in a given line and position is going to work.

So let`s recap;

Line 1 = 128
Line 2 = 192
Line 3 = 148
Line 4 = 212

That gives us the start of each of the 4 lines, or indeed the same can be said for the 2 lines in a 16/2 since it works there too. We address each start of line thus;

lcd_cmd(128) # This is Line 1
lcd_string("Line 1 text")
lcd_cmd(192) # This is Line 2
lcd_string("Line 2 text")
lcd_cmd(148) # This is Line 3
lcd_string("Line 3 text")
lcd_cmd(212) # This is Line 4
lcd_string("Line 4 text")

So if we want centre for our chosen text on line 2 we see the code starts at 192 - 211, so it restarts at line 212 in line 4 (1&3, 2&4). There are 11 characters inc spaces in our line 2 text so 20 - 11 = 9 then divide by 2 (to get an equal gap both ends of the text) and we see approx 4 characters at the start and 5 at the end. So add 4 to 192 inc 192, 195 would centralize the chosen text. In line 4 it would be 215. Change the text to something else and count the characters inc spaces between words, subtract from 20 and divide by two. Then add that figure to the start code, that`s where you want to be roughly, assuming you want centred text.

Just a little FYI......

Steve
Posts: 75
Joined: Sun Mar 04, 2012 12:00 pm
by kghunt » Sun Jul 15, 2012 11:36 am
When writing to an LCD I just write the whole line every time it is easier.
Posts: 360
Joined: Sun Mar 04, 2012 9:28 am
by tailer-gth » Mon Jul 16, 2012 5:56 pm
Hey,

after i tested the date and so on its very easy to write something to the Raspi.

Now i got a ver simple Problem, when i write:

lcd.message("example")

then the lcd show me something like this:

mple

so it cuts the first characters, the reason for me was to write this:

lcd.message(" example")
then it works.

Where is the problem??

Thx Tailer
Posts: 3
Joined: Sat Jul 14, 2012 10:07 pm
by kghunt » Mon Jul 16, 2012 6:48 pm
I know with my LCD it expects 16 characters exactly. So if it is less I have to pad it to 16 with spaces and if it is more I have to trim it to 16 or it will not display correctly. Maybe it's a similar thing?
Posts: 360
Joined: Sun Mar 04, 2012 9:28 am
by lrvick » Mon Jul 16, 2012 8:46 pm
tailer-gth wrote:Hey,

after i tested the date and so on its very easy to write something to the Raspi.

Now i got a ver simple Problem, when i write:

lcd.message("example")

then the lcd show me something like this:

mple

so it cuts the first characters, the reason for me was to write this:

lcd.message(" example")
then it works.

Where is the problem??

Thx Tailer


Hmm. Sure your using my latest push from this week? The sleep I added should of resolved that...

Also try doing lcd.clear() before sending in text.
Posts: 18
Joined: Wed Jun 20, 2012 4:35 pm
Location: Orlando, FL
by kghunt » Tue Jul 17, 2012 10:14 am
Using this thread I managed to get a £2 (free delivery) 1602 LCD from china working off the GPIO's.

Image

At first I could not get it to work but I realised I had forgot to tie RW to GND.

The code supplied here works good pretty fast too the only issue I have is that if I write to both lines there is a flicker on the second line because it has to write the first line first I think. It would be better to write the lines separately like I do in my USB lcd driver.

The only things that I miss from the USB one is the ability to control the back light and brightness from software. Although I am going to work out how I can put another pot on the back light so I can adjust in manually.
Posts: 360
Joined: Sun Mar 04, 2012 9:28 am
by kghunt » Tue Jul 17, 2012 10:54 am
I added a second pot to the negative of the back light and now I can trim that too.
Posts: 360
Joined: Sun Mar 04, 2012 9:28 am