LCD Interface

Another update for anyone watching the page – 2/10/2012. There appears to be some timing issues with the LCD library I wrote – which have become apparent since I made teh delayMicroseconds() function more accurate. More updates on the page linked to below as well as some nre Fristing diagrams!

Update to this page – 11/7/2012: Decided to make a “proper” LCD library and add it into the wiringPi library. See here for full details.

Doing a bit of a tidy up, I found an old, but unused 16 x 2-line LCD text display module. Type: LMB162A. This has a fairly standard controller on it which accepts simple commands and ASCII text.

The interface can run in 4-bit mode or 8-bit mode. Since I had the GPIO pins, I decided to run it in 8-bit mode…

Problem No. 1: It’s a 5V device and the Pi is 3.3v… However most 5V logic will work when presented with a 3.3v signal…

Problem No. 2: The LCD controller is a read/write device, and if it were to ever try to write to the Pi, it would put 5v on the output pins… But who wants to read from it anyway, so I hard-wired the R/W pin to ground, so it was permanently in write mode – which also meant I needed one less wire from the Pi to the module.

So 8 data bits, a bit to tell it you’re writing a command or data, and a strobe bit. 10 GPIO pins needed. I used the first 8 general purpose IO pins and the 2 I2C pins set to output mode.

The LCD was powered from the 5V on the GPIO connector.

And after some simple software, it worked!

Note: New GIT Repository here

LCD on Raspberry Pi via GPIO

LCD on Raspberry Pi via GPIO

Now… Do I carry this on and make a general purpose LCD library as an extension to wiringPi, or just leave it as that…

-Gordon

Comments

LCD Interface — 43 Comments

  1. Hi!

    Your projects are absolutely great!!!
    Your tutorials are great for beginners and experienced users as well!
    Could you please send me your code to control the LCD module?

    Thank you for your work!

    • Glad you’re enjoying it!
      I’ve put a link to the code so-far in the article, so just re-load it and you’ll see the link. The program I wrote is very basic, but it contains the essence of what you need to put text on the display.
      Since I’ve had a couple of enquiries about it now, I might just make the effort to expanding it and making it a bit easier to use.
      -Gordon

      • Thanks!

        The connections, just to make sure I’m correct (if unsure, I put a ? mark; wiringPi pin numbering):

        LCD — RPi

        Ground — GROUND
        VCC (+3.3 to +5V) — +3.3V
        Contrast adjustment (VO) — ?
        Register Select (RS). RS=0: Command, RS=1: Data — 8
        Read/Write (R/W). R/W=0: Write, R/W=1: Read — GROUND
        Clock (Enable). Falling edge triggered — 9 ?
        Bit 0 (Not used in 4-bit operation) — 0
        Bit 1 (Not used in 4-bit operation) — 1
        Bit 2 (Not used in 4-bit operation) — 2
        Bit 3 (Not used in 4-bit operation) — 3
        Bit 4 — 4
        Bit 5 — 5
        Bit 6 — 6
        Bit 7 — 7
        Backlight Anode (+) — 3.3V ?
        Backlight Cathode (-) — GROUND

        Do you think these are correct?

        Thanks again!

        • Almost!

          VCC on the LCD module I’m using goes to +5V. If you have a 3.3v module then it’s obviously going to go to the +3.3v pin.
          The contrast pin – I have that going to the wiper of a 10K pot between +5V and 0v.
          R/W is tied to ground (We’re not going to read from it – and can’t when it’s at 5V anyway – we are taking a bit of a risk here by using a 5V module, but as long as the module never tries to write, then we’ll be OK)
          RS – GPIO pin 0 (SDA0) – or wiringPi pin 8.
          E (clock/strobe) – GPIO pin 1 (SCL0) – or wiringPi pin 9.

          I’m using all 8 data lines here. If you want to use 4, then you’ll need to change the code to put it into 4-bit mode and split the dats into 4-bit chunks.

          The data lines go from wiringPi pins 0 through 7, or in GPIO terms, it’s the seauence 17, 18, 21, 22, 23, 24, 25, 4

          The backlight is wired directly between +5v and ground (the module I’m using has an internal resistor)

          Hope this helps.

          -Gordon

          • It certianly looks similar from a quick look at the data sheet. It’s also a 5V device, so remember to connect it to the 5V supply of the GPIO header.
            Most of these LCD devices use the same controller chip anyaway – just double check the pin configurations.
            -Gordon

  2. Thanks for your quick replies!

    I’ve got some suggestions about the RPi LCD Interface:

    – you could use a 8-bit port expander like the TI PCF8574 (through I2C), so more LCD modules could be added (and if one is used, significantly less wire would be required)
    – if I2C communication works, an ADC (and RTC) could be added as well; so one could measure signals (e. g. temperature, light intensity) and display it realtime on LCD. This would be really-really useful, because the SoC lacks an ADC (and RTC as well).
    – it would be great if the displayed text could be modified directly from shell possibly without superuser permissions (like the LEDs through command gpio), so scripts could write messages to the LCD.
    – you could see by this way kernel messages, or a prompt on the LCD 🙂

    More details about PCF8574 and I2C (with RPi):
    http://hackaday.com/2008/12/27/parts-8bit-io-expander-pcf8574/

    More details about connecting an LCD through I2C (with RPi):
    http://hackaday.com/2012/06/28/using-an-hd44780-character-lcd-with-the-raspberry-pi/

    • Hi,

      Yes – you’re right – I could use other chip, or I could use serial access displays, or I2C/SPI, etc. all of those things are possible!

      However… What I’m doing here is just experimenting. Seeing what I can do and what can be done – hopefully giving some additional inspiration to others too! I had an old and unused spare 5V 2-line LCD display, so I wanted to see if I could hook it to the Pi and use it – and I can!

      And I think that’s the great thing about the Pi – it’s opened up so many possibilities! Sure, I’ve done all this on Arduinos and other microcontrollers on the past too (for fun and for business), but here is a little platform that you can do the development directly on, which I think it really neat.

      As for the command-like non-root stuff – you’ve seen the wiringPi gpio command, and to adapt the code here to work as non-root wouldn’t be hard at all. (make it set-uid is the obvious one!) Getting comsole messages out – a little bit harder, but writing a fake “serial” driver to integrate into the kernel isn’t that hard, but 2 lines of 16 characters for console messages – challenging!

      I’m looking at the I2C and SPI drivers in the newer kernels now, so maybe soon I’ll have something going via that interface too.

      Cheers,

      -Gordon

    • The red one, the green one the … 😉
      Not sure they have a proper name though – they’re female to male jump (or patch) leads to me…
      -Gordon

  3. Hey,

    Just wondering if you know where I can get one of these LCD screens. I’ve been hunting about on Google but I don’t want to order 500 of them.

    Great work here BTW, very much what I am after as a novice playing with my Pi.

    Neil.

    • These screens are available on eBay very cheaply. I got a 3.3V 1602 display from a Hong Kong seller for £3.16 including P&P.

      Now off to the loft to dig out the Veroboard I bought in 1979.

      Thanks Gordon for posting this, I’m a bit of a dunce on electronics, maybe I can make it work from your information.

  4. Hi Gordon,
    Great work on the GPIO intros! Loving it!
    Regarding the danger of writing 5v back to the PI – will simply putting diodes between the GPIO and the LCD guard against that? Or will the voltage drop across the diodes reduce the 3.3V below a valid high logic level?

    Just got my LCD from SKPang and am looking forward to playing with it!

    Cheers,
    Chris

    • Hi Gordon,
      I have just connected up my LCD display to the power (Vcc 5v) and added the contrast control (10k pot). I have soldered a hard link between pins 5 and 2 to pull the R/W pin permanently down to zero. I have not connected the Pi yet.

      However, I am a bit worried as when I checked the voltages on the DB0 to DB7 pins, they are all +5v, even thought the R/W pin is grounded. Is that right? If I connect those pins to the PI GPIO, won’t it fry it?!

      I’ve double checked the schematic and can’t see anything wrong with how I have wired it. I am really anxious to try out the new display – but dont want to zap my Pi with 5V!!!

      Thanks!
      chris.

      • It’s possible the data lines are floating high rather than being driven high – I never actually measured them on my devices, just plugged them in and hoped for the best – possibly a little foolish on my part, but it did work well…

        Make sure the contrast pot does change things – I found that I had to turn it almost fully to one end then the pixels would start to get visibly dark on the display, then hook it up.

        And do check your power connections – pin 2 is +5v, pin 1 is 0v/ground.

        (and now that I recall, I think we’ve chatted on IRC about this – how did you get on?)

        -Gordon

    • Make sure the R/W line is tied to 0v/ground before you power it up and you should be fine. ie. as long as you stop the LCD device trying to write back to the Pi, it should be fine. If you wanted to be absolutely sure, you could use a 3.3 zener diode and a series resistor, but that may be a bit overkill. (You’ll need 4 or 8 of them)

      -Gordon

      • Thanks Gordon – I noticed something a little odd though before I gave up last night. If I remove the hard link between the R/W pin and ground all the DBx pins go to ground! It’s almost as if it is the reverse of what the documentation suggests! Maybe though – like you say – they are just floating. I have some 3.3 to 5v converters on order from SKPang, so I think I will wait until they arrive just to make sure I am not doing anything stupid!

        Thanks again for the help!

        Chris

        P.S. it wasn’t me on IRC, unless I am IRC’ing in my sleep….!

        • OK. Check the manual – the one I used was this one:

          Pin 1 is 0v, pin 2 is +5v and pin 3 is the input for the contrast. (wiper of a 10K pot between 5v and ground).

          And ok, not you on IRC, but I’m wondering if there is a duff manual out there as the chap I was talking to there had connected 5v and ground up the wrong way…

          -Gordon

  5. Is it possible to add mobile phone LCD? Say when not hooked with pc,I wish to use it? I have a dead mototorola rockr,Nokia 1100,reviving their screens will be awesome 🙂

    • There are some Nokia LCDs that can be used, but they have a somewhat different interface – it’s a high speed serial type interface rather than video or the parallel interface these character LCDs use. I’ve not used thee type of displays though.
      -Gordon

      • I have seen few videos in YouTube where Nokia displays are used for display experiments.I guess it mite b feasible to implement a colored display as well.will be awesome if u put forward similar crazy experiment with pi.say it gives u email notification or currently playing mp3 file.m hooking to ur blog for such post ,plz bring it soon 🙂

        • I’ll have a look about. I did see some someone had a Nokia 3300 display hooked up, so it’s possible, if somwewhat tiny!
          -Gordon

  6. I have an LCD up and running, so thanks for this article. Just one question: In the setup() function, in lcd.c, I noticed there are three calls to pinMode that come immediately after the call to digitalWrite. Is this intended, or should the pinMode calls come before the digitalWrite calls, which would seem more logical?

    Thanks – Dave

    • The idea is that when the pins switch from input mode (which they are in a reboot time) to output, I’ve already set the output values, so no potential glitches in-case the outputs have been (accidentally?) set the wrong way.

      Is this good or bad? I’m not sure, but it’s something I often do as it “feels” like the right thing to me…

      Seems to work though!

      Cheers,

      -Gordon

  7. This has been a great experience. I’ve not really played with the gpio interface on my pi. But I found this site, and I was using LEDs and buttons in no time! I went ahead and hooked up my 4×20 LCD that came included with my nerdkits. I cannot get anything to display. I have checked and double checked the wiring. I am using the 4bit interface, and my code is simple:
    (relvant parts)
    wiringPiSetup ()

    fd = lcdInit (4, 20, 4, 11,10 , 0,1,2,3,0,0,0,0) ;

    lcdHome (fd);
    lcdClear (fd);
    lcdPosition(fd,0,0);
    lcdPuts(fd, “Hello!”);

    Does anyone have any suggestions on what else to try?

  8. Well, apparently, lcdClear(fd), was causing the display to clear… but my “Hello!” never showed up! I removed the lcdClear() and it works.

    • I’m going to be looking at this code again in the next few days (if I have time). There have been 1 or 2 issues reported – I think mostly to do with timings. It seems some displays are slower than others… You might want to look at the library and increase the delays in some places to see if it works.

      -Gordon

  9. hello G
    i have a QUESTION about the progressbar command on the lcd 2×16 it is normal grows with blocks counting up, is it possible to let it go with thin lines and no blocks, a finer counting progress.

    • Not sure what the progressbar is – I’m guessing its someone elses program, however you can download your own characters to the display – you need to read the manual for the detaisl though.

      -Gordon

  10. Can the library address the lcd’s in 4bit mode yet? As ive seen quad level changers quite cheep and the lower pi. Count is tempting.

    Look forward to teying this out 🙂