I2C 20×4 LCD character display on a #RaspberryPi

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)

# Write a command and argument
   def write_cmd_arg(self, cmd, data):
      self.bus.write_byte_data(self.addr, cmd, data)

# Write a block of data
   def write_block_data(self, cmd, data):
      self.bus.write_block_data(self.addr, cmd, data)

# 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

# flags for display entry mode

# flags for display on/off control

# flags for display/cursor shift

# flags for function set
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00

# flags for backlight control

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(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
      self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT)

   # clocks EN to latch command
   def lcd_strobe(self, data):
      self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT)
      self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT))

   def lcd_write_four_bits(self, data):
      self.lcd_device.write_cmd(data | LCD_BACKLIGHT)

   # 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:
      if line == 2:
      if line == 3:
      if line == 4:

      for char in string:
         self.lcd_write(ord(char), Rs)

   # clear lcd and set to home
   def lcd_clear(self):

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!

105 comments for “I2C 20×4 LCD character display on a #RaspberryPi

  1. ashley
    20 August 2013 at 5:47 pm

    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?

    • null
      12 October 2015 at 1:25 am

      In i2c_lib.py, change def __init__(self, addr, port=1) to def __init__(self, addr, port=0) and try again.

  2. Mike
    10 November 2013 at 6:26 pm

    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.

  3. Supra
    15 December 2013 at 2:17 am

    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?

  4. Supra
    15 December 2013 at 2:49 am

    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!

  5. Supra
    16 December 2013 at 3:06 pm

    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)
    while True:
    distance = display_measure()
    lcd.lcd_display_string(“Distance : %.1f cm” % distance, 2)

  6. Supra
    20 December 2013 at 2:39 pm

    Never mind. I solved problem by adding brace bracket.

    Many thanks!
    We wish you A Merry Christmas and a Happy New Year to You.

  7. 5 January 2014 at 6:58 pm

    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!

  8. fish
    16 January 2014 at 7:37 pm

    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

  9. Matt
    25 January 2014 at 5:10 am

    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:
    elif state == 0:

    then in lcd.py just put “lcd.backlight(1)” to turn it on, an use 0 to turn it off


    • Pepijn Koopman
      9 February 2015 at 6:46 pm

      Nice addition, thanks!

  10. fpp
    1 February 2014 at 4:37 pm

    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 !

    • 1 February 2014 at 4:44 pm

      You’re very welcome πŸ™‚

      • funtomi
        1 February 2014 at 9:57 pm

        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)


  11. fpp
    2 February 2014 at 10:07 pm

    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 πŸ™‚

  12. simon
    12 April 2014 at 2:40 pm

    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

    • Supra
      1 May 2014 at 7:16 pm

      • 2 May 2014 at 8:38 am

        Thanks Supra for posting the answer πŸ™‚

        • Samuel L.
          9 November 2014 at 1:08 am

          What is the answer?

  13. Randy
    13 April 2014 at 7:19 pm

    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.

    • 13 April 2014 at 7:31 pm

      You’re welcome – glad it worked πŸ™‚

  14. 2 May 2014 at 12:45 pm

    Many Thanks i have posted comments on the You Tube Site will Try again this weekend πŸ™‚

  15. Andrew
    24 June 2014 at 7:18 pm

    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?

    • Andrew
      7 July 2014 at 10:41 am

      Any news on this?

  16. satish
    2 July 2014 at 12:53 pm

    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!

    • 2 July 2014 at 1:31 pm

      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.

  17. Tony Muir
    5 July 2014 at 2:32 pm

    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.

  18. Denis
    5 July 2014 at 5:08 pm

    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?

  19. Denis
    6 July 2014 at 7:08 pm

    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(my_long_string[:19], 1)
    ===> end of code

    Hope this saves some time to others who might find this page πŸ™‚

    • Pedro Rodrigues
      1 October 2014 at 10:40 pm

      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.


      • supra
        7 October 2014 at 8:06 pm

        No!. You don’t needed to do indentation.
        Just copy and paste it or type manual.

  20. 9 July 2014 at 12:46 pm

    Just a word to thank You, works on a eBay i2c LCD 16×2, just had to change address from 0x27 to 0x20 πŸ™‚

  21. dirtdiver
    31 July 2014 at 5:40 pm

    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?

  22. dirtdiver
    1 August 2014 at 7:29 am

    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:
    elif state == 0:

    I use the Pi B+ with an 20×4 I2C LCD with out an Levelshifter.

  23. assassins
    25 August 2014 at 3:18 pm


    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.


    • recantha
      25 August 2014 at 5:19 pm

      You just need to get the output and push it to the LCD functions. Not sure how I can help.

  24. Pedro Rodrigues
    16 September 2014 at 1:32 pm

    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.


    • supra
      7 October 2014 at 8:03 pm

      Look up above @Denis

  25. Rayman
    1 October 2014 at 1:31 pm

    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?


    • recantha
      1 October 2014 at 2:17 pm

      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 πŸ™‚

  26. Marcel Ro
    6 October 2014 at 7:16 am

    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__
    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


    • Supra
      7 October 2014 at 7:59 pm

      Did you added sudo apt-get install python-smbus?

  27. Samuel
    19 October 2014 at 4:59 pm

    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:

    #End at:
    while (x < 256):
    # There can be 'pages' of empty addresses
    # so keep hitting enter.
    # Wait for enter-key:
    raw_input("Press Enter")

    #First line
    while (x < (start+16)):
    lcd.lcd_write(x, Rs)

    while (x < (start+32)):
    lcd.lcd_write(x, Rs)

    #Show numbers of symbols
    print start, "-" ,x-1

    • samuel
      19 October 2014 at 5:06 pm

      sorry, indentation missing.

    • recantha
      21 October 2014 at 8:18 am

      Thanks Samuel! πŸ™‚

  28. 21 October 2014 at 4:28 pm

    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

  29. 31 October 2014 at 8:41 pm

    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__
    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?

    • Supra
      4 November 2014 at 10:09 am

      Sorry! I cannot recalled how How I did it(year ago). But I’m using python 3 or later.

  30. MarcelRo
    4 November 2014 at 10:57 am

    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.

    • Supra
      16 November 2014 at 12:25 pm

      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.

  31. Supra
    4 November 2014 at 12:30 pm

    @ Eric and Marcel Ro.

    Can you delete function?
    def write_cmd(self, cmd):
    self.bus.write_byte(self.addr, cmd)

    After you deleted it. Just type manual on keyboard, but do not copy and paste it. But same indentation.

    Hope that help!

  32. 9 February 2015 at 9:52 am

    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 πŸ™‚


  33. 9 February 2015 at 3:38 pm

    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:

    • Supra
      10 February 2015 at 2:56 pm

      Thank, Denis. I tested it today under new version of wheezy

      • 10 February 2015 at 9:32 pm

        Supra, does it work OK?

  34. Supra
    11 February 2015 at 9:39 am

    Yes! It is working now

  35. Tomas
    21 March 2015 at 7:08 am

    Some picture for lcd, you can modify it…

    pi@raspberrypi ~ $ cat ./bin/startscript.sh
    verzia=$(uname -r)

    cat > /home/pi/lcd/text.txt <
    / )\ Raspberry Pi
    ,/_/_/ $verzia
    cd /home/pi/lcd
    sudo python lcd.py
    cd – >/dev/null
    pi@raspberrypi ~ $ uname -r
    pi@raspberrypi ~ $

  36. Dario
    19 April 2015 at 10:23 pm

    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

    • recantha
      20 April 2015 at 5:40 pm

      Is that the WHOLE error? Seems to be missing what the actual error was.

  37. 8 May 2015 at 1:39 am

    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?

    • recantha
      8 May 2015 at 6:51 am

      Hi Sandrine. Is the backlight definitely on?

      • 8 May 2015 at 1:30 pm

        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.

        • recantha
          8 May 2015 at 1:34 pm

          Ahhh. Yes, that’ll be it πŸ™‚

        • Supra
          10 June 2015 at 12:07 pm

          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

  38. Mecaz
    7 June 2015 at 7:59 pm


    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

    • Supra
      10 June 2015 at 12:09 pm

      You can google on search or youtube. There are tons of codes online.

      • mecaz
        10 June 2015 at 9:10 pm

        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


        • supra
          11 June 2015 at 10:03 am

          Did you do sudo i2cdetect -y 1?
          Did you do enable i2c?

  39. mecaz
    11 June 2015 at 8:11 pm

    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

    • Supra
      12 June 2015 at 2:08 am

      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

    • supra
      12 June 2015 at 2:10 am

      One question.
      Did u downloaded from github source code?

      • recantha
        15 June 2015 at 10:22 am

        Not too sure what you mean.

        • Supra
          16 June 2015 at 9:56 am

          If mecaz downloaded from github. Probably will have to used i2C library in order to get mpdlcd and lcdproc working

    • supra
      12 June 2015 at 2:29 am

      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:

      Hope that help

      • maxdid
        11 August 2015 at 11:48 pm

        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

  40. Doctor
    22 June 2015 at 11:45 pm

    WOW! This helped me sooo much! I’ve been all over looking for this solution, Thanks!

    • Supra
      25 June 2015 at 10:25 am

      You’re welcome

  41. Bub
    29 September 2015 at 7:56 am

    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.

    • Michael Horne
      29 September 2015 at 8:55 am

      Try twiddling the trimming potentiometer on the back – it might be working but not turned up enough for the text to display.

      • Konstantin
        30 September 2015 at 9:24 pm

        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!

        • Michael Horne
          1 October 2015 at 8:48 am

          You’re welcome πŸ™‚

  42. Bub
    29 September 2015 at 7:12 pm

    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 πŸ˜‰

  43. Bub
    3 October 2015 at 11:08 am


    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 ?

    • supra
      3 February 2016 at 3:33 pm

      cd.lcd_display_string(β€˜Desk: %s %sC %s’ % (temp_1,degree,), upA,2)

      • supra
        3 February 2016 at 3:39 pm

        My mistaken.
        It should be like this:
        lcd.lcd_display_string(β€˜Desk: %s %sC %s’ % (temp_1,degree), upA,2)

  44. Harry
    1 February 2016 at 9:32 pm

    Has anyone gotten cursor to work ? I can’t figure out how to turn it on or move it to the desired position.

    • supra
      3 February 2016 at 3:37 pm

      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

  45. Dave
    22 February 2016 at 2:33 am

    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

    • supra
      2 March 2016 at 6:57 pm

      Is your lcd has a black square?

  46. John V.
    1 March 2016 at 9:22 pm

    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?

    • supra
      2 March 2016 at 6:49 pm

      Used potentiometer. If u know value from potentiometer, then u can used resistor with 5 bands resistor

  47. redbeer23
    2 March 2016 at 11:11 am

    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 ??

    • supra
      2 March 2016 at 6:46 pm

      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.

    • alx_rom
      4 April 2016 at 3:46 am

      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_display_string(“*Control de Acceso*”,2)

      lcd.lcd_display_string(“* Iniciando… *”,3)


      lcd.lcd_display_string(“* Se ah iniciado *”,2)

      lcd.lcd_display_string(“* el sistema *”,3)


      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

      thanks users fpp and supra

  48. alx_rom
    4 April 2016 at 4:23 am

    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

    • supra
      5 April 2016 at 7:28 pm

      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.

      • alx_rom
        7 April 2016 at 5:49 am

        I want the text will be displayed side to side , but commands can not drive

  49. supra
    7 April 2016 at 12:25 pm

    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.

    • alx_rom
      9 April 2016 at 7:28 am

      I am also using LCD 20 x 4 , thanks for my question πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *