raspberryfish
Posts: 16
Joined: Wed May 23, 2012 6:13 pm

Re: Python driving 44870 LCD

Sun Dec 02, 2012 12:05 pm

Hi,

can anyone post a schematic how to wire up a 44870 with pi?

Thanks
Peter

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Python driving 44870 LCD

Sun Dec 02, 2012 12:43 pm

raspberryfish wrote:Hi,

can anyone post a schematic how to wire up a 44870 with pi?

Thanks
Peter

On page 2 of this very thread, someone has done exactly that.

http://i303.photobucket.com/albums/nn12 ... piGPIO.png
Alex Eames RasPi.TV, RasP.iO

raspberryfish
Posts: 16
Joined: Wed May 23, 2012 6:13 pm

Re: Python driving 44870 LCD

Sun Dec 02, 2012 3:25 pm

mmh there is an issue.

I wired up like in the picture. But i get nothing. First line hast white Squares, second has dark squares. BL is on. When i use one of the codes examples which were posted in this thread, nothing is happen.

:-/

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Python driving 44870 LCD

Sun Dec 02, 2012 3:57 pm

Either the wiring is wrong, or the code does not match the wiring. Show your program, and a picture of your wiring ;)
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

canibalimao
Posts: 557
Joined: Tue Aug 07, 2012 8:48 pm
Location: Portugal

Re: Python driving 44870 LCD

Thu Dec 06, 2012 3:26 pm

raspberryfish wrote:mmh there is an issue.

I wired up like in the picture. But i get nothing. First line hast white Squares, second has dark squares. BL is on. When i use one of the codes examples which were posted in this thread, nothing is happen.

:-/
I'm having the same problem as you. First line full of squares and then I run this code and nothing happens.
Can anyone help me?

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Python driving 44870 LCD

Thu Dec 06, 2012 6:34 pm

Hi Pedro,
firstly the LCd I supply is not compatible with the code you posted. The LCD's control and data lines need to match those of the LCD such :
LCD_RS = 17
LCD_E = 4
LCD_D4 = 24
LCD_D5 = 22
LCD_D6 = 23
LCD_D7 = 21
LCD_LED = 18

Secondly, if you are using a v2 rpi, then the 21 must be changed to 27.
Hope his helps,
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Python driving 44870 LCD

Thu Dec 06, 2012 6:52 pm

I have done a TKinter program to drive and test the LCD board that I sell. You can modify it for your own LCD if you wish ;)

First copy and paste this into a new file called LCD :

Code: Select all

#!/usr/bin/python
#
# HD44780 LCD Test Script for
# Raspberry Pi
#
# Author : Matt Hawkins + Texy
# Site   : http://www.raspberrypi-spy.co.uk
# 
# Date   : 2/12/2012
#
# uses RPi.GPIO to drive the LCD and
# wiringpi to drive the backlight LED via hardware PWM
# this version to be used with TKinter
# this version for v1 rpi

#import
import RPi.GPIO as GPIO
import time
import wiringpi

# Define GPIO to LCD mapping
LCD_RS = 17
LCD_E  = 4
LCD_D4 = 24
LCD_D5 = 22
LCD_D6 = 23
LCD_D7 = 21 # use 27 for v2 rpi, or 21 for v1 rpi
LCD_LED = 18

# Define some device constants
LCD_WIDTH = 20    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line 
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005

def main():
  # Main program block
  # Initialise display
  lcd_init()

  # Send some test
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("wiringpi and")
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("RPi.GPIO 2gether")

def line1txt(message1):
  message1=message1[:-1] # remove last char (whitespace)
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string(message1)
  
def line2txt(message2):
  message2=message2[:-1]
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string(message2)

def line3txt(message3):
  message3=message3[:-1]
  lcd_byte(LCD_LINE_3, LCD_CMD)
  lcd_string(message3)

def line4txt(message4):
  message4=message4[:-1]
  lcd_byte(LCD_LINE_4, LCD_CMD)
  lcd_string(message4)
  
def cls():
  lcd_byte(0x01,LCD_CMD)

def led(led_value):
 wiringpi.pwmWrite(LCD_LED, led_value)
# print led_value


def switch_led(led_value):
    if led_value==0:
        wiringpi.pinMode(LCD_LED,1)
        wiringpi.digitalWrite(LCD_LED,0)
    else:
        wiringpi.pinMode(LCD_LED,2)
        wiringpi.pwmWrite(LCD_LED, 128);
 
def lcd_init():
  # Initialise display
  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  GPIO.setwarnings(False)
  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  wiringpi.wiringPiSetupGpio()
  wiringpi.pinMode(LCD_LED, 2) # LED set up as PWM
  wiringpi.pwmWrite(LCD_LED, 128)
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD)  
  lcd_byte(0x06,LCD_CMD)
  cls()  

def lcd_string(message):
  # Send string to display

  message = message.ljust(LCD_WIDTH," ")  

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command

  GPIO.output(LCD_RS, mode) # RS

  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)      

  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)   

if __name__ == '__main__':
  main()
Note, if you have a v2 rpi, then you need this version instead :

Code: Select all

#!/usr/bin/python
#
# HD44780 LCD Test Script for
# Raspberry Pi
#
# Author : Matt Hawkins + Texy
# Site   : http://www.raspberrypi-spy.co.uk
# 
# Date   : 2/12/2012
#
# uses RPi.GPIO to drive the LCD and
# wiringpi to drive the backlight LED via hardware PWM
# this version to be used with TKinter
# this version for v2 rpi only

#import
import RPi.GPIO as GPIO
import time
import wiringpi

# Define GPIO to LCD mapping
LCD_RS = 17
LCD_E  = 4
LCD_D4 = 24
LCD_D5 = 22
LCD_D6 = 23
LCD_D7 = 27 # use 27 for v2 rpi, or 21 for v1 rpi
LCD_LED = 18

# Define some device constants
LCD_WIDTH = 20    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line 
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005

def main():
  # Main program block
  # Initialise display
  lcd_init()

  # Send some test
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("wiringpi and")
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("RPi.GPIO 2gether")

def line1txt(message1):
  message1=message1[:-1] # remove last char (whitespace)
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string(message1)
  
def line2txt(message2):
  message2=message2[:-1]
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string(message2)

def line3txt(message3):
  message3=message3[:-1]
  lcd_byte(LCD_LINE_3, LCD_CMD)
  lcd_string(message3)

def line4txt(message4):
  message4=message4[:-1]
  lcd_byte(LCD_LINE_4, LCD_CMD)
  lcd_string(message4)
  
def cls():
  lcd_byte(0x01,LCD_CMD)

def led(led_value):
 wiringpi.pwmWrite(LCD_LED, led_value)
# print led_value


def switch_led(led_value):
    if led_value==0:
        wiringpi.pinMode(LCD_LED,1)
        wiringpi.digitalWrite(LCD_LED,0)
    else:
        wiringpi.pinMode(LCD_LED,2)
        wiringpi.pwmWrite(LCD_LED, 128);
 
def lcd_init():
  # Initialise display
  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  GPIO.setwarnings(False)
  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  wiringpi.wiringPiSetupGpio()
  wiringpi.pinMode(LCD_LED, 2) # LED set up as PWM
  wiringpi.pwmWrite(LCD_LED, 128)
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD)  
  lcd_byte(0x06,LCD_CMD)
  cls()  

def lcd_string(message):
  # Send string to display

  message = message.ljust(LCD_WIDTH," ")  

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command

  GPIO.output(LCD_RS, mode) # RS

  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)      

  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)   

if __name__ == '__main__':
  main()
Then create a second file using this program :

Code: Select all

import LCD
from LCD import *
import Tkinter
from Tkinter import *
import tkFont


        
class App:

    def __init__(self, parent):

        frame = Frame(parent);
        frame.pack();

        parent.title("44780 LCD GUI");
 
        self.Line1Entry = Text(frame, width=16, height=1, font=("systemfixed", 12));
        self.Line1Entry.grid(row=0, column=2, columnspan=1);

        self.Line2Entry = Text(frame, width=16, height=1, font=("systemfixed", 12));
        self.Line2Entry.grid(row=1, column=2, columnspan=1);

        self.Line3Entry = Text(frame, width=16, height=1, font=("systemfixed", 12));
        self.Line3Entry.grid(row=2, column=2, columnspan=1);

        self.Line4Entry = Text(frame, width=16, height=1, font=("systemfixed", 12));
        self.Line4Entry.grid(row=3, column=2, columnspan=1);

        self.sndButt1 = Button(frame, text="Send Line 1", command=self.send_line1txt);
        self.sndButt1.grid(row=0, column=4, padx=5, pady=5, sticky=W);

        self.sndButt2 = Button(frame, text="Send Line 2", command=self.send_line2txt);
        self.sndButt2.grid(row=1, column=4, padx=5, pady=5, sticky=W);

        self.sndButt3 = Button(frame, text="Send Line 3", command=self.send_line3txt);
        self.sndButt3.grid(row=2, column=4, padx=5, pady=5, sticky=W);

        self.sndButt4 = Button(frame, text="Send Line 4", command=self.send_line4txt);
        self.sndButt4.grid(row=3, column=4, padx=5, pady=5, sticky=W);

        self.lightVar = IntVar();
        self.chkB = Checkbutton(frame, text="Backlight On/Off", variable=self.lightVar, offvalue=0, onvalue=1, command=self.back_off);
        self.chkB.grid(row=4, columnspan=3, sticky=W);
        self.chkB.select();

        self.clsButt = Button(frame, text="CLS", command=LCD.cls);
        self.clsButt.grid(row=5, column=4, padx=5, pady=5);

        self.quitButt = Button(frame, text="Quit", fg="red", command=frame.quit);
        self.quitButt.grid(row=55, column=4);

    def send_line1txt(self):
        LCD.line1txt(self.Line1Entry.get(1.0, END));

    def send_line2txt(self):
        LCD.line2txt(self.Line2Entry.get(1.0, END));

    def send_line3txt(self):
        LCD.line3txt(self.Line3Entry.get(1.0, END));

    def send_line4txt(self):
        LCD.line4txt(self.Line4Entry.get(1.0, END));

    def back_off(self):
        LCD.switch_led(self.lightVar.get());


def sel_pwm():
    pwm = var2.get()
#    selection = "Contrast = " + str(value)
#    label.config(text = selection)
#    print int(pwm)
    LCD.led(int(pwm))
   
root = Tk();
LCD.lcd_init();



var2 = IntVar()
scale2 = Scale( root, orient = HORIZONTAL, from_ = 0, to = 255, variable = var2 )
scale2.pack(anchor=CENTER)
button2 = Button(root, text="Backlight PWM", command=sel_pwm)
button2.pack(anchor=CENTER)

def callback():
    print "called the callback!";

# create a toolbar
toolbar = Frame(root);


toolbar.pack(side=TOP, fill=X);

label = Label(root)
label.pack()
app = App(root);

root.mainloop();
You'll need to of installed wiring pi for python and RPi.GPIO and be in X. From the xterminal use sudo idle then select the second file and F5 to run it.

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

canibalimao
Posts: 557
Joined: Tue Aug 07, 2012 8:48 pm
Location: Portugal

Re: Python driving 44870 LCD

Thu Dec 06, 2012 7:09 pm

Hi texy,

Thanks for the pass, but theres is some things I don't understand:

1- I need to install any driver for the LCD or something? I've already installed this library for the python...

2- I don't understand your last note. I need to run that program only on X, right? But I will need to do that on X always, or just the first time?

3- On the second code you have there 4 lines to display. With a 2 line display, if I want to scroll text in multiple lines (2 are shown now, and other 2 are shown after a especified number of seconds) I cand use that piece of code, right?

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Python driving 44870 LCD

Thu Dec 06, 2012 7:27 pm

Hi pedro,
if you have RPi.GPIO installed and working, then that is good. You need 'wiring pi for python' installed and TKinter installed for my program above which must be run in X always. Wiring pi for python is only used to drive the backlight as it uses the hardware pwm. If you do not wish to use pwm, you can still use RPi.GPIO to switch on and off the backlight, but you will not be able to control the brightness.

This program should work using Ri.GPIO only :

Code: Select all

#!/usr/bin/python
#
# HD44780 LCD Test Script for
# Raspberry Pi
#
# Author : Matt Hawkins
# Site   : http://www.raspberrypi-spy.co.uk
# 
# Date   : 26/07/2012
#

# The wiring for the LCD is as follows:
# 1 : GND
# 2 : 5V
# 3 : Contrast (0-5V)*
# 4 : RS (Register Select)
# 5 : R/W (Read Write)       - GROUND THIS PIN
# 6 : Enable or Strobe
# 7 : Data Bit 0             - NOT USED
# 8 : Data Bit 1             - NOT USED
# 9 : Data Bit 2             - NOT USED
# 10: Data Bit 3             - NOT USED
# 11: Data Bit 4
# 12: Data Bit 5
# 13: Data Bit 6
# 14: Data Bit 7
# 15: LCD Backlight +5V**
# 16: LCD Backlight GND

#import
import RPi.GPIO as GPIO
import time

# Define GPIO to LCD mapping
LCD_RS = 17
LCD_E  = 4
LCD_D4 = 24
LCD_D5 = 22
LCD_D6 = 23
LCD_D7 = 21
LCD_LED = 18

# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line 

# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005

def main():
  # Main program block

  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  GPIO.setwarnings(False)
  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  GPIO.setup(LCD_LED, GPIO.OUT) # LED

  # Initialise display
  lcd_init()

  # LED backlite ON
  GPIO.output(LCD_LED, True)

  # Send some test
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Rasbperry Pi")
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("Model B")

  time.sleep(3) # 3 second delay

  # Send some text
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Raspberrypi-spy")
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string(".co.uk")

#  time.sleep(20)

def lcd_init():
  # Initialise display
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD)  
  lcd_byte(0x06,LCD_CMD)
  lcd_byte(0x01,LCD_CMD)  

def lcd_string(message):
  # Send string to display

  message = message.ljust(LCD_WIDTH," ")  

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command

  GPIO.output(LCD_RS, mode) # RS

  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)      

  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)   

if __name__ == '__main__':
  main()
But note that if you have a V2 RPI then you need to change LCD_D7 = 21 to LCD_D7 = 27.
Also note that you do not need to be in X to run this code, but you will need to be root, or use this command :

pi@raspberry:~$ sudo python myprog.py

Hope this helps,
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

canibalimao
Posts: 557
Joined: Tue Aug 07, 2012 8:48 pm
Location: Portugal

Re: Python driving 44870 LCD

Thu Dec 06, 2012 8:36 pm

Oh thanks Texy.

I don't know if the RPi.GPIO is running fine, I installed it a few days ago and never tried it.
I will try your program and then tell you the results.

Just two more things:
1- the version 2 that you're talking about is that with mounting holes, right? Mine version only have the 0ohm resistors, and no holes.

2- The TKinter only runs under X environment? I'm thinking about run it throw Putty, so I'll need to install a tool to work with a remote desktop...

Thank you very much for the patience! I'm a real noob :lol:

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Python driving 44870 LCD

Thu Dec 06, 2012 10:27 pm

1..I think you have a v1 rpi
2..yes, only for X, because it is for windows, button, sliders and text entry, etc.

I would make sure you can get RPi. GPIO working first. Do you have some LED's?
If not I will send something in the post with the resistor.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

canibalimao
Posts: 557
Joined: Tue Aug 07, 2012 8:48 pm
Location: Portugal

Re: Python driving 44870 LCD

Fri Dec 07, 2012 7:03 pm

texy wrote:1..I think you have a v1 rpi
2..yes, only for X, because it is for windows, button, sliders and text entry, etc.

I would make sure you can get RPi. GPIO working first. Do you have some LED's?
If not I will send something in the post with the resistor.
Texy
Thanks for the replies Texy.

I'll search for a remote desktop for the RPi and then I'll install those programs :mrgreen:

Right now I haven't. I had some in my pencil holder, but I lost it. Also had there some transistors and a couple of potentiometers. I need to "borrow" some more components on my university :lol:

I tried now install again the RPi.GPIO and I get an error with "gcc". I think I have the compiler installed already, but I'll try it again...

Thanks Texy

canibalimao
Posts: 557
Joined: Tue Aug 07, 2012 8:48 pm
Location: Portugal

Re: Python driving 44870 LCD

Sat Dec 08, 2012 11:25 am

Hi Texy (again :oops: )

I follow your gide with the TKinter and I get this error when type "sudo idle":

Code: Select all

pi@raspberrypi ~ $ sudo idle
PuTTY X11 proxy: wrong authorisation protocol attemptedTraceback (most recent call last):
  File "/usr/bin/idle", line 5, in <module>
    main()
  File "/usr/lib/python2.7/idlelib/PyShell.py", line 1406, in main
    root = Tk(className="Idle")
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1688, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display "localhost:10.0"
And when I press F5 on the second file I get this:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/LCD/teste.py", line 1, in <module>
    import LCD
  File "/home/pi/LCD/LCD.py", line 1
    Python 2.7.3rc2 (default, May  6 2012, 20:02:25)
                   ^
SyntaxError: invalid syntax
(the ^ is under the "7")

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Python driving 44870 LCD

Sat Dec 08, 2012 11:34 am

I've not seen those errors before. Have you apt-get update and upgrade recently? If not then I would do so.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

canibalimao
Posts: 557
Joined: Tue Aug 07, 2012 8:48 pm
Location: Portugal

Re: Python driving 44870 LCD

Sat Dec 08, 2012 12:03 pm

Yes, I had run the apt-get update right this morning.

And I have good news: I picked up your code for the RPi-GPIO and it worked. I had to change a few things (Python don't like the "tabs" on the begining of each line :lol:) but now It shows the messages. The first time I run it the screen was a bit confuse with a lot of chinese characters, but the second time it show right what I want :D

Now I'll try to put the text in loop. I don't want to have the same info on the screen...

Thank you very much Texy. Really! This comunity need guys like you helping the new guys :mrgreen:

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Python driving 44870 LCD

Sat Dec 08, 2012 12:11 pm

Great.
I sent you LED and resistors in the post today.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

poing
Posts: 1132
Joined: Thu Mar 08, 2012 3:32 pm

Re: Python driving 44870 LCD

Sat Dec 08, 2012 12:18 pm

After seeing this thread I cleared out my PayPal account yesterday and ordered a 1620 LCD unit for a whopping €1.78 including shipping from China. Can't wait.

canibalimao
Posts: 557
Joined: Tue Aug 07, 2012 8:48 pm
Location: Portugal

Re: Python driving 44870 LCD

Sat Dec 08, 2012 1:34 pm

texy wrote:Great.
I sent you LED and resistors in the post today.
Texy
Thank you very much (really!) for everything, specially for the patience!

I can't wait to get rid of my exams to test this to the limit :D
poing wrote:After seeing this thread I cleared out my PayPal account yesterday and ordered a 1620 LCD unit for a whopping €1.78 including shipping from China. Can't wait.
Keep in mind that it could take more than a month to arrive... The posts in China are really busy now because the christmas time. I'm waiting for a package since 25th November, so be prepared for the worst :?

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Defining and using custom characters

Sun Dec 30, 2012 10:48 am

Here is a short example of how to define and use custom characters with a 44780 based LCD. There is an area of 64 bytes of RAM in the chip that is set aside for defining a maximum of 8 5x8 custom characters. The start address of this memory is 0x40 hex, (or 64 decimal). The first character has a 'value' of 0. To define a character you first need to send 0x40 as a command byte, followed by 8 character bytes that map the character - remember that as there are only 5 bits to each row, only those bits, from the lsb onwards, count.

Code: Select all

#!/usr/bin/python
#
# HD44780 LCD Test Script for
# Raspberry Pi
#
# Author : Matt Hawkins + Texy
# Site   : http://www.raspberrypi-spy.co.uk
# 
# Date   : 29/12/2012
#
# uses RPi.GPIO to drive the LCD and
# wiringpi to drive the backlight LED via hardware PWM

#import
import RPi.GPIO as GPIO
import time
import wiringpi

# Define GPIO to LCD mapping
LCD_RS = 17
LCD_E  = 4
LCD_D4 = 24
LCD_D5 = 22
LCD_D6 = 23
LCD_D7 = 27
LCD_LED = 18

# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line 

# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005

# CGRAM definitions
bytes = [8,20,8,7,8,8,8,7] # degrees celcius character

def main():
  # Main program block
  GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
  GPIO.setwarnings(False)      # supress the warning messages 
  GPIO.setup(LCD_E, GPIO.OUT)  # E
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
  wiringpi.wiringPiSetupGpio()
  wiringpi.pinMode(LCD_LED, 2) # LED set up as PWM
  wiringpi.pwmWrite(LCD_LED, 128) # set pwm to mid brightness
  # Initialise display
  lcd_init()
  cgchar(0x40, bytes) # set up the custom degrees character as char 0
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Temperature:")
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("23")
  lcd_byte(0, LCD_CHR) # send degrees celcius character

def cgchar(CGCHR, bytes):
  lcd_byte(CGCHR, LCD_CMD)
  for value in (bytes[0],bytes[1],bytes[2],bytes[3],bytes[4],bytes[5],bytes[6],bytes[7]):
    lcd_byte(value, LCD_CHR)
  
def lcd_init():
  # Initialise display
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD)  
  lcd_byte(0x06,LCD_CMD)
  lcd_byte(0x01,LCD_CMD)  

def lcd_string(message):
  # Send string to display
  for i in range(len(message)):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command

  GPIO.output(LCD_RS, mode) # RS

  # High bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)      

  # Low bits
  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)   

if __name__ == '__main__':
  main()
Image

Here is a useful online webpage tool for defining characters :
http://www.quinapalus.com/hd44780udg.html

Hope this helps,
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2876
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Python driving 44870 LCD

Sun Dec 30, 2012 11:42 am

Excellent. Thanks for this Texy and Matt :)

I had stumbled upon that character design page, but making the jump from designing a character to "getting the bits into the LCD controller" was going to take me too long to work out, so I'd shelved it. :lol:
Alex Eames RasPi.TV, RasP.iO

poing
Posts: 1132
Joined: Thu Mar 08, 2012 3:32 pm

Re: Python driving 44870 LCD

Sun Dec 30, 2012 12:00 pm

I finally received my 16x2 LCD yesterday. Quickly soldered the wires to the little board, carefully (I thought) connected everything to the Pi and a breadboard, fired her up... I saw some very strange and intense grayish-white blocks coming up for a while, then...nothing. So I tried some different setups and code blocks posted around the net, ... nothing.

Then I took a closer look at the wires in the breadboard... AAAARRRGH! There were two blue wires (ground) stuck in the 5V area and one red wire (5V) in the ground area! I killed my LCD! :cry: I guess my wife was right after all, man I feel stupid.

Luckily the little gadget was only $2.20 including shipping, so it won't break the bank, and I ordered 3 more right away. See you guys next month I guess :( Still flabbergasted how I could make a mistake like that so easily.

Rezendes
Posts: 12
Joined: Sat May 25, 2013 11:53 pm

Re: Python driving 44870 LCD

Sun May 26, 2013 3:55 am

Hi, I'm brand new to LCDs and Raspberry Pi GPIO. I've wired up a LMB162ABC 16x2 LCD that came in an Arduino Uno kit from Maker Faire. I've used just any open GPIO pins that I wanted figuring it doesn't matter which ones I use as long as they are GPIO, is that correct? I want to operate in 4 bit mode so I have wired it accordingly. I get the screen to come on and the contrast variable resistor works and one row lights up solid blocks. I have tried a few python code examples and even adjusted the pins in the code to match the GPIO pins I chose and no matter what I do I cannot seem to get the display to do anything (besides the solid row of blocks). Can someone please assist me with some super basic code or what I may be doing wrong? Here's a link to a PDF of the specs for the LCD I'm using if it helps. http://www.datasheetdir.com/LMB162ABC+download

I've tried every single code example to run an lcd on the raspi and nothing happens, wondering why it's not working...

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Python driving 44870 LCD

Sun May 26, 2013 8:15 am

Did you remember to tie the r/w line and db0-3 to gnd?
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Rezendes
Posts: 12
Joined: Sat May 25, 2013 11:53 pm

Re: Python driving 44870 LCD

Mon May 27, 2013 2:52 am

db0-3 have to be tied to Ground? are you for sure about that? I swear I saw a youtube example where the guy ran his LCD in 4-bit mode with those extra 4 pins just nothing connected... if that is mandatory, then that may be my problem. I definitely have the r/w pin grounded though.

texy
Forum Moderator
Forum Moderator
Posts: 5172
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Python driving 44870 LCD

Mon May 27, 2013 7:26 am

For 4-bit mode, those pins need to be tied to gnd. However done lcd's may already have pull down resistors on board, others may not. No harm in trying it ;-)
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Python”