mrdrake
Posts: 4
Joined: Fri Jan 18, 2013 5:29 am

Whats wrong with my LCD code?

Fri Jan 18, 2013 5:32 am

Code: Select all

from Adafruit_CharLCD import Adafruit_CharLCD

lcd = Adafruit_CharLCD()

lcd.begin(16,1)

lcd.clear()

lcd.message("hello world")
I'm bloody stumped.

The Adafruit example works though so I have no idea whats going on.

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Whats wrong with my LCD code?

Fri Jan 18, 2013 1:59 pm

Are you getting an error message when you attempt to run or no display?

First thing I noticed is the 'lcd.begin(16,1)'. This says you are using a 16 character by 1 line display. Is this correct? The typical display is a 16x2 display, which would need a 'lcd.begin(16,2)' line.

Are you running like this (assuming lcdhello.py is your program name)?

Code: Select all

sudo python lcdhello.py
If you want to run directly, you need to do add the '#!/usr/bin/python' line at the beginning:

Code: Select all

#!/usr/bin/python

from Adafruit_CharLCD import Adafruit_CharLCD
from RPi import GPIO

lcd = Adafruit_CharLCD()

lcd.begin(16,1)

lcd.clear()

lcd.message("hello world")

GPIO.cleanup()
I also added the 'GPIO.cleanup()' to eliminate the warnings on subsequent runs.

And then make it executable using:

Code: Select all

chmod +x lcdhello.py
Then you can run it using:

Code: Select all

sudo ./lcdhello.py
Enjoy!
Bill

mrdrake
Posts: 4
Joined: Fri Jan 18, 2013 5:29 am

Re: Whats wrong with my LCD code?

Sat Jan 19, 2013 4:43 am

Code: Select all

from: can't read /var/mail/Adafruit_CharLCD
from: can't read /var/mail/time
./testing.py: 5: ./testing.py: Syntax error: "(" unexpected (expecting "do")
This came up when I copied your code and then made it executable. I'm really confused right now, just changing the Adafruit example makes it not work at all. And by not work, it runs perfectly, shows no errors on the terminal, but nothing shows up on the LCD. I know its not the wiring because both of the Adafruit examples work perfectly when I use them without changing them. Grrr....

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

Re: Whats wrong with my LCD code?

Sat Jan 19, 2013 7:53 am

Please the full code that works and the full code that doesn't work.
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
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: Whats wrong with my LCD code?

Sat Jan 19, 2013 1:07 pm

Yes, please post the code.

Also, what editor are you using for modifying the code? Seems that the save code source is being corrupted. The files you are attempting to execute must be 'plain text' without any formatting added by a word processing style application. If you are editing on another computer and transferring to the Raspberry Pi (seems possible since you have /var/mail listed in the file path), you must use a program that stores as plain text.

Enjoy!
Bill

mrdrake
Posts: 4
Joined: Fri Jan 18, 2013 5:29 am

Re: Whats wrong with my LCD code?

Sun Jan 20, 2013 4:50 am

I'm using Sublime Text 2. I've never had a problem with it, Python, and the raspberry pi before. I'm transferring it with Cyberduck through SFTP (also never had a problem).

This works (from their repo)

Code: Select all

#!/usr/bin/python

from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import * 
from time import sleep, strftime
from datetime import datetime

lcd = Adafruit_CharLCD()

cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"

lcd.begin(16,1)

def run_cmd(cmd):
        p = Popen(cmd, shell=True, stdout=PIPE)
        output = p.communicate()[0]
        return output

while 1:
	lcd.clear()
	ipaddr = run_cmd(cmd)
	lcd.message(datetime.now().strftime('%b %d  %H:%M:%S\n'))
	lcd.message('IP %s' % ( ipaddr ) )
	sleep(2)
This doesn't

Code: Select all

#!/usr/bin/python

from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import * 
from time import sleep, strftime
from datetime import datetime

lcd = Adafruit_CharLCD()

cmd = "ip addr show wlan0 | grep inet | awk '{print $2}' | cut -d/ -f1"

lcd.begin(16,1)

def run_cmd(cmd):
        p = Popen(cmd, shell=True, stdout=PIPE)
        output = p.communicate()[0]
        return output

while 1:
	lcd.clear()
	ipaddr = run_cmd(cmd)
	lcd.message(datetime.now().strftime('%b %d  %H:%M:%S\n'))
	lcd.message('IP')
	sleep(2)
The only thing I changed was taking away the IP address at the end of the second line. Adafruit_CharLCD.py is cloned from github and is the same for both (excepting changing the pin numbers to match Rev. 2 raspberry pi)

mrdrake
Posts: 4
Joined: Fri Jan 18, 2013 5:29 am

Re: Whats wrong with my LCD code?

Sun Jan 20, 2013 4:57 am

I hadn't changed the 3rd pin from 21 to 27. Thank you for your help, how do I go about getting this thread closed?

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

Re: Whats wrong with my LCD code?

Sun Jan 20, 2013 8:00 am

Glad you got it sorted. You don't have to close the thread.
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”