t r o n i x s t u f f

fun and learning with electronics

Tutorial: Arduino and Colour LCD

This is chapter twenty-eight 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 herePlease note that the tutorials are not currently compatible with Arduino IDE v1.0. Please continue to use v22 or v23 until further notice. 

Welcome back fellow arduidans!

Today we are going to start using a colour LCD with our Arduino for data display and other fun and games. (The monochrome LCD article is here).  Although there are many colour LCDs on the market, the first one we examine is relatively inexpensive and easy to mount and use – the Sparkfun “Color LCD Shield“:

From a hardware perspective there isn’t much to do – only solder in some header pins (which, again, are not included) to enable use with out Arduino boards. The quickest way to solder them in is to insert the pins into another Arduino board or shield, place the LCD shield on top, check the alignment, and solder away. The shield pins used are D3~D5 for the three buttons, and D8, 9, 11 and 13 for the LCD interface. The shield takes 5V and doesn’t require any external power for the backlight. The LCD unit is 128 x 128 pixels, with nine defined colours (red, green, blue, cyan, magenta, yellow, brown, orange, pink) as well as black and white.

Hint – there is a huge database of shields and their details at http://shieldlist.org

From a software perspective, the first thing to do is download and install the library for the LCD shield. There are two versions of the library floating around. First of all, please try the original library from here if your shield was purchased before (around) July 2011. If the example sketches do not work, please try the new library from here. (Why? There are two versions of the shield using different controller chipsets. This was made known to me only recently [August 2011] The example sketches are written for the original library and shield. They may not work with the new library and shield. I don’t have a ‘new’ shield and cannot help you with it). Extract and copy the resulting folder into your ..\arduino-2x\libraries folder. Considering there are more parameters to consider as against using a monochrome LCD, there is nothing to worry about. This shield is easy to use and we will prove this in the following examples.

At the start of every sketch, you will need the following lines:

#include "LCD_driver.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "WProgram.h"
#include "HardwareSerial.h"

as well as the following in void setup():

ioinit();
LCDInit(); or LCDInit(EPSON) or LCDInit(PHILLIPS) // depends on the chipset on your LCD - see below.
LCDContrast(45); // sets LCD contrast (value between 0~63)

With regards to LCDInit(), try it first without a parameter. If the screen doesn’t work, try PHILLIPS or EPSON instead. There are two versions of the LCD shield floating about each with a different controller chipset. If you have the PHILLIPS chipset, my example sketches may not work. This is out of my control, Sparkfun changed the chipset and some functions have not been defined in the new library. The contrast parameter is subjective, however 45 looks good and will be used in the forthcoming demonstrations. We will examine each function with a small example, then use the LCD shield in more complex applications.

Example 28.1 – Text display

The LCD can display 9 rows of 16 characters of text. The function to display text is:

LCDPutStr("text", y,x, foreground colour, background colour);

where x and y are the coordinates of the top left pixel of the first character in the string. Another necessary function is:

LCDClear(colour);

Which clears the screen and sets the background colour to the parameter colour.

(Please note – when referring to the X- and Y-axis in this article, they are relative to the LCD in the position shown below)

For example, to recreate the following display:

… use the following sketch:

/*  Example 28.1 - Colour LCD shield
tronixstuff.com/tutorials > chapter 28 CC by-sa */
// we need all these for the LCD
#include "LCD_driver.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "WProgram.h"
#include "HardwareSerial.h"
void setup()
{
// following two required for LCD
ioinit();
LCDInit();
LCDContrast(45); // sets LCD contrast (value between 0~63)
}
void loop()
{
LCDClear(BLACK);
LCDPutStr("0123456789012345", 0,2, WHITE, BLACK);
LCDPutStr("0123456789012345", 13,2, WHITE, BLACK);
LCDPutStr("0123456789012345", 26,2, WHITE, BLACK);
LCDPutStr("0123456789012345", 39,2, WHITE, BLACK);
LCDPutStr("0123456789012345", 52,2, WHITE, BLACK);
LCDPutStr("0123456789012345", 65,2, WHITE, BLACK);
LCDPutStr("0123456789012345", 78,2, WHITE, BLACK);
LCDPutStr("0123456789012345", 91,2, WHITE, BLACK);
LCDPutStr("0123456789012345", 104,2, WHITE, BLACK);
delay(30000);
}


In example 28.1 we used the function LCDClear();, which unsurprisingly cleared the screen and set the background a certain colour. Let’s have a look at the various background colours in the following example. The LCDClear() function is helpful as it can set the entire screen area to a particular colour. As mentioned earlier, there are the predefined colours red, green, blue, cyan, magenta, yellow, brown, orange, pink, as well as black and white. Here they are in the following example:

Example 28.2

/*  Example 28.2 - Colour LCD shield
tronixstuff.com/tutorials > chapter 28 CC by-sa */
#include "LCD_driver.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>\
#include "WProgram.h"
#include "HardwareSerial.h"
int del = 1000;
void setup()
{
ioinit();
LCDInit();
LCDContrast(45);
}
void loop()
{
LCDClear(WHITE);
LCDPutStr("White", 39,40, WHITE, BLACK);
delay(del);
LCDClear(BLACK);
LCDPutStr("Black", 39,40, WHITE, BLACK);
delay(del);
LCDClear(YELLOW);
LCDPutStr("Yellow", 39,40, WHITE, BLACK);  delay(del);
LCDClear(PINK);
LCDPutStr("Pink", 39,40, WHITE, BLACK);
delay(del);
LCDClear(MAGENTA);
LCDPutStr("Magenta", 39,40, WHITE, BLACK);
delay(del);
LCDClear(CYAN);
LCDPutStr("Cyan", 39,40, WHITE, BLACK);
delay(del);
LCDClear(BROWN);
LCDPutStr("Brown", 39,40, WHITE, BLACK);
delay(del);
LCDClear(ORANGE);
LCDPutStr("Orange", 39,40, WHITE, BLACK);
delay(del);
LCDClear(BLUE);
LCDPutStr("Blue", 39,40, WHITE, BLACK);
delay(del);
LCDClear(RED);
LCDPutStr("Red", 39,40, WHITE, BLACK);
delay(del);
LCDClear(GREEN);
LCDPutStr("Green", 39,40, WHITE, BLACK);
delay(del);
}

And now to see it in action. The colours are more livid in real life, unfortunately the camera does not capture them so well.

Now that we have had some experience with the LCD library’s functions, we can move on to drawing some graphical objects. Recall that the screen has a resolution of 128 by 128 pixels. We have four functions to make use of this LCD real estate, so let’s see how they work. The first is:

LCDSetPixel(COLOUR, Y, X);

This functions places a pixel (one LCD dot) at location x, y of colour COLOUR. Next is:

LCDSetLine(x0, y0, x1, y1, COLOUR);

Which draws a line of colour COLOUR, from position x0, y0 to x1, y1. Our next function is:

LCDSetRect(x0, y0, x1, y1, fill, COLOUR);

This function draws an oblong or square of colour COLOUR with the top-left point at x0, y0 and the bottom right at x1, y1. Fill is set to 0 for an outline, and 1 for a filled oblong. It would be convenient for drawing bar graphs for data representation. And finally, we can also create circles, using:

LCDDrawCircle(x, y, radius, COLOUR, type);

X and Y is the location for the centre of the circle, radius and COLOUR are self-explanatory. However type presents some more options – FULLCIRCLE, OPENSOUTH, OPENNORTH, OPENEAST, OPENWEST, OPENNORTHEAST, OPENNORTHWEST, OPENSOUTHEAST, OPENSOUTHWEST. These parameters determine how much of the circle’s circumference is drawn. FULLCIRCLE natuarlly generates a whole circle, OPENSOUTH generates the top-half of a circle, OPENNORTH generates the bottom half of a circle, and so on.

We will now use these graphical functions in the following demonstration sketch. For photographic reasons, I will stick with white on black for the colours. The sketch is quite long, so it can be downloaded from the following link – Example 28.3. And here is a short clip of the sketch in action:

That is all we have time for in this instalment. Now you have an explanation of the functions to drive the screen, only your imagination is holding you back. In a future instalment we will examine some another type of colour display – the TFT.

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.

February 7, 2011 - Posted by | arduino, education, LCD, microcontrollers | , , , , , , , , , , , , , , , , , , , , , , ,

30 Comments »

  1. Is there an easy way to display images through the lcd colour shield??

    Comment by dany rios | May 25, 2011 | Reply

  2. It seems that i have a problem. I put the sketch in, it compiled without error and downloads to the Arduino Uno without error but it only lits the backlight and then i get some colored pixel on the lcd, nothing else.

    Comment by Juergen Moeders | August 3, 2011 | Reply

    • Hello
      Double-check that there are not any shorts in the soldering between the shield header pins. Otherwise if the example sketches compile and upload without issue, you most likely have a faulty board and should contact the seller.
      cheers
      john

      Comment by John Boxall | August 4, 2011 | Reply

    • Hello again Juergen
      After some research it seems you may have one of the new LCD shields with a different controller chipset. Please download the new library from – https://github.com/jimbloom/ColorLCDShield Then try the sketch again with LCDInit(EPSON) in void setup(). If that doesn’t work, try again with LCDInit(PHILLIPS).
      cheers
      john

      Comment by John Boxall | August 4, 2011 | Reply

      • Thank you, that helped, it has a PHILLIPS chipset. Now I got another problem, it can’t read most of the commands in your sketch … it seems that it hasn’t the text commands implemented and I don’t speak C and don’t know how to get around this.
        But thanks anyway, at least now I know that it isn’t damaged. Guess i have to wait until someone puts the text commands into the library.

        Comment by Juergen Moeders | August 4, 2011

      • Oh no, I didn’t see that. There is the setChar and setStr functions though which may be of use. Curse Sparkfun for updating the chipset.
        john

        Comment by John Boxall | August 4, 2011

  3. Yeah, I will for sure curse Sparkfun for that, maybe I tell them to get the text commands back into the library.
    I saw the setCar and setStr functions but I have no clue how to use them, I tried some things but it didn’t work. And I’m not going to start learning C just for that :(

    Otherwise, your tutorials are excellent and i like them, they gave me some inspiration already and i just have to find another LCD that can display text easier than the Sparkfun one, like a 16×2 character LCD.

    Comment by Juergen Moeders | August 5, 2011 | Reply

    • I’m not too happy about this shield at all… now I will most likely have to rewrite this article.. However thank you for your questions and positive feedback, I really appreciate it.

      Comment by John Boxall | August 5, 2011 | Reply

      • Well, at least you know now that they changed the shield and can change the tutorial before it happens to others.

        I just fiund another LCD, Sparkfun’s SKU:LCD-10097, and I got a question about it. It says that it comes with an ATMega328, does that mean it has already the arduino processor on board? and could I use it instead of the arduino if I uploaded the code to it? Just making sure I get the right LCD, and having the the arduino already on board would be very nice.

        Comment by Juergen Moeders | August 5, 2011

      • Yes, it does contain an Arduino Duemilanove-equivalent in there, but you need an FTDI cable to program it from USB. You can also get serial backpacks separately if you already have a HD4470-compatible LCD. adafruit sell them but their site is down so I can’t find the part number.
        cheers
        john

        Comment by John Boxall | August 5, 2011

      • Thanks, great. Then I get an FTDI cable to program it

        Comment by Juergen Moeders | August 5, 2011

  4. Hi, I just got the board and tried the library, both old and new version, the latter with PHILLIPS and EPSON. Unfortunately it does not seem to work at all – in either version with either parameter. The shield is not defective, though, because this library: https://github.com/TCWORLD/gLCD-Library actually does work. I guess it’s just the init sequence – is there a chance someone will check/update the library explained here? The videos seem to indicate it works faster than the mentioned gLCD library.
    Thanks!

    Comment by hmouse | December 10, 2011 | Reply

    • Hard to say. Sparkfun released the shield using someone else’s design who doesn’t work for Sparkfun, so there is no logical connection between the shield and the library. Perhaps try and contact the author of the library.

      Comment by John Boxall | December 10, 2011 | Reply

  5. Does any UK company sell this model such as farnell?

    Comment by epictronics | December 23, 2011 | Reply

  6. Hi again, what pin should I connect my potentiometer to so that I can change the contrast?

    Comment by Jay | December 25, 2011 | Reply

    • Can’t be done with this LCD unit.

      Comment by John Boxall | December 25, 2011 | Reply

  7. Hi John, I am currently making a pong game and am refreshing the screen once in my while loop. I notice that the function

    void LCDClear(int color);

    takes about one second each time, hence slowing the graphics (paddle and ball) significantly. Is there anyway I could speed up the time it takes to clear the screen? Thanks!

    Comment by epictronics | January 8, 2012 | Reply

  8. Thanks John, I may have to buy one of these!

    Comment by epictronics | January 9, 2012 | Reply

  9. I don’t have one of these displays yet and I am looking at various options so I have a question.

    For you basic write strings code, how big is it. With all of those includes, I am concerned that the program gets a bit large. Yes, I know I could figure this out but my programming PC is not connected to the internet so there would be a lot of work to get all of this over there and compiled and I cannot find a memory stick anywhere.

    These can be had through Ebay for as little as $3.49 which is awesome for a small production versus the $14.95 sparkfun price. I get that I might have issues with the chip and I am okay throwing $10 at it to find out. But if the code is quite large, I am thinking about using a separate 328 to drive this and just pass it serial data. While this will add a little to the cost, it is still significantly below the $14.95.

    I have a couple of the 4D displays (144 and a 2.4) and they rock but they are also VERY expensive.

    Comment by Wade | January 26, 2012 | Reply

    • The sketch for example 28.1 uses 3634 (out of a possible 32256) bytes of memory. So you still have some space to play with on a ’328.

      Comment by John Boxall | January 26, 2012 | Reply

  10. Nice!

    Thanks for that. One more question.

    Is it possible with the current library to rotate thru the various orientations, 90,180,270 degrees?

    Comment by Wade | January 27, 2012 | Reply

  11. Will you be updating your sketches to work with the new Arduino 1.0 IDE?

    Comment by Nick | January 27, 2012 | Reply

    • Too early to say. Planning a site redesign soon, it may be a part of that process. In the meanwhile people can run v23 and v1.0 on the same machine.

      Comment by John Boxall | January 27, 2012 | Reply

  12. Good post and hope you guys can help me out in solving the problem I’m facing

    I just want to bring up a logo on this Spark fun LCD however I’ve almost pant close to 8 hrs without any success.

    I’ll appreciate if someone can help me with this.

    !!! I’m using Arduino Uno R3 with this Sparkfun LCD shield”

    Comment by AJ | February 17, 2012 | Reply

    • You could draw it using individual pixels, lines, rectangles etc. If you want to display a .jpg or similar, get an LCD from 4DSystems.com.au with the microSD card and GFX processor
      John

      Comment by John Boxall | February 17, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 2,588 other followers