Tag Archive | "custom"

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.

Posted in arduino, education, LCD, LCD-00710, learning electronics, lesson, microcontrollers, tutorialComments (52)


Subscribe via email

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: