HELP: LCD Module: Cannot run python script (GPIO)


15 posts
by strandedmotorist » Sun Dec 09, 2012 3:16 pm
Hello again, another problem im having....

Im wanting to run this python script for a 16x2 lcd module:
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 = 7
LCD_E  = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 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.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

  # Initialise display
  lcd_init()

  # 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()


I got this code from: http://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/

So i done
Code: Select all
sudo apt-get install python-rpi.gpio
and this worked fine,

I then Opened IDLE and saved the script for the lcd as lcdtest.py

I went into the terminal and typed
Code: Select all
cd /home/pi
followed by
Code: Select all
sudo python lcdtest.py
When the pi tried to run it i got something along the lines of "run-time warning: this channel is already in use, continuing anyway"
Then another time it came up with syntax errors, and sometimes it couldnt find the file.

Im new to the pi, python and GPIO. Is there anyone that could give me a step by step guide even to get the script running? Or what am i doing wrong with this?

Thanks in advance! :)

Mike
Mmmm....pie..... Mmmm..... pi
Posts: 19
Joined: Sat Sep 29, 2012 6:53 pm
by rickseiden » Sun Dec 09, 2012 4:44 pm
It's not an error, but a warning. Your project should continue to run and everything should function even after seeing this warning. The warning gives directions on how to turn it off.
There are 10 types of people in this world. Those that understand binary, and those that don't.
Posts: 389
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA
by rickseiden » Sun Dec 09, 2012 4:45 pm
PS: That script is pretty much the standard for getting a display to run. If your pi and display are wired correctly, it's a slam dunk, so if it's not working, check your wiring.
There are 10 types of people in this world. Those that understand binary, and those that don't.
Posts: 389
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA
by strandedmotorist » Sun Dec 09, 2012 5:15 pm
rickseiden wrote:PS: That script is pretty much the standard for getting a display to run. If your pi and display are wired correctly, it's a slam dunk, so if it's not working, check your wiring.


Ahh, ok. And if I'm doing everything right so far, i will check the wiring then just in case.
Mmmm....pie..... Mmmm..... pi
Posts: 19
Joined: Sat Sep 29, 2012 6:53 pm
by strandedmotorist » Sun Dec 09, 2012 7:31 pm
If this could help anyone get to the cause of my problem,

Heres the code that comes up after the commands, im pretty sure its working as it should:

Code: Select all
pi@raspberrypi ~ $ cd /home/pi
pi@raspberrypi ~ $ sudo python lcdtest.py
lcdtest.py:58: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_E, GPIO.OUT)  # E
lcdtest.py:59: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_RS, GPIO.OUT) # RS
lcdtest.py:60: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_D4, GPIO.OUT) # DB4
lcdtest.py:61: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_D5, GPIO.OUT) # DB5
lcdtest.py:62: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_D6, GPIO.OUT) # DB6
lcdtest.py:63: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(LCD_D7, GPIO.OUT) # DB7
pi@raspberrypi ~ $ ^C
pi@raspberrypi ~ $ ^C
pi@raspberrypi ~ $



And here is a picture of my setup and what the lcd looks like if that helps anybody?:

Image

Image


Thanks alot :)
Mmmm....pie..... Mmmm..... pi
Posts: 19
Joined: Sat Sep 29, 2012 6:53 pm
by rickseiden » Sun Dec 09, 2012 7:43 pm
Those are just warnings. Add the code they specify:

Code: Select all
GPIO.setwarnings(False)


after your GPIO.setmode() command, and they will go away.
There are 10 types of people in this world. Those that understand binary, and those that don't.
Posts: 389
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA
by rickseiden » Sun Dec 09, 2012 7:46 pm
It's not possible to really diagnose the problem on the wiring from that picture. Can you get some close-ups?
There are 10 types of people in this world. Those that understand binary, and those that don't.
Posts: 389
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA
by strandedmotorist » Sun Dec 09, 2012 8:38 pm
rickseiden wrote:It's not possible to really diagnose the problem on the wiring from that picture. Can you get some close-ups?


Yeah, ill disable the warnings.
And, Sure, here you go:

Image

Image

Image

Image
Mmmm....pie..... Mmmm..... pi
Posts: 19
Joined: Sat Sep 29, 2012 6:53 pm
by rickseiden » Sun Dec 09, 2012 9:35 pm
It's hard to tell, but I think part of your problem is that it looks like you've got a data pin hooked up to ground.
There are 10 types of people in this world. Those that understand binary, and those that don't.
Posts: 389
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA
by strandedmotorist » Mon Dec 10, 2012 5:33 pm
rickseiden wrote:It's hard to tell, but I think part of your problem is that it looks like you've got a data pin hooked up to ground.


Ahh, ok, ill have a quick look at it and reply if i get any progress.
Mmmm....pie..... Mmmm..... pi
Posts: 19
Joined: Sat Sep 29, 2012 6:53 pm
by strandedmotorist » Mon Dec 10, 2012 8:14 pm
Ive had a look and i can only see that RW (readwrite), Ground, the backlight ground and ground for contract are the only connected to ground, which is correct according to the guide.

Im open to suggestions from anyone please!

Thanks!

(If an admin or mod sees this, could they please change the title to:
"Possible problem with LCD module"
Thanks!)
Mmmm....pie..... Mmmm..... pi
Posts: 19
Joined: Sat Sep 29, 2012 6:53 pm
by rickseiden » Mon Dec 10, 2012 8:38 pm
One last suggestion for you. Make sure you've got data 5-8 hooked up correctly. It's really hard to tell, but they might be hooked up data 8-5 instead. (The wire I thought was a data pin is clearly the ground for the backlight, and the data pin line I thought was missing is just snuggled up against a brown wire.)

If that doesn't work, I don't know what's going on. I find that tearing things down and building them back up makes all the difference sometimes. Other times, I find I make the same mistakes over and over again. :)
There are 10 types of people in this world. Those that understand binary, and those that don't.
Posts: 389
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA
by strandedmotorist » Mon Dec 10, 2012 10:07 pm
Yeah, thats true, i will tear it down and check and doubble check the connections tomarrow and see what that does for it.

Thanks for your help with this so far, i really appriciate it :)

Mike.
Mmmm....pie..... Mmmm..... pi
Posts: 19
Joined: Sat Sep 29, 2012 6:53 pm
by rickseiden » Mon Dec 10, 2012 10:31 pm
You're welcome, and good luck!
There are 10 types of people in this world. Those that understand binary, and those that don't.
Posts: 389
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA
by JakeWisniewski » Thu Aug 01, 2013 12:29 pm
Do you have the potentiometer connected to pin 3 of the LCD? I don't see it in your pictures. When I was setting up mine and I tried to just ground the pin nothing would show because the contrast was not adjusted correctly.

What I ended up doing was adding a 2.2k ohms resistor(red,red,red,gold) from pin 3 to ground and it starting working.

It's still not at the contrast level I would like but the best I could do quickly.
Posts: 42
Joined: Fri Feb 08, 2013 4:58 pm