Skip to content

Instantly share code, notes, and snippets.

@DenisFromHR
Last active December 1, 2023 21:05
Show Gist options
  • Save DenisFromHR/cc863375a6e19dce359d to your computer and use it in GitHub Desktop.
Save DenisFromHR/cc863375a6e19dce359d to your computer and use it in GitHub Desktop.
RaspberryPi I2C LCD Python stuff
# requires RPi_I2C_driver.py
import RPi_I2C_driver
from time import *
mylcd = RPi_I2C_driver.lcd()
# test 2
mylcd.lcd_display_string("RPi I2C test", 1)
mylcd.lcd_display_string(" Custom chars", 2)
sleep(2) # 2 sec delay
mylcd.lcd_clear()
# let's define a custom icon, consisting of 6 individual characters
# 3 chars in the first row and 3 chars in the second row
fontdata1 = [
# Char 0 - Upper-left
[ 0x00, 0x00, 0x03, 0x04, 0x08, 0x19, 0x11, 0x10 ],
# Char 1 - Upper-middle
[ 0x00, 0x1F, 0x00, 0x00, 0x00, 0x11, 0x11, 0x00 ],
# Char 2 - Upper-right
[ 0x00, 0x00, 0x18, 0x04, 0x02, 0x13, 0x11, 0x01 ],
# Char 3 - Lower-left
[ 0x12, 0x13, 0x1b, 0x09, 0x04, 0x03, 0x00, 0x00 ],
# Char 4 - Lower-middle
[ 0x00, 0x11, 0x1f, 0x1f, 0x0e, 0x00, 0x1F, 0x00 ],
# Char 5 - Lower-right
[ 0x09, 0x19, 0x1b, 0x12, 0x04, 0x18, 0x00, 0x00 ],
# Char 6 - my test
[ 0x1f,0x0,0x4,0xe,0x0,0x1f,0x1f,0x1f],
]
# Load logo chars (fontdata1)
mylcd.lcd_load_custom_chars(fontdata1)
# Write first three chars to row 1 directly
mylcd.lcd_write(0x80)
mylcd.lcd_write_char(0)
mylcd.lcd_write_char(1)
mylcd.lcd_write_char(2)
# Write next three chars to row 2 directly
mylcd.lcd_write(0xC0)
mylcd.lcd_write_char(3)
mylcd.lcd_write_char(4)
mylcd.lcd_write_char(5)
sleep(2)
mylcd.lcd_clear()
mylcd.lcd_display_string_pos("Testing",1,1) # row 1, column 1
sleep(1)
mylcd.lcd_display_string_pos("Testing",2,3) # row 2, column 3
sleep(1)
mylcd.lcd_clear()
# Now let's define some more custom characters
fontdata2 = [
# Char 0 - left arrow
[ 0x1,0x3,0x7,0xf,0xf,0x7,0x3,0x1 ],
# Char 1 - left one bar
[ 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 ],
# Char 2 - left two bars
[ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 ],
# Char 3 - left 3 bars
[ 0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c,0x1c ],
# Char 4 - left 4 bars
[ 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e ],
# Char 5 - left start
[ 0x0,0x1,0x3,0x7,0xf,0x1f,0x1f,0x1f ],
# Char 6 -
# [ ],
]
# Load logo chars from the second set
mylcd.lcd_load_custom_chars(fontdata2)
block = chr(255) # block character, built-in
# display two blocks in columns 5 and 6 (i.e. AFTER pos. 4) in row 1
# first draw two blocks on 5th column (cols 5 and 6), starts from 0
mylcd.lcd_display_string_pos(block * 2,1,4)
#
pauza = 0.2 # define duration of sleep(x)
#
# now draw cust. chars starting from col. 7 (pos. 6)
pos = 6
mylcd.lcd_display_string_pos(unichr(1),1,6)
sleep(pauza)
mylcd.lcd_display_string_pos(unichr(2),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(unichr(3),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(unichr(4),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(block,1,pos)
sleep(pauza)
# and another one, same as above, 1 char-space to the right
pos = pos +1 # increase column by one
mylcd.lcd_display_string_pos(unichr(1),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(unichr(2),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(unichr(3),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(unichr(4),1,pos)
sleep(pauza)
mylcd.lcd_display_string_pos(block,1,pos)
sleep(pauza)
#
# now again load first set of custom chars - smiley
mylcd.lcd_load_custom_chars(fontdata1)
mylcd.lcd_display_string_pos(unichr(0),1,9)
mylcd.lcd_display_string_pos(unichr(1),1,10)
mylcd.lcd_display_string_pos(unichr(2),1,11)
mylcd.lcd_display_string_pos(unichr(3),2,9)
mylcd.lcd_display_string_pos(unichr(4),2,10)
mylcd.lcd_display_string_pos(unichr(5),2,11)
sleep(2)
mylcd.lcd_clear()
sleep(1)
mylcd.backlight(0)
# -*- coding: utf-8 -*-
"""
Compiled, mashed and generally mutilated 2014-2015 by Denis Pleic
Made available under GNU GENERAL PUBLIC LICENSE
# Modified Python I2C library for Raspberry Pi
# as found on http://www.recantha.co.uk/blog/?p=4849
# Joined existing 'i2c_lib.py' and 'lcddriver.py' into a single library
# added bits and pieces from various sources
# By DenisFromHR (Denis Pleic)
# 2015-02-10, ver 0.1
"""
#
#
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)
# 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_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))
# write a character to lcd (or character rom) 0x09: backlight | RS=DR<
# works!
def lcd_write_char(self, charvalue, mode=1):
self.lcd_write_four_bits(mode | (charvalue & 0xF0))
self.lcd_write_four_bits(mode | ((charvalue << 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)
# define backlight on/off (lcd.backlight(1); off= lcd.backlight(0)
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)
# add custom characters (0 - 7)
def lcd_load_custom_chars(self, fontdata):
self.lcd_write(0x40);
for char in fontdata:
for line in char:
self.lcd_write_char(line)
# define precise positioning (addition from the forum)
def lcd_display_string_pos(self, string, line, pos):
if line == 1:
pos_new = pos
elif line == 2:
pos_new = 0x40 + pos
elif line == 3:
pos_new = 0x14 + pos
elif line == 4:
pos_new = 0x54 + pos
self.lcd_write(0x80 + pos_new)
for char in string:
self.lcd_write(ord(char), Rs)
@dentex
Copy link

dentex commented Dec 17, 2015

Thanks @DenisFromHR and @gknauf for your efforts. :)
Thumbs up!

@smit468
Copy link

smit468 commented Dec 30, 2015

Hi, thanks for this it looks promising but I can't get it to work... I'm using a 16x2 HD44780 LCD connected to a PCF8574N chip, wired to the RPi 2 via I2C (stand alone chip, not a back pack) wired following this tutorial: http://www.rpiblog.com/2012/07/interfacing-16x2-lcd-with-raspberry-pi.html. My problem is when I run examples.py, the program runs without errors, but nothing displays on the LCD. I have changed the address in the library. Is there anything else I need to change in the library or example program for my specific set up? Any help would really be appreciated!

@marsel60
Copy link

marsel60 commented Jan 9, 2016

Hello, first of all, thanks Denis for your job... I'm very sorry for this silly question but I'm not able to install your library in my Raspi. Downloaded the library in my RPi and tried to install using "pip install RPi_I2C_driver.py".... I obtain a bunch of errors and library wont install.
Any Hints? please
I have a Jessy distro with Linux 4.1.15-v7+ , consider that I'v connected and used positively an LCD 20x4 with MCP23017 on my RPi.
Thanks in advance guys.

@flamingm0e
Copy link

Thanks, Denis. Great implementation.

Copy link

ghost commented Mar 28, 2016

I could not write on a 16x2 display with SPLC780C chip, apareece only strange characters, any tips ?

@shadowfxd
Copy link

I can't get mylcd.backlight(0) to work with my i2c LCD. Any ideas?

@raphaelyancey
Copy link

raphaelyancey commented Sep 7, 2016

It works like a charm on my RPi B. Thank you very much!

@NickTulett
Copy link

Can you only have 8 custom characters? I defined 9 but char(0) then displayed the same symbol as char(8)

@NickTulett
Copy link

@darnokb
Copy link

darnokb commented Oct 30, 2016

I have following problem:

root@OpenWrt:~/lcd2# python examples.py Traceback (most recent call last): File "examples.py", line 5, in <module> mylcd = RPi_I2C_driver.lcd() File "/root/lcd2/RPi_I2C_driver.py", line 107, in __init__ self.lcd_write(0x03) File "/root/lcd2/RPi_I2C_driver.py", line 132, in lcd_write self.lcd_write_four_bits(mode | (cmd & 0xF0)) File "/root/lcd2/RPi_I2C_driver.py", line 127, in lcd_write_four_bits self.lcd_device.write_cmd(data | LCD_BACKLIGHT) File "/root/lcd2/RPi_I2C_driver.py", line 26, in write_cmd self.bus.write_byte(self.addr, cmd) IOError: [Errno 5] I/O error

Does anyone know what is wrong? I copy pasted code. Running i2cdetect 0 says that my i2c is under 0x27 address (so I did not change anything in the script). Do you have any ideas what is wrong? I connected i2c to hd44780 pin by pin (1--1, 2--2, 3--3, 4--4, etc.)

@raphaelyancey
Copy link

Anybody experiencing strange characters after a certain amount of uptime? Seems to work again when restarting the script, maybe a memory issue?

@Machacator
Copy link

Machacator commented Dec 24, 2016

raphaelyancey commented on 6 Nov
Anybody experiencing strange characters after a certain amount of uptime? Seems to work again when restarting the script, maybe a memory issue?

Yes! me!
In previous versions, my script would run fine for about a day or two. I never figured out the reason why it was behaving like that so i gave up.
About a week ago i installed everything from scratch and use raspbian lite, thinking that maybe that would help........well, it now stops working after half an hour.
Using RPI 1B

EDIT: Internet conection seems to be affecting somehow. With either ethernet cable or wifi dongle, it displays the characters of doom after about 30min. Without any conection it will last for about 6 hours.

@psybertech
Copy link

Looking for help or advice on this.
I have the RPi_I2C_driver working fine for basic use, but when I try to use it how I want, strange things happen similar to what raphaelyancey and Machacator mention about strange characters. I also experience it writing the strange characters to the wrong lines.

What I am trying to do is have 4 independent scripts, each write to one single line.
No scrolling code.
All text is being printed from each script is 20 or less characters.
Each script writes to one line directly using either:
mylcd.lcd_display_string("string here",1);
or
mylcd.lcd_display_string_pos("this string here",2,0);

Script 1 - python - real time date and time with seconds displayed on line 1 - script is always running and updates line one once a second. No lcd_clear() used.
Script 2 - outside temp and inside temp checked every minute displayed on line 2. No lcd_clear() used. Script is run from cron, writes to LCD and exits.
Script 3 - RSS feed tracking my favorite NHL team score updated every 3 minutes and displays on line 3. No lcd_clear() used. Script is run from cron, writes to LCD and exits.
Script 4 - a feedback line showing last home automation feature used and display on line 4. No lcd_clear() used. Script is run when a command is issued, so it can be anytime. Script outputs and then exits.

So, each one works perfectly on their own.

When I have two or more running, it will soon start writing random characters to the screen, mostly on the wrong lines (can be above or below). It can happen nearly instantly or start happening 20 minutes later.
When it starts, only a lcd_clear can fix it. Even if line 1 is being controlled by the always running date time script updating every second.

I have tried adding the screen clear to one or more of the scripts, but due to other issues (data not being present on the script run), lines won't update again until the next cron run or command. Not ideal, plus I hate the way the lcd_clear() looks when it happens.

I am sure it has to do something with two or more scripts trying to access the lcd at nearly the same time.
I assumed having the ability to address the writing per line wouldn't cause any issues, but it doesn't seem that way.

So, any ideas or suggestions to get 4 scripts each writing their own line, to play nice and not corrupt the display?
My only limitation I can think of is that combining the scripts into one is not an option.

Any thoughts are welcomed.

Thanks!

@pwlandoll
Copy link

Hey all! I came across this looking for info on how to get my own LCD/I2C combo to work with my rpi 3, and thought it would be beneficial for enough people to make it into a pypi package. Any ideas/contributions/complaints are welcome!

https://github.com/pwlandoll/i2c_lcd

@Halo07
Copy link

Halo07 commented Aug 17, 2017

I can't understand what mean: 0x00, 0x00, 0x03, 0x04, 0x08, 0x19, 0x11, 0x10
What is this? Coordonates? Or what? Because i don't understand how to edit this

@salva2k4
Copy link

Hi, can u help me ?
When i run this file, i've this messge !

Traceback (most recent call last):
.../exampleLCD.py", line 91, in
mylcd.lcd_display_string_pos(unichr(1),1,pos)
NameError: name 'unichr' is not defined

where do i defined this 'unichr' ?
thanks

@j0hnsmith
Copy link

@DenisFromHR any idea what these are for?

   def __init__(self):
      self.lcd_device = i2c_device(ADDRESS)

      self.lcd_write(0x03)  # what
      self.lcd_write(0x03)  # are
      self.lcd_write(0x03)  # these
      self.lcd_write(0x02)  # for

      self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)

@atchoo78
Copy link

atchoo78 commented Dec 16, 2017

Great stuff!

I live in Norway, but apparently I have the LCD ROM with the Japanese font set, which makes displaying the nordic characters (æ, ø, å) a real headache, at least when you're dealing with inconsistent ascii-tables and unicode (UTF8) conversions on top of that.

However, I made a custom font set with the "missing" norwegian characters, if anyone's interested.
I used this character generator, set it to "hex" datatype, and used @DenisFromHR 's example code to write the characters to the display.

Just out of curiousity:
Is there a an (relatively easy) way to "map" these custom characters to their unicode/UTF-8 counterparts?
Example: If I try to

"lcd_display_string("æøå", 1)

it would be GREAT if I somehow managed to automatically map the character "æ" to (for instance) chr(1), "ø" to chr(2) and so on.

And if everything else fails, shouldn't it be possible to do a "char.replace('ø', chr(1))", or something similar in Python? It would make the programming flow SO much easier.

Any thoughts?

Anyway, here's my (very basic) example code (updated):

import RPi_I2C_driver
lcd = RPi_I2C_driver.lcd()
fontdata1 = [
    # Æ
    [ 0x00,0x0C,0x13,0x12,0x1F,0x12,0x13,0x00 ],
    # Ø
    [ 0x01,0x0E,0x13,0x15,0x19,0x0E,0x10,0x00 ],
    # Å
    [ 0x04,0x00,0x0E,0x11,0x1F,0x11,0x11,0x00 ],
    # æ
    [ 0x00,0x00,0x1A,0x05,0x0F,0x14,0x1F,0x00 ],
    # ø
    [ 0x00,0x01,0x0E,0x15,0x15,0x0E,0x10,0x00 ],
    # å
    [ 0x04,0x00,0x0E,0x01,0x0F,0x11,0x0F,0x00 ],
    # degrees Celsius
    [ 0x08,0x14,0x0B,0x04,0x04,0x03,0x00,0x00 ],
    # humidity
    [ 0x00,0x04,0x0E,0x0E,0x1F,0x1F,0x0E,0x04 ],
    # low batt.
    [ 0x0E,0x11,0x11,0x11,0x11,0x11,0x1F,0x1F ],
    # full batt.
    [ 0x0E,0x11,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F ],
]
lcd.lcd_load_custom_chars(fontdata1)

#and to use the custom characters:
lcd.lcd_display_string(chr(2) + "some other random letters" , 1)
lcd.lcd_display_string("Batt: " + chr(9) , 2)

Update:

Here's how I managed to add the extra Norwegian characters to the font set. I know there are more efficient ways to do this, but I'm still learning Python, and for this purpose it kind of works:

lcd = RPi_I2C_driver.lcd()

LCD_NORCHARS = [
[ 0b00000,0b01111,0b10100,0b10100,0b11111,0b10100,0b10111,0b00000 ], # Æ
[ 0b00001,0b01110,0b10011,0b10101,0b10101,0b11001,0b01110,0b10000 ], # Ø
[ 0b01100,0b00000,0b01100,0b10010,0b11110,0b10010,0b10010,0b00000 ], # Å
[ 0b00000,0b00000,0b01101,0b00010,0b01111,0b10010,0b01101,0b00000 ], # æ
[ 0b00000,0b00000,0b00001,0b01110,0b10101,0b10101,0b01110,0b10000 ], # ø
[ 0b00000,0b01100,0b00000,0b01100,0b00010,0b11110,0b10010,0b01101 ], # å
]

lcd.lcd_load_custom_chars(LCD_NORCHARS)
string01 = "Æ Ø Å"
string02 = "æ ø å"

string01_N = string01.replace('Æ', chr(0)).replace('Ø', chr(1)).replace('Å', chr(2)).replace('æ', chr(3)).replace('ø', chr(4)).replace('å', chr(5)).decode('utf8')  # This would be better with a "For X in array..." loop, but I can't figure out how to do it yet
string02_N = string02.replace('Æ', chr(0)).replace('Ø', chr(1)).replace('Å', chr(2)).replace('æ', chr(3)).replace('ø', chr(4)).replace('å', chr(5)).decode('utf8')

lcd.lcd_display_string(string01_N, 1)
lcd.lcd_display_string(string02_N, 2)

@ThallerIT
Copy link

ThallerIT commented Mar 27, 2018

Good job, Denis!
I love the user defined characters. I've created a little php script to be able to create the code for the special characters in the syntax you are using here, e.g. [ 0x0E,0x11,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F ]

In case anyone is interested, here it is:
https://pastebin.com/EL1nR5MX

lcd_char

cheers, Martin

@Maikeleg
Copy link

Maikeleg commented May 5, 2018

Hi Denis,
This looks awesome, thanks for putting this on github. I’m very interested in some examples that you mentioned in your first post to make a scrolling text.
Thanks a ton
Maikel

@mish0petr0v
Copy link

I'm trying to use this driver with PCF8574's younger sibling - PCA9554P with a 16x2 lcd display, but it does not work at all. I'm constantly getting a same error:

Traceback (most recent call last): File "examples.py", line 5, in <module> mylcd = RPi_I2C_driver.lcd() File "/home/pi/i2c/RPi_I2C_driver.py", line 105, in __init__ self.lcd_write(0x03) File "/home/pi/i2c/RPi_I2C_driver.py", line 130, in lcd_write self.lcd_write_four_bits(mode | (cmd & 0xF0)) File "/home/pi/i2c/RPi_I2C_driver.py", line 125, in lcd_write_four_bits self.lcd_device.write_cmd(data | LCD_BACKLIGHT) File "/home/pi/i2c/RPi_I2C_driver.py", line 24, in write_cmd self.bus.write_byte(self.addr, cmd) IOError: [Errno 121] Remote I/O error
with python 2.7.13

and

Traceback (most recent call last): File "examples.py", line 5, in <module> mylcd = RPi_I2C_driver.lcd() File "/home/pi/i2c/RPi_I2C_driver.py", line 105, in __init__ self.lcd_write(0x03) File "/home/pi/i2c/RPi_I2C_driver.py", line 130, in lcd_write self.lcd_write_four_bits(mode | (cmd & 0xF0)) File "/home/pi/i2c/RPi_I2C_driver.py", line 125, in lcd_write_four_bits self.lcd_device.write_cmd(data | LCD_BACKLIGHT) File "/home/pi/i2c/RPi_I2C_driver.py", line 24, in write_cmd self.bus.write_byte(self.addr, cmd) File "/usr/local/lib/python3.5/dist-packages/smbus/util.py", line 59, in validator return fn(*args, **kwdefaults) File "/usr/local/lib/python3.5/dist-packages/smbus/smbus.py", line 121, in write_byte raise IOError(ffi.errno) OSError: 121
with python 3.5.3

I'm using Raspbian stretch full, downloaded on 9.11.2018.
What's wrong with my situation?

@wolfmanbass
Copy link

Is it possible to run multiple LCD's each with a different address?

@gingerhelp
Copy link

Thanks for the libs!

I've spend several hours for trying start libs working.. And I fix it with screwdriver.. Fix potentiometer brightness.

Thank you for posting this - I was pulling my hair out over the same thing.

@ruediheimlicher
Copy link

Hi Denis
Thanks for the libs. I have two 16x2 LCD's from different shops.
It works fine on LCD 1, but LCD 2 displays sometimes a few characters, sometimes blocks, sometimes nothing, hanging for a few seconds. Switching on and off the display works. Probably a timing problem.
Any Idea where to change any delays?

@SimonChelkowski
Copy link

Dear @DenisFromHR

Myself and some others use your code to make a contribution to the RPi-Jukebox-RFID project. This project allows to build a simple to use Jukebox like Music Player which is extremely interesting for small Kids. I use these Jukeboxes for my three Kids.
We developed a project specific script this uses directly your RPi_I2C_driver.py driver (renamed to i2c_lcd_driver.py to be compatible with the contribution regulations of the RPi-Jukebox-RFID project).

Current status is, that I placed a pull request to the RPi-Jukebox-RFID project which can be found under RPi-Jukebox-RFID Pill request #859.

Currently the decision is that the code cannot be added to the project as we have a license issue. The RPi-Jukebox-RFID projet uses MIT license, see also RPI-Jukebox-RFID License Page. Your driver is released under GPL.

I kindly ask you read the discussion on the pull request page related to the driver scripts/i2c_lcd_driver.py where the license issue is discussed. I hope that you could to a statement, that allows us to use your code in our Jukebox project.

Thank you very much for your help regarding this matter.

Best regards,
Simon

@ibnuh
Copy link

ibnuh commented Aug 29, 2020

Thanks for the libs!

I've spend several hours for trying start libs working.. And I fix it with screwdriver.. Fix potentiometer brightness.

Thanks, I'm new to this stuff, TIL about potentiometer from your comment

@gersmit
Copy link

gersmit commented Sep 12, 2020

Hi Dennis,

Here Ger from Holland.
Can you tell me if you already made a scroll option.
Your scripts are very nice.

Thanks for an answer

Greetings Ger

@deejayves
Copy link

deejayves commented Mar 14, 2021

Hi Dennis,

I'm using your library in my outdoor spa project. It works like a charm!

Thx.

Yves
https://blog.jbip.be/blog/jacuzzi/

@bnwlkr
Copy link

bnwlkr commented Aug 1, 2021

Thanks for the code Dennis. Here's what I'm using for text scrolling in case it helps anyone:

async def display(lcd, display_queue):
    ln1, ln2 = "", ""
    ln1_i, ln2_i = 0, 0
    while True:
        if not display_queue.empty():
            ln1, ln2 = await display_queue.get()
            ln1_i, ln2_i = 0, 0
            lcd.lcd_clear()
        else:
            ln1_i = 0 if len(ln1) <= 16 else (ln1_i + 1) % (len(ln1) - NUM_COLS + 1)
            ln2_i = 0 if len(ln2) <= 16 else (ln2_i + 1) % (len(ln2) - NUM_COLS + 1)

        lcd.lcd_display_string(ln1[ln1_i:ln1_i+NUM_COLS], 1)
        lcd.lcd_display_string(ln2[ln2_i:ln2_i+NUM_COLS], 2)

        await asyncio.sleep(0.5)

where display_queue is an asyncio.Queue.
To display something, call await display_queue.put(("Loooooooooooooooooonng", "Teeeeeeeeeeeeeeeeeeext"))

@nox0x1
Copy link

nox0x1 commented Aug 18, 2021

Hi Dennis,

Thank you for your work on this. I attempted to make this work in the 8-bit mode but have not been able to.

116 self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_8BITMODE)

How can the following write statements be condensed into 1 line instead of the two in 8 bit mode.

def lcd_write_char(self, charvalue, mode=1):
      self.lcd_write_four_bits(mode | (charvalue & 0xF0))
      self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0))

def lcd_write(self, cmd, mode=0):
      self.lcd_write_four_bits(mode | (cmd & 0xF0))
      self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))

I tried this but it gives strange results even though the binary output is correct for this command:

def lcd_write(self, cmd, mode=0):
      self.lcd_write_four_bits(mode | (cmd & 0xFF))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment