Take a look at this more up-to-date post which uses a Python library to do the necessary.
I bought this display from Hobby Components:
It is very similar to the Sainsmart 2004 LCD display and the pin mappings are the same (it’s a J204A LCD board).
I came across this thread on the Foundation forum on which member ‘natbett’ gave out some code to drive the display. Fortunately, this code works (after trying out 4 other examples found elsewhere). Here is the code for posterity:
The first file is called ‘i2c_lib.py’
import smbus from time import * class i2c_device: def __init__(self, addr, port=1): self.addr = addr self.bus = smbus.SMBus(port) # Write a single command def write_cmd(self, cmd): self.bus.write_byte(self.addr, cmd) sleep(0.0001) # Write a command and argument def write_cmd_arg(self, cmd, data): self.bus.write_byte_data(self.addr, cmd, data) sleep(0.0001) # Write a block of data def write_block_data(self, cmd, data): self.bus.write_block_data(self.addr, cmd, data) sleep(0.0001) # Read a single byte def read(self): return self.bus.read_byte(self.addr) # Read def read_data(self, cmd): return self.bus.read_byte_data(self.addr, cmd) # Read a block of data def read_block_data(self, cmd): return self.bus.read_block_data(self.addr, cmd)
The second file is called lcddriver.py
import i2c_lib from time import * # LCD Address ADDRESS = 0x27 # commands LCD_CLEARDISPLAY = 0x01 LCD_RETURNHOME = 0x02 LCD_ENTRYMODESET = 0x04 LCD_DISPLAYCONTROL = 0x08 LCD_CURSORSHIFT = 0x10 LCD_FUNCTIONSET = 0x20 LCD_SETCGRAMADDR = 0x40 LCD_SETDDRAMADDR = 0x80 # flags for display entry mode LCD_ENTRYRIGHT = 0x00 LCD_ENTRYLEFT = 0x02 LCD_ENTRYSHIFTINCREMENT = 0x01 LCD_ENTRYSHIFTDECREMENT = 0x00 # flags for display on/off control LCD_DISPLAYON = 0x04 LCD_DISPLAYOFF = 0x00 LCD_CURSORON = 0x02 LCD_CURSOROFF = 0x00 LCD_BLINKON = 0x01 LCD_BLINKOFF = 0x00 # flags for display/cursor shift LCD_DISPLAYMOVE = 0x08 LCD_CURSORMOVE = 0x00 LCD_MOVERIGHT = 0x04 LCD_MOVELEFT = 0x00 # flags for function set LCD_8BITMODE = 0x10 LCD_4BITMODE = 0x00 LCD_2LINE = 0x08 LCD_1LINE = 0x00 LCD_5x10DOTS = 0x04 LCD_5x8DOTS = 0x00 # flags for backlight control LCD_BACKLIGHT = 0x08 LCD_NOBACKLIGHT = 0x00 En = 0b00000100 # Enable bit Rw = 0b00000010 # Read/Write bit Rs = 0b00000001 # Register select bit class lcd: #initializes objects and lcd def __init__(self): self.lcd_device = i2c_lib.i2c_device(ADDRESS) self.lcd_write(0x03) self.lcd_write(0x03) self.lcd_write(0x03) self.lcd_write(0x02) self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE) self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON) self.lcd_write(LCD_CLEARDISPLAY) self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT) sleep(0.2) # clocks EN to latch command def lcd_strobe(self, data): self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT) sleep(.0005) self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT)) sleep(.0001) def lcd_write_four_bits(self, data): self.lcd_device.write_cmd(data | LCD_BACKLIGHT) self.lcd_strobe(data) # write a command to lcd def lcd_write(self, cmd, mode=0): self.lcd_write_four_bits(mode | (cmd & 0xF0)) self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0)) # put string function def lcd_display_string(self, string, line): if line == 1: self.lcd_write(0x80) if line == 2: self.lcd_write(0xC0) if line == 3: self.lcd_write(0x94) if line == 4: self.lcd_write(0xD4) for char in string: self.lcd_write(ord(char), Rs) # clear lcd and set to home def lcd_clear(self): self.lcd_write(LCD_CLEARDISPLAY) self.lcd_write(LCD_RETURNHOME)
The last file, which is the example script, is called lcd.py
import lcddriver from time import * lcd = lcddriver.lcd() lcd.lcd_display_string("Hello world", 1) lcd.lcd_display_string("My name is", 2) lcd.lcd_display_string("picorder", 3) lcd.lcd_display_string("I am a Raspberry Pi", 4)
It displays 4 lines of text.
My next task is to connect this up to the Picorder version 3!
[…] recantha I bought this display from Hobby […]
pi@raspberrypi ~/projects/python/lcd $ python lcd.py
Traceback (most recent call last):
File “lcd.py”, line 4, in
lcd = lcddriver.lcd()
File “/home/pi/projects/python/lcd/lcddriver.py”, line 56, in __init__
self.lcd_device = i2c_lib.i2c_device(ADDRESS)
File “/home/pi/projects/python/lcd/i2c_lib.py”, line 7, in __init__
self.bus = smbus.SMBus(port)
IOError: [Errno 2] No such file or directory
pi@raspberrypi ~/projects/python/lcd $
i get the following error, can you share any insight?
In i2c_lib.py, change def __init__(self, addr, port=1) to def __init__(self, addr, port=0) and try again.
[…] I bought this display from Hobby Components: http://www.hobbycomponents.com/index.php?route=product/product&filter_name=lcd&product_id=285 It is very similar to the Sainsmart 2004 LCD displ… […]
I have a 20×4 I2C SAINSMART display which does want to not work with RPI.
I’have tried to power it with 5V (default voltage), 3.3V, convert I2C bus voltage with “4-channel I2C-safe Bi-directional Logic Level Converter – BSS138”, nothing.
It’s live with his address (0x3f) but using different library nothing compare on display.
Using raspbian wheezy 23-09-25 model B 512mb. Using LCD 20×4
Traceback (most recent call last):
File “lcd.py”, line 1, in
import lcddriver
File “/home/pi/lcddriver.py”, line 1, in
import i2c_lib
ImportError: No module named i2c_lib
Ic2_lib.py still in home/pi directory.
Can you help me?
Never mind! I got lcd 20×4 working. I mistyped wrong filename ic2_lib.py instead of i2c-lib.py
I;m very happy that my lcd 20×4 working.
Thank, Michael Horne for posting source code.
Many thanks!
How do you clear all lines?
lcd = lcddriver.lcd()
lcd.lcd_display_string(“Hello world”, 1)
lcd.lcd_display_string(“My name is Supra”, 2)
lcd.lcd_display_string(“picorder”, 3)
lcd.lcd_display_string(“I am a Raspberry Pi”, 4)
sleep(5)
lcd.lcd_clear
while True:
distance = display_measure()
lcd.lcd_display_string(“Distance : %.1f cm” % distance, 2)
Never mind. I solved problem by adding brace bracket.
lcd.lcd_clear()
Many thanks!
We wish you A Merry Christmas and a Happy New Year to You.
i have raspberry pi v2(512MB) and LCM1602 (LCD1602 with I2c controler ) like http://www.ebay.com/itm/1PCS-IIC-I2C-TWI-1602-Serial-LCD-Module-Display-for-Arduino-/251141025928?pt=LH_DefaultDomain_0&hash=item3a792bf088
Work Perfect!
Then You!
hello I’m trying to turn on and off the LCD light with these commands you, but wanted a python function to do, you know?
pi@RaspFishServ ~/lcd $ sudo i2cset -y 1 0x27 0x08 0x00 – OFF
pi@RaspFishServ ~/lcd $ sudo i2cset -y 1 0x27 0x08 0x08 – ON
Take a look example how to turn off backlight. U will have to remove jumper.
http://www.raspberrypi-spy.co.uk/2012/08/20×4-lcd-module-control-using-python/
hi fish, using the above code as an example i added a function to lcddriver.py:
def backlight(self, state): # for state, 1 = on, 0 = off
if state == 1:
self.lcd_device.write_cmd(LCD_BACKLIGHT)
elif state == 0:
self.lcd_device.write_cmd(LCD_NOBACKLIGHT)
then in lcd.py just put “lcd.backlight(1)” to turn it on, an use 0 to turn it off
cheers,
Nice addition, thanks!
Does that method require the jumper to be removed? because i tried it both ways and it did not toggle the backlight.
Hi, just popping in to say thanks, I mean, thanks a lot !
I had this page bookmarked for quite a while, and today I finally connected my 20×4 to my raspi : everything worked straight away using your code, and the comments are really useful too.
Thanks for making this available !
You’re very welcome 🙂
thank you,
i have modified lcd.py to read text from file, but i am not good in python….
import lcddriver
from time import *
lcd = lcddriver.lcd()
f = open(‘text.txt’, ‘r’)
mystring = f.readline()
mystring = mystring.rstrip(‘n’)
lcd.lcd_display_string(mystring, 1)
mystring = f.readline()
mystring = mystring.rstrip(‘n’)
lcd.lcd_display_string(mystring, 2)
mystring = f.readline()
mystring = mystring.rstrip(‘n’)
lcd.lcd_display_string(mystring, 3)
mystring = f.readline()
mystring = mystring.rstrip(‘n’)
lcd.lcd_display_string(mystring, 4)
f.close()
Hi again,
I have made a few tweaks to lcddriver.py, mostly cosmetic.
There is one addition that may be of use to others though : the ability to control the backlight when writing to the screen.
The code is available here, feel free to pilfer 🙂
http://pastebin.fr/32576
Here is tutorial link:
http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
Btw, thank for backlight function. That was one line of code.
Hey, good looking cosmetic changes there. You’ll have to tell me the new code for printing to screen now though, because display_string(“Initializing…”,1) doesn’t work any more.
Many Thanks For the above code i got it to display no problem
How do i get it to display time and date 🙂
Thanks again
Simon
Thanks Supra for posting the answer 🙂
What is the answer?
After THREE DAYS of trying every python script that Google could find, your example above worked!!!
Thank you for now giving me the inspiration to keep moving toward my goal! It’s nice when things finally work.
You’re welcome – glad it worked 🙂
Many Thanks i have posted comments on the You Tube Site will Try again this weekend 🙂
recantha, thanks for driver – it really works.
Is there a way to get some features from Adafruit_CharLCD like positioning cursor before printing, turning underline cursor on/off, (auto)scrolling? Actually I’d like to use a simple 20×4 in Scratch though Pridopia (children, you know) – but they use Adafruit’s library which does not suit for simple displays. Maybe you suggest your library to Adafruit and/or Pridopia?
Any news on this?
hello Sir,
thank you for such nice tutorials.
I am completely noobs in Raspi and electronics 🙁 ; I have a question
I soldered Adafruit 16×2 LCD -I2C on Raspi and followed tutorial and works nicely ;
now I want to blink a single LED and show the count on LCD(I2C).. so I soldered LED on GPIO pins which are resurrecting from LCD …..and now nothing works???
so what i did wrong?
was soldering LED on GPIO while using I2C LCD is wrong ??? or i need some libraries to get it work?(i.e. I2c LCD and LED input)?
Please help!
Can you take a picture of what you’ve done and email it to mike@recantha.co.uk ?
Take a few pics from different angles so I can see what you’ve done.
Thank you,
Your post saved me a lot of time to get my LCD working.
I know have a functioning Feathercoin ‘ticker’
You can see the details of it here.
https://forum.feathercoin.com/index.php?/topic/7412-raspbery-pi-based-ftc-ticker/
Thanks for this! I can confirm it works on my I2C LCD (same as yours), running with RPi and Wolfson audio card…
But, is there any way to add autoscrolling on long lines?
Denis
I managed to cobble up a text scroller from various snippets around the net, which works withthe above library (including the backlight command…).
Here’s my line scroller – this one refers to line 1 of the LCD:
====> start of code
import lcddriver
from time import *
lcd = lcddriver.lcd()
str_pad = ” ” * 20
my_long_string = “this is a long string that needs to scroll”
my_long_string = my_long_string + ” ” # add space at the end
lcd.lcd_display_string(my_long_string[:19], 1) # initially show just the first part
for i in range (0, len(my_long_string)):
lcd_text = my_long_string[i:(i+19)]
lcd.lcd_display_string(lcd_text,1)
sleep(0.2)
lcd.lcd_display_string(str_pad,1)
lcd.lcd_display_string(my_long_string[:19], 1)
sleep(2)
lcd.lcd_clear()
lcd.backlight(0)
===> end of code
Hope this saves some time to others who might find this page 🙂
Regards
Denis
Hi Denis. Can you send me your code to my email about scroll ? i copied it, but works in part. I think that is something about python Indentation.
Thanks
No!. You don’t needed to do indentation.
Just copy and paste it or type manual.
Just a word to thank You, works on a eBay i2c LCD 16×2, just had to change address from 0x27 to 0x20 🙂
Thanks for this code. But some questions.
I have some basic knowledge in coding.
I tried the code from Matt to controll the Backligth ist works.
But when i change the LCD-Command to blinking courser or someting else, the Backlight also switch of.
I use this LCD http://www.ebay.de/itm/IIC-I2C-TWI-2004-Serial-Blue-LCD-Module-For-Arduino-und-Raspberry-Pi-CP02015-J72/281353509316?_trksid=p2045573.c100033.m2042&_trkparms=aid%3D111001%26algo%3DREC.SEED%26ao%3D1%26asc%3D20131017132637%26meid%3D8707713610718416030%26pid%3D100033%26prg%3D20131017132637%26rk%3D1%26rkt%3D4%26sd%3D281353509316 with out an levelshifter for the i2c.
I dont can imagine how to work with the code, i really confused. Have somebody any additional examples?
Thanks
thanks for the code, but some questions.
i tried several display commands e.g. LCD_CURSORON.
And it is ever the same result, the backlight swtichs off.
I have also seen there is the 0x00 value. How should this work for MOVELEFT, NOBACKLIGHT?
I tried this:
def cursor(self, state):
if state == 1:
self.lcd_device.write_cmd(LCD_CURSORON)
elif state == 0:
self.lcd_device.write_cmd(LCD_CURSOROFF)
I use the Pi B+ with an 20×4 I2C LCD with out an Levelshifter.
Thanks
Hi,
i tested your python scripts. Working fine.
i founded some MPD scripts https://github.com/Mic92/python-mpd2/tree/master/examples. How can i make MPD state output to my LCD.
greets
You just need to get the output and push it to the LCD functions. Not sure how I can help.
What code i need add to lcddriver.py so was possible do scrolling and write (x,y,line)
Someone have a updated version of it ?
I have a 20×4 display working with this code already.
Thanks
Look up above @Denis
Thaks alot for this script! It works fine.
Can you help me with an script for C?
My main program is in C and I do:
system(“sudo pyhton lcd.py -t text”)
to use the LCD with my own text.
Do you know a runnig script for this LCD?
Thanks!
I don’t I’m afraid – I’m not very good at C. Try the Raspberry Pi Foundation forum – there’s plenty of bright people on there who might be able to help 🙂
Hi
i try to get my SaintSmart 20×4 LCD to work.
I have activated the modules and installed smbus and i2c-tools.
When i do #i2cdetect -y 1 i got:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: — — — — — — — — — — — — —
10: — — — — — — — — — — — — — — — —
20: — — — — — — — — — — — — — — — —
30: — — — — — — — — — — — UU — — — —
40: — — — — — — — — — — — — — — — —
50: — — — — — — — — — — — — — — — —
60: — — — — — — — — — — — — — — — —
70: — — — — — — — —
When i start lcd.py i got the following error:
Traceback (most recent call last):
File “./lcd.py”, line 4, in
lcd = lcddriver.lcd()
File “/home/pi/i2c/lcddriver.py”, line 58, in __init__
self.lcd_write(0x03)
File “/home/pi/i2c/lcddriver.py”, line 82, in lcd_write
self.lcd_write_four_bits(mode | (cmd & 0xF0))
File “/home/pi/i2c/lcddriver.py”, line 77, in lcd_write_four_bits
self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
File “/home/pi/i2c/i2c_lib.py”, line 11, in write_cmd
self.bus.write_byte(self.addr, cmd)
IOError: [Errno 5] Input/output error
Need help
THX
Did you added sudo apt-get install python-smbus?
Thanks for the driver! Works fine. I want to share python code for printing the alphabet of the lcd, I know there are ‘asian’ and ‘normal’. I have the asian one. So you can see and pick up symbols you like for later use with lcd.write(number of symbol, Rs). Im using only two first lines becouse i have only two. if you have only one then remove section for second line.
# -*- coding: utf-8 -*-
import lcddriver
Rs = 0b00000001 # Register select bit
lcd = lcddriver.lcd()
#Print 1602 lcd alphabet, start from position:
x=32
#End at:
while (x < 256):
# There can be 'pages' of empty addresses
# so keep hitting enter.
# Wait for enter-key:
raw_input("Press Enter")
start=x
#First line
lcd.lcd_write(0x80)
while (x < (start+16)):
lcd.lcd_write(x, Rs)
x=x+1
#Second
lcd.lcd_write(0xC0)
while (x < (start+32)):
lcd.lcd_write(x, Rs)
x=x+1
#Show numbers of symbols
print start, "-" ,x-1
sorry, indentation missing.
Thanks Samuel! 🙂
I was wondering if it would this could be easily modified for SPI and a LCD12864? i have one of these laying around being unused. http://www.dfrobot.com/wiki/index.php/3-wire_Serial_LCD_Module_%28Arduino_Compatible%29_%28SKU:DFR0091%29
I have the same problem as the user Marcel Ro. i get the following errors
Traceback (most recent call last):
File “lcd.py”, line 4, in
lcd = lcddriver.lcd()
File “/home/pi/lcddriver.py”, line 58, in __init__
self.lcd_write(0x03)
File “/home/pi/lcddriver.py”, line 82, in lcd_write
self.lcd_write_four_bits(mode | (cmd & 0xF0))
File “/home/pi/lcddriver.py”, line 77, in lcd_write_four_bits
self.lcd_device.write_cmd(data | LCD_BACKLIGHT)
File “/home/pi/i2c_lib.py”, line 11, in write_cmd
self.bus.write_byte(self.addr, cmd)
IOError: [Errno 5] Input/output error
i have installed python-smbus, what else can be wrong?
Sorry! I cannot recalled how How I did it(year ago). But I’m using python 3 or later.
How do you connect the display? I have returned my LCD to the seller, but a few days later i found a comment at Amazon where somebody wrote, that he has to connect the display to 3,3v power at the Pi and not to 5v and the display works. Conected to 5V it won’t.
He probably used extra I2C chip that connected to 3.3V Pi.
But what I did. I connected LCD to external power supply(5V). And that ground connected to both PI and power supply. So I merely used I2C with lcd to 5V.
@ Eric and Marcel Ro.
Can you delete function?
def write_cmd(self, cmd):
self.bus.write_byte(self.addr, cmd)
sleep(0.0001)
After you deleted it. Just type manual on keyboard, but do not copy and paste it. But same indentation.
Hope that help!
[…] I2C 20×4 LCD character display on a RaspberryPi […]
[…] Download Bereich liegen die Skripte die ich vom Blog (externer Link: http://www.recantha.co.uk) habe. Das Skript hat sofort funktioniert, vorausgesetzt man weiß das die Programmstruktur eines […]
Hi,
Has anyone written a function to make custom characters for this library yet?
I’m trying to do something about it, so if something already exists, there’s no point in reinventing the wheel 🙂
Rgds,
Denis
OK, again I did everything myself 🙂
But, since copy-paste from this blog leaves a lot to be desired, I’ve put the new library on github/gists, plus one example Python script, which will make it easier to understand and use the custom character function.
Again, 90% of it all was lifted from other web sites and publicly accessible code, so I really can’t take much credit for this, except for making it publicly available and writing a few comments and some examples.
Hope this helps others!
The new, modified library, which combines i2c_lib.py and lcddriver.py into a single library/file, is called “RPi_I2C_driver.py”, and there’s also an example of what can be done with it.
I’ll probably also add some scrolling examples, while I’m at it 🙂
The URL is:
https://gist.github.com/DenisFromHR/cc863375a6e19dce359d
Thank, Denis. I tested it today under new version of wheezy
Supra, does it work OK?
Yes! It is working now
Some picture for lcd, you can modify it…
pi@raspberrypi ~ $ cat ./bin/startscript.sh
#!/bin/bash
verzia=$(uname -r)
cat > /home/pi/lcd/text.txt <
/ )\ Raspberry Pi
,/_/_/ $verzia
EOL
cd /home/pi/lcd
sudo python lcd.py
cd – >/dev/null
pi@raspberrypi ~ $ uname -r
3.18.7+
pi@raspberrypi ~ $
I wonder if anyone can help when a execute the file a get this error.
sudo python lcd.py
Traceback (most recent call last):
File “lcd.py”, line 1, in
import lcddriver
File “/home/pi/lcddriver.py”, line 1, in
import i2c_lib
File “/home/pi/i2c_lib.py”, line 35
Is that the WHOLE error? Seems to be missing what the actual error was.
Really good tuto.
For some reason I can barely see the text on the lcd and my lcd works fine on with my arduino…
Any ideas?
Hi Sandrine. Is the backlight definitely on?
Sorry for bothering you with this.
Your question help me in a strange way. I try my lcd on my arduino just to make sure it was properly working and it was the case. So I reconnected my lcd to my pi and then I realized it was connected to my 3.3v pin instead of 5v… And now everything works fine.
Ahhh. Yes, that’ll be it 🙂
Sandrine,
Easy way for you. You don’t have to test in Arduino
You can test:
sudo i2cdetect -y 1
This tell you when you see address—meaning your lcd is working
Hello,
Thanks for your help. Your script worked on my project. I can put static words to my lcd but i couldnt connect it to lcdproc or mpdlcd. How can i get nowplaying info on my screen. I guess i have to edit something on LCDd.conf file.
I hope someone help me
You can google on search or youtube. There are tons of codes online.
When i run lcdproc my screen’s backligt flicker. But no text on screen. only light switches on and of. no info shown on screen.
my LCDd.conf
Driver=hd44780
Port=0x27
ConnectionType=i2c
Backlight=yes
Size=20×4
thanks.
Did you do sudo i2cdetect -y 1?
Did you do enable i2c?
Yes i enabled i2c and i have executed your lcd.py script succesfully. It displayed Hello world without any problem. I can edit your lcd.py file and could display any static text on my lcd. but cant make any dynamic text like lcdproc and mpdlcd script output. like i said before its backlight turns on and off again and again and no words shown on screen. my only goal is display mpd info on my screen
Did u upgrade mplcd 0.4.3?
did u install lcdproc and mplcd?
U don’t needed to do lcdproc.conf.
U will have to edit /etc/mpdlcd.conf
One question.
Did u downloaded from github source code?
Not too sure what you mean.
If mecaz downloaded from github. Probably will have to used i2C library in order to get mpdlcd and lcdproc working
Mecaz,
It is my misunderstood.
You cannot used lcddriver and i2c_lib. And also you cannot do backlight function. Both of them will not work for mplcd.
You will have to used another I2C lib and lcd driver from github’s link.
Here is link:
https://github.com/speendo/raspi-mpd-lcd
Hope that help
Hi Supra
Thanks for the link!!
I installed the software and the library and now I’m stuck with the following error:
Traceback (most recent call last):
File "main.py", line 6, in
from LineController import *
File "/home/pi/raspi-mpd-lcd/LineController.py", line 184, in class MPDLine(TextLine):
File "/home/pi/raspi-mpd-lcd/LineController.py", line 185, in MPDLine from mpd import MPDClient
ImportError: No module named 'mpd'
Have you any idea on how I can bypass this?
Thanks again
WOW! This helped me sooo much! I’ve been all over looking for this solution, Thanks!
You’re welcome
I’ve tried everything and just can’t get this working. I have a RPi2 and 20×4 LCD with I2C converter on the board. It’s connected to 5v and the backlight is on – so power is getting through.
Whatever I try the display stays completely blank. I can’t get anything to display. I2C is enabled and address of board is hex 3f so it’s seen my the RPi.
I’m completely stuck now … any help would be very much appreciated.
Try twiddling the trimming potentiometer on the back – it might be working but not turned up enough for the text to display.
Michael, you like a God! Thank for the advice! I’ve spend around a two hours for choose i2c lib for my 16×2 screen. And I’ve fix it with screwdriver! Thanks!
You’re welcome 🙂
Just had the exact same problem… fixed with a simple turn of the screwdriver. Thank you!
Haha. I’m such a dork.
Michael – you, sir, are a genius. Who’d have thought that it would be the simple thing stopping it working. I tried every complicated way imaginable and it was the bl&*dy brightness setting !
Thank you very much. Off to start again with Jessie now – which will no doubt stop it working 😉
Greetings.
So, I got the LCD working and even displaying temperatures from my 2 probes, complete with a degree symbol. Whoopee.
Thought it might be fun to indicate whether the temp was going up or down so I made a couple of arrows. But I can’t get them to display after the temp reading.
I’m defining them then applying to upA and downA : upA = unichr(0) etc.
Temp is sent to LCD like this :
lcd.lcd_display_string(‘Desk: %s %sC ‘ % (temp_1,degree), 2)
But where, and how, do I add my upA bit. Nothing seems to work.
lcd.lcd_display_string(‘Desk: %s %sC %s’ % (temp_1,degree, upA), 2)
It’s a string so why doesn’t the above line work ?
Another silly error on my part most like. Any thoughts anyone ?
cd.lcd_display_string(‘Desk: %s %sC %s’ % (temp_1,degree,), upA,2)
My mistaken.
It should be like this:
lcd.lcd_display_string(‘Desk: %s %sC %s’ % (temp_1,degree), upA,2)
Has anyone gotten cursor to work ? I can’t figure out how to turn it on or move it to the desired position.
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_CURSORMOVE = 0x00
Scroll up to see example of how to turn backlight off
U should do in search engine
Hi, could someone help me please… Im trying to get this to work with a PCF8574 and a 16×2 lcd. I can see the i2c address with i2cdetect, but when I run the script, nothing happens. Ive connected the lcd like in this article: http://www.rpiblog.com/2012/07/interfacing-16×2-lcd-with-raspberry-pi.html
it works using his code, but I want to use this because there are more functions
Is your lcd has a black square?
Hi! Thanks for sharing this code, works great! What would be the easiest way to adjust backlight strength programatically? It’s easily done with a resistor, but could it be done via PWM?
Used potentiometer. If u know value from potentiometer, then u can used resistor with 5 bands resistor
What code do I need in my script to get the cursor to turn on/off. Tried supras comment but cant get it to work ??
U will have to write own procedure event in lcddriver library
for example: How to turn off backlight. The code was written by fpp. See above february 2nd, 2014
# turn backlight on and off
def backlight(self, state): # state: 1 = on, 0 = off
self.lcd_device.write_cmd( (LCD_NOBACKLIGHT,LCD_BACKLIGHT)[state])
I haven’t tried it yet, because I’m getting new one raspberry pi 3.
me sirvio el ejemplo que tengo para llamar el lcd.backlight(0) es:
import lcddriver
from time import *
import i2c_lib
lcd = lcddriver.lcd()
lcd.lcd_clear()
lcd.lcd_display_string(“*******************”,1)
lcd.lcd_display_string(“*Control de Acceso*”,2)
lcd.lcd_display_string(“* Iniciando… *”,3)
lcd.lcd_display_string(“*******************”,4)
sleep(5)
lcd.lcd_clear()
lcd.backlight(0)
lcd.lcd_display_string(“*******************”,1)
lcd.lcd_display_string(“* Se ah iniciado *”,2)
lcd.lcd_display_string(“* el sistema *”,3)
lcd.lcd_display_string(“*******************”,4)
sleep(2)
lcd.lcd_clear()
lcd.backlight(0)
esto es en raspberry pi 2
ingresar las lineas en el archivo lcddriver.py
# turn backlight on and off
def backlight(self, state): # state:1=on, 0=off
self.lcd_device.write_cmd((LCD_NOBACKLIGHT,LCD_BACKLIGHT)[state])
thanks users fpp and supra
Hello again, my question is how to enter the command to move the text from left to right or vice versa
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00
as the income and I do not see a pacman wanted to do work that was charging indicator system , so to speak
Added 4 buttons: up, down left and right. Or u can use joystick or whatever u can.
I haven’t attempt yet, because of moving to raspberry pi 3.
I want the text will be displayed side to side , but commands can not drive
I’m using 20×4 lcd.
Absolutely, commands can not drive.
U needed to do this with extra spaces
For instance:
lcd.lcd_display_string(” RASPBIAN JESSIE”, 1). Depended number of characters.
Right now I’m setting up for RPI 3.
I am also using LCD 20 x 4 , thanks for my question 🙂
i am looking for help i can not get my LCD to work. i have taken the code and pasted it into python 3 but i get this error…..
>>>
Traceback (most recent call last):
File “/home/pi/RPi_I2C_driver.py”, line 1, in
import smbus
ImportError: No module named ‘smbus’
>>>
if anyone could help me that would be great. thank you.
Have you tried it in Python 2?
You should also take a look at installing support for I2C. Try sudo apt-get install i2c-tools
Also sudo apt-get install python3-smbus to install it for Python 3
Did u enabled i2C? Smbus is already installed too.
Hello
Happy new year
With your code I have got my LCD running and showing pre defined strings, thank you.
But l have failed to get it to display the value of a variable ‘latitude’, usually getting an error about not enough arguments for format string.
My script contains
print latitude
And the value of latitude from gpsd is displayed in the terminal window correctly.
But how do I get it to display on the LCD screen please?
Hi Friend,
I would like to do text movement on LCD 20×4.
Can anyone help to show me how to write the codes?
Appreciate in advance.
For example : test move from left to right in case text is longer that 20 digits.
[…] display was made using a module for the LCD display. The code for this module can be found here: https://www.recantha.co.uk/blog/?p=4849 . The clock was done by taking a section of code from the alarm that is used to compare the time […]
how can I display the temperature from a sensor?
Take a look at this updated post: https://www.recantha.co.uk/blog/?p=18467