Tutorial: Arduino and monochrome LCDs

Please note that the tutorials are not currently compatible with Arduino IDE v1.0. Please continue to use v22 or v23 until further notice. 

This is chapter twenty-four of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – A tutorial on the Arduino universe.

The first chapter is here, the complete series is detailed here.

Welcome back fellow arduidans!

The purpose of this article is to summarise a range of affordable monochrome liquid-crystal display units that are available to work with our Arduino; and to replace the section about LCDs in chapter two of this series. We will first examine some fixed-character and then graphical LCD units in this article. So let’s go!

Fixed-character LCD modules

When shopping around for LCD modules, these will usually be the the most common found in retail outlets. Their size is normally measured by the number of columns and rows of characters in the display. For example, the three LCDs below are 8×2, 16×2 and 20×4 characters in size:

lcdtypesss

Currently, most LCDs should have a backlight of some sort, however you may come across some heavily-discounted models on (for example) eBay that are not. Character, background and backlight colours can vary, for example:

backlitsss

Interfacing these screens with our Arduino boards is very easy, and there are several ways to do so. These interface types can include four- and eight-bit parallel, three-wire,  serial, I2C and SPI interfaces; and the LCD price is usually inversely proportional to the ease of interface (that is, parallel are usually the cheapest).

Four-bit parallel interface

This is the cheapest method of interface, and our first example for this article. Your LCD will need a certain type of controller IC called a Hitachi HD44780 or compatible such as the KS0066. From a hardware perspective, there are sixteen pins on the LCD. These are usually in one row:

16pinsss

… or two rows of eight:

2by8pinsss

The pin labels for our example are the following:

  1. GND
  2. 5V (careful! Some LCDs use 3.3 volts – adjust according to LCD data sheet from supplier)
  3. Contrast
  4. RS
  5. RW
  6. Enable
  7. DB0 (pins DB0~DB7 are the data lines)
  8. DB1
  9. DB2
  10. DB3
  11. DB4
  12. DB5
  13. DB6
  14. DB7
  15. backlight + (unused on non-backlit LCDs) – again, check your LCD data sheet as backlight voltages can vary.
  16. backlight GND (unused on non-backlit LCDs)

As always, check your LCD’s data sheet before wiring it up.

Some LCDs may also have the pinout details on their PCB if you are lucky, however it can be hard to decipher:

Now let’s connect our example 16×2 screen to our Arduino using the following diagram.

Our LCD runs from 5V and also has a 5V backlight – yours may differ, so check the datasheet:

4bitparallel2

(Circuit layout created using Fritzing)

Notice how we have used six digital output pins on the Arduino, plus ground and 5V. The 10k ohm potentiometer connected between LCD pins 2, 3 and 5 is used to adjust the display contrast. You can use any digital out pins on your Arduino, just remember to take note of which ones are connected to the LCD as you will need to alter a function in your sketch. If your backlight is 3.3V, you can use the 3.3V pin on the Arduino.

From a software perspective, we need to use the LiquidCrystal() library. This library should be pre-installed with the Arduino IDE. So in the start of your sketch, add the following line:

Next, you need to create a variable for our LCD module, and tell the sketch which pins are connected to which digital output pins. This is done with the following function:

The parameters in the brackets define which digital output pins connect to (in order) LCD pins: RS, enable, D4, D5, D6, and D7.

Finally, in your void setup(), add the line:

This tells the sketch the dimensions in characters (columns, rows) of our LCD module defined as the variable lcd. In the following example we will get started with out LCD by using the basic setup and functions. To save space the explanation of each function will be in the sketch itself. Please note that you do not have to use an Arduino Mega – it is used in this article as my usual Arduino boards are occupied elsewhere.

And here is a quick video of the example 24.1 sketch in action:

There are also a some special effects that we can take advantage of with out display units – in that we can actually define our own characters (up to eight per sketch). That is, control the individual dots (or pixels) that make up each character. With the our character displays, each character is made up of five columns of eight rows of pixels, as illustrated in the close-up below:

pixels

In order to create our characters, we need to define which pixels are on and which are off. This is easily done with the use of an array (array? see chapter four). For example, to create a solid block character as shown in the image above, our array would look like:

Notice how we have eight elements, each representing a row (from top to bottom), and each element has five bits – representing the pixel column for each row. The next step is to reference the custom character’s array to a reference number (0~7) using the following function within void setup():

Now when you want to display the custom character, use the following function:

where 0 is the memory position of the character to display.

To help make things easier, there is a small website that does the array element creation for you. Now let’s display a couple of custom characters to get a feel for how they work. In the following sketch there are three defined characters:

And here is a quick video of the example 24.2 sketch in action:

So there you have it – a summary of the standard parallel method of connecting an LCD to your Arduino. Now let’s look at the next type:

Three-wire LCD interface

If you cannot spare many digital output pins on your Arduino, only need basic text display and don’t want to pay for a serial or I2C LCD, this could be an option for you. A 4094 shift register IC allows use of the example HD44780 LCD with only three digital output pins from your Arduino. The hardware is connected as such:

twilcd

And in real life:

exam24p3ss

From a software perspective, we need to use the LCD3Wire library, which you can download from here. To install the library, copy the folder within the .zip file to your system’s \Arduino-2x\hardware\libraries folder and restart the Arduino IDE. Then, in the start of your sketch, add the following line:

Next, you need to create a variable for our LCD module, and tell the sketch which of the 4094′s pins are connected to which digital output pins as well as define how many physical lines are in the LCD module. This is done with the following function:

Finally, in your void setup(), add the line:

The number of available LCD functions in the LCD3wire library are few – that is the current trade-off with using this method of LCD connection … you lose LCD functions but gain Arduino output pins. In the following example, we will demonstrate all of the available functions within the LCD3Wire library:

And as always, let’s see it in action. The LCD update speed is somewhat slower than using the parallel interface, this is due to the extra handling of the data by the 4094 IC:

Now for some real fun with:

Graphic LCD modules

(Un)fortunately there are many graphic LCD modules on the market. To keep things relatively simple, we will examine two – one with a parallel data interface and one with a serial data interface.

Parallel interface

Our example in this case is a 128 by 64 pixel unit with a KS0108B parallel interface:

glcdparallelss

For the more technically-minded here is the data sheet. From a hardware perspective there are twenty interface pins, and we’re going to use all of them. For breadboard use, solder in a row of header pins to save your sanity!

This particular unit runs from 5V and also has a 5V backlight. Yours may vary, so check and reduce backlight voltage if different.

You will again need a 10k ohm potentiometer to adjust the display contrast. Looking at the image above, the pin numbering runs from left to right. For our examples, please connect the LCD pins to the following Arduino Uno/Duemilanove sockets:

  1. 5V
  2. GND
  3. centre pin of 10k ohm potentiometer
  4. D8
  5. D9
  6. D10
  7. D11
  8. D4
  9. D5
  10. D6
  11. D7
  12. A0
  13. A1
  14. RST
  15. A2
  16. A3
  17. A4
  18. outer leg of potentiometer; connect other leg to GND
  19. 5V
  20. GND

A quick measurement of current shows my TwentyTen board and LCD uses 20mA with the backlight off and 160mA with it on. The display is certainly readable with the backlight off, but it looks a lot better with it on.

From a software perspective we have another library to install. By now you should be able to install a library, so download this KS0108 library and install it as usual. Once again, there are several functions that need to be called in order to activate our LCD. The first of these being:

which is placed within void setup(); The parameter sets the default pixel status. That is, with NON_INVERTED, the default display is as you would expect, pixels off unless activated; whereas INVERTED causes all pixels to be on by default, and turned off when activated. Unlike the character LCDs we don’t have to create an instance of the LCD in software, nor tell the sketch which pins to use – this is already done automatically. Also please remember that whenever coordinates are involved with the display, the X-axis is 0~127 and the Y-axis is 0~63.

There are many functions available to use with the KS0108 library, so let’s try a few of them out in this first example. Once again, we will leave the explanation in the sketch, or refer to the library’s page in the Arduino website. My creative levels are not that high, so the goal is to show you how to use the functions, then you can be creative on your own time. This example demonstrate a simpler variety of graphic display functions:

Now let’s see all of that in action:

You can also send normal characters to your KS0108 LCD. Doing so allows you to display much more information in a smaller physical size than using a character  LCD. Furthermore you can mix graphical functions with character text functions – with some careful display planning you can create quite professional installations. With a standard 5×7 pixel font, you can have eight rows of twenty-one characters each. Doing so is quite easy, we need to use another two #include statements which are detailed in the following example. You don’t need to install any more library files to use this example. Once again, function descriptions are in the sketch:

Again,  let’s see all of that in action:

If you’re looking for a very simple way of using character LCD modules, check this out.

LEDborder

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The following two tabs change content below.

John Boxall

Founder, owner and managing editor of tronixstuff.com.

58 Responses to “Tutorial: Arduino and monochrome LCDs”

  1. Russ says:

    John – this is excellent stuff, as usual.

    I wan’t aware it was possible to use a shift register to reduce the number of pins used – I have been working on a project to create a clock and have been struggling to find a couple of free pins so this may work for me, but I have a question: I have created custom characters for alarm status and snooze – will I loose these functions using the shift register?

    Many thanks

    Russ

  2. eren says:

    Good tutorial. Thanks for sharing

  3. pasha34 says:

    Thnks. Very Good Site.

  4. Justin W says:

    John, great tutorials! I just wanted to note that I was having difficulties with 24.1 until I hooked pin 5: RW up to ground, like shown here http://arduino.cc/en/uploads/Tutorial/LCD_bb.png. Once I did that, my text displayed correctly.

  5. Ed says:

    Hey! Got this working!

    Thanks a lot for the tutorial, it’s great!

    Ed

  6. wowmir says:

    Hi, have you posted a list of all the components that are required to complete all your tutorials. If not maybe you can make your own kit to go with this series. I am following it from India and would not mind buying all of the stuff required( two of each if possible,so that I can blow stuff out ) at one go.

    1) This would save a lot of time
    2) It will actually be cheaper
    3) Most importantly it will help people stay in the flow of learning for long hours at a stretch.

    As you can see your site is now very popular so it might be a venture worth trying.

  7. JRMN says:

    Is it possible to scroll a single continuously line, but not effect the previously output text? I tried leftToRight() and rightToLeft(), but they don’t seem to do what I’m looking for.

    • John Boxall says:

      Yes, it could be done. However some work would be involved. Suggestion would be to put your text in an array of characters, then a loop would display array elements 0~15, then 1~16 etc and also track when the array ended to fill the rest of the line with characters starting again from array element zero.
      cheers
      john

  8. JRMN says:

    Thanks for the suggestion John, I’ll give that a try.

  9. Mitchell says:

    Hi Tronixstuff.

    Ive been following this tutorial, its fantastic. Thankyou.

    One Question….
    My LCD doesnt exactly match the pinout you have given. I can match all the pins ok except one. On the LCD data sheet I have

    Pin1= VSS, Ground, 0v
    Pin2= V0, Input voltage for LCD, “–”
    Pin3 = VDD, Supply votage for logic, 5v

    Pin2 is confusing me as it says ‘input voltage’ but then says ‘–’ in the column where the other two have 0v and 5v.

    What should I do with it?

    Data sheet-
    http://docs-europe.electrocomponents.com/webdocs/0887/0900766b80887814.pdf

    Thankyou

    • John Boxall says:

      Hello
      What an odd LCD unit. According to max ratings on page 4, you can feed it between 0 and supply voltage. So try 5V, as you would already have 5V for pin 3.
      John

  10. Rich Carthew says:

    Hi John,

    Once again, thanks for a well thought through tutorial!

    Quick question with the LiquidCrystal lcd constructor – Is it possible to pass in analog A1 & A2 pins [set as pinMode(A1, OUTPUT) etc in void setup] in place of digital 5 and 6?
    E.g. LiquidCrystal lcd(12, 8, 7, A2, A1, 4); I’d like to free up digital 5 and 6 for PWM use, don’t wish to use the 4094 & have all the other digital pins in use. (ATmega 328)

    Any hints/code appreciated-
    Cheers and thanks!
    Rich

    • John Boxall says:

      Theoretically yes, but I haven’t tried it so cannot be sure. However a lot easier method would be to use an I2C-interface LCD, then you are only using A4/5 and 5V/GND. Adafruit sell an i2c LCD backpack (http://www.adafruit.com/products/292) which solves the problem neatly.
      cheers
      john

      • Rich Carthew says:

        Thanks John,
        I hadn’t seen those backpack units before – interesting solution and not too bad a $10 a pop. I did try to code the analog pins to do the digital connection prior to posting, however without much success. If I do find out anything interesting, I’ll reply again here for others benefit.
        Cheers,

        Rich

  11. Rich Carthew says:

    Hi John.
    OK got it working via a hint from liudr on Arduino Forum- thought to post it here in case anyone else finds it useful.

    For anyone else wanting to use an analog pin or two instead of digital pins here’s a summary:
    Beginning with a tested and working LCD module set up in the standard way:
    1. Change (and pass in as arguments) the analog pin numbers you want as their respective digital pin numbers in the lcd function at the top of the sketch e.g. LiquidCrystal lcd(12, 8, 7, 16, 15, 4) where 16 = analog A2 & 15 = analog A1 respectively.
    2. Ensure you rewire correctly once you make the change! (I had the two from the LCD crossed over in error on A1 & A2 instead of the other way as determined by the numbers in 1 (long day),
    3. Set their pinMode’s as OUTPUT e.g. pinMode(15, OUTPUT); would be for Analog 1 == can use pinMode(A1, OUTPUT); as equivalent.

    [i]side note: There is no need to digitalWrite HIGH any analog pins used for this particular example (just in case it occurs for you to do so, as there is mention of doing that in some sections on using analog pins as digital outputs when reading about Arduino. digitalWrite(pin#, HIGH) would be to turn an LED or something on (via a resistor in series) etc.
    [/i]
    Cheers,

    Rich

  12. Nuno says:

    i have a glcd k0108 and a arduino mega, i conect all the wires and make upload of ks0180 and the lcd don´t do anything on serial monitor showm me this Serial initialized
    ——————————————————————–
    GLCD Lib Configuration: glcd ver: 3 glcd_Device ver: 1 gText ver: 1
    Panel Configuration:ks0108
    Pin Configuration:ks0108-Mega
    ——————————————————————–
    GLCD:ks0108 DisplayWidth:128 DisplayHeight:64
    Chips:2 ChipWidth:64 ChipHeight:64
    CSEL1:33(PIN_C4) CSEL2:34(PIN_C3)
    RW:35(PIN_C2) DI:36(PIN_C1) EN:37(PIN_C0)
    D0:22(PIN_A0) D1:23(PIN_A1) D2:24(PIN_A2) D3:25(PIN_A3)
    D4:26(PIN_A4) D5:27(PIN_A5) D6:28(PIN_A6) D7:29(PIN_A7)
    Delays: tDDR:320 tAS:140 tDSW:200 tWH:450 tWL:450
    ChipSelects: CHIP0:(33,0×1, 34,0×0) CHIP1:(33,0×0, 34,0×1)
    Data mode: byte
    ——————————————————————–
    Diag Loop: 1
    Initializing GLCD

    please help

  13. Rich Carthew says:

    I had the same issue Nuno, with exactly the same readout.

    As John indicates, perhaps double check your wiring is correct- and also that the connections are actually good- that was my problem- I had it hooked up on a breadboard correctly, but one of my hookup wires was faulty when I tested them all for continuity.

  14. Mr. Sharkey says:

    Thanks so much for these excellent tutorials, I’ve learned so much more here than anywhere else.

    Regarding parallel LCD’s, I am interested in experimenting with 8-bit mode, but am unable to find any code examples that will allow me to initialise the display in this manner. Can you point me at some tips and tricks for using 8-bit mode? Everyone seems to be phobic about using up the additional four pins, but I have a few to spare and would like to see if there are any advantages to it over 4-bit.

    Thanks, and please keep making us smarter, one tutorial at a time!

  15. Mr. Sharkey says:

    A follow-up report on 8-bit mode:

    Although the display doesn’t refresh any faster that I can see, it is more stable. Under 4-bit mode, I was seeing the individual pixels blink on and off in a scrolling pattern when the contrast was set very low (Vo controlled by a PWM pin on the Arduino) or when viewing the display from an odd angle above. Now that blinkyness is gone. I’m well satisfied, and the use of the additional pins doesn’t affect my project in the least.

  16. sudip says:

    controlling each pixel was great, it make good graphics
    thanks, learning sth new.

  17. Runes says:

    I’m having an issue with a KS0108B panel, in that adjustments to the pot make no difference to the screen, it’s always blank. I’m using a 10k pot, with the slider (middle pin) on pin 3 of the LCD, left pin of the pot to gnd, and the right pin to pin 18 of the LCD. Any idea what I may be doing wrong?

  18. David says:

    Great tutorial. For those who try this out, PAY HEED TO THE COMMENT ABOUT CHECKING THE BACKLIGHT VOLTAGE. IF THE BACKLIGHT VOLTAGE IS LESS THAN 5V, DON’T MAKE A CONNECTION BETWEEN PINS 2 AND 15.

    After seeing John’s comment on Sparkfun’s LCD-00255 display, I hooked one up as shown in the first circuit diagram above. This put 5V to the backlight, which apparently fried it. Only then did I read that the max backlight voltage is 4.7 V for that display. A current-limiting resistor probably would have saved it.

    Oh well, it was worth it to learn how to use the display. They’re cheap.

  19. David says:

    Update on above comment….

    The backlight on my Sparkfun LCD-00255 display was NOT fried. There must be some sort of overvoltage protection.

    A different display (a Microtivity IM161) came with a 1N4004 diode with instructions to use it to drop the voltage for the backlight. The 1N4004 has a forward voltage drop of 0.8V, bringing the backlight voltage down to 4.2V. I tried the diode with the Sparkfun display, and the backlight works. Nice.

  20. Jack says:

    Thanks for your turtorial,it helps me a lot.

  21. Rain says:

    Hello John!
    I find your tutorial very informative, like seeing light at the end of a tunnel. :)
    However, i have a few clarifications, which is really helpful for a school project.
    1. i want the text generation to be activated by a button. like, once i press the button, the text will be shown.
    2. i want a random number to be included as well, for example, “you are (random number from 0 to 70) years old”.
    what kind of modifications in the code should i do?

    • John Boxall says:

      It is not a modification, you need to write your own sketch. Review the first few chapters as well as the LCD chapter and you will learn the knowledge to do what you want.

  22. Adrian Lopez says:

    Concerning the KS0108B parallel interface:

    You have one leg of the potentiometer connected to pin 18 (VEE), another leg connected to ground, and the center pin connected to pin 3 (A0) of the KS0108B. The KS0108B’s datasheet, on the other hand, shows one leg of the potentiometer connected to +5V, another connected to pin 18 (VEE), and the potentiometer’s center pin again connected to pin 3 (A0). Why did you set it up the way you did?

    Also, why do you have 5V for the LCD’s backlight when the datasheet says 4.2V?

    I’m new to electronics, so forgive me if my questions appear naive.

    • John Boxall says:

      If using the Sparkfun model as demonstrated it works as described. Furthermore this model has a 5V backlight. If using LCDs from other sources you may need to modify things.

      • Adrian Lopez says:

        I’m looking at the SparkFun model of the LCD (sku LCD-00710). It’s the same datasheet as the one you linked to above, with 4.2V for the backlight (see page 3) and the contrast potentiometer connected to +5V instead of ground (see page 9). I’m trying to understand the discrepancy: Does the wiring work either way or am I not reading the datasheet correctly?

      • John Boxall says:

        OK I built it up again using my instructions and it worked. Either the screens are 5V tolerant or Sparkfun have changed the specification since I wrote the article (January 2011). If you are unsure use a resistor to drop the voltage to 4.2V.
        cheers
        john

      • Adrian Lopez says:

        Thank you. I’ll try following the datasheet as best I can, as I suspect it may indeed be a different revision. This might explain not only the backlight voltage differences, but also the differences in the wiring of the contrast potentiometer (per SparkFun’s claim that the latest module “includes the negative voltage circuitry on board.”).

  23. johnmach says:

    Hi John,

    Thanks a lot for your great and clearly tutorial about LCD Crystal. I’m trying to make a module using HEF 4094 in the middle of Arduino and LCD. I see that in your tutorial, there’s a problem about the timing when using 4094 IC.

    I googled by myself and find an interesting forum post on how to fix this problem.
    Here’s the forum post: http://arduino.cc/forum/index.php?topic=115987.0
    And here’s the library: https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads

    The problem caused by the library in Arduino SDK. This library has optimized some code to boost the display performance up.

    Hope people will read this comment and don’t worry about using shift register IC to drive LCD crystal (like me).

    johnmach
    (from http://machtudong.vn)

  24. Rick says:

    I Really love these tutorials, is challenging and I learn. I think my question has been answered above but want to verify as these 2×16 LCD displays are expensive. I have a Topway LMB162ABC. It appears that the Backlight is 4.9 v if I read the spec correctly. Want to very that a 1n4001 will work as if I understand it will drop the 5v input to 4.2 v for the backlight. I appoligize in advance if my post is trivial.

    • John Boxall says:

      Hi Rick, thanks for your feedback. The diode will give you a 0.7V voltage drop.
      cheers

      • Rick says:

        I was able to get the tutorial done and am quite pleased and will try some other parts of the examples. Found out that the backlight on my Topway LMB162abc will work just fine with the 3.3v pin from the Arduino Uno. Did have issue with black boxes on the second line of the display at first, found that my solder job for the male 16 pin header was not good on three pins. Hope you have more tutorials planned, I would love to see a do it yourself WiFi from scratch.

      • John Boxall says:

        Nice to hear you’re enjoying it. DIY WiFi? No hope :)

  25. Emmanuel says:

    sir how about with a password with keypad?

  26. Valmond says:

    Hi John,

    Just wanted to let you know that your tutorials are very well written and very helpful. This tutorial got me up and running with an LCD(24×2, no back light)I scavenged from a dead Northern Telecom/Meridian office telephone. Made in Australia no less! None of the other libraries I tried seem to work, but I got yours to work first time out. For some reason, the program hangs up once uploaded, leaving a display of strange identical characters. But once i press the reset button, everything works smoothly. I think it’s times to run through all your Arduino tutorials from the beginning.

    Thanks.
    (From New Brunswick, Canada)

  27. Mirko says:

    I have a 20×4 lcd display w/backpack I2C and backlight seems to be not working. Is there any way to test it or in case to repair it ? Otherwise it working. Anybody’s help will be appreciated. Thanks, Mirko

Trackbacks/Pingbacks


Leave a Reply

Receive notifications of new posts by email.

The Arduino Book

Arduino Workshop

Für unsere deutschen Freunde

Dla naszych polskich przyjaciół ...

Australian Electronics!

Buy and support Silicon Chip - Australia's only Electronics Magazine.

Use of our content…

%d bloggers like this: