Advertisement
LCD-Alpha 5

Alphanumeric LCD with Shift Register on Arduino – part 2

As promised in the first part of this article, now it is time to see in detail how we can use the Alphanumeric LCD Display in our Arduino sketches.

The idea beside this project is to give a simple output empowering the Arduino board behavior without compromising the sketch space.

While the Alphanumeric LCD project for Arduino hardware aims to be simple, cheap and save as much as possible the available I/O ports (only three pins are used), in the same way the software to manage the display should fit leaving a reasonable free amount of memory for data and application firmware.

The AlphaLCD Library for Arduino

First you should download the AlphaLCD library for Arduino. Unzip the file and put the folder in in your Arduino library folder, so it can be used in the sketch, then restart the Arduino IDE.

The AlphaLCD library is derived by the original LCDShift library (you can find it in our previous article) with some small changes including a complete documentation also available separately in two formats: html interactive documentation AlphaLCD HtmlDoc and PDF AlphaLCD Manual. We suggest to take a look at the documentation to see the changes and how the library works.

The sample application

Instead of the traditional – and in most case too obvious – “hello world” sketch we have added a sample application including a couple of source tips to simplify the usage of the LCD output everywhere in the sketch. It is a set of helper mathods to manage the LCD Alphanumeric display through the AlphaLCD library.

The sample application can be downloaded from here with the html interactive documentation and the PDF manual.

This sketch should be intended as a modificable skeleton, where you should add your program; the skeleton sketch, when compiled for Arduino UNO or Arduino Duemilanove, occupy less than 2,5 Kb of the 32Kb of available memory space.

If some of the functions included in the skeleton are not needed in your sketch for display purposes you can easly remove them without problems.

The sample in depth

The sample application is organised in several files:

  • Display_Example.ino is the main source of the sketch
  • Strings.h is an example of a symbolic defined strings set can reduce the space for messages. Defining strings in a separate file is also useful as all the user strings are in a single file.
  • Version.h a simple example of few macro to show the sketch version without spending too much time to create one. If you want to save more space, remove this file from the project and remove the version method and include from the main source file.
  • LCD.h is the file including all the fixed constants and the local methods used to manage the display. Also useful to create a good documentation of the display features local methods. You should modify the three pin number assigned to the LCD Display depending on where you connect it to your Arduino board.

In addition to these files to simplify and get more flexibility in the LCD output functions we have included the use of the Streaming library for Arduino. The downloadable version available from here Streaming has already been optimized for the last versions of the Arduino IDE platforms (1.0 and more) Also for this library it is necessary, after downloading, to copy the unzipped folder in the Arduino library folder as usual. More details on the use and features of this library can be found on the Arduino playground site.

The first part of the sketch source don’t need great comments:

#include <AlphaLCD.h>
#include <Streaming.h>

// Include the LCD helper and the application string
#include "LCD.h"
#include "Strings.h"
#include "Version.h"

//! AlphaLCD class instance for display hardware control
AlphaLCD lcd(2,3,4);

Note that the AlphaLCD library should be instantiated on top of the code in the same way of the traditional ShiftLCD library.

// Your setup initialization here
void setup() 
{
	// Initializes the LCD library
	lcd.begin(LCDCHARS, LCDROWS);
	// Turn LCD On
	lcd.display();
	welcome();
}

// Your Arduino application here
void loop(void)
{

  // ...

}

As you can see the only few calls are in the setup() method where the LCD library will be initalized.
The last method called in the setup function is welcome(), that is one of the methods defined in the third part of the sketch.

In fact, at the end of the area that is supposed should contain the application code of the sketch there is a set of helper methods that can be used anywhere in the rest of the program, helpful to simplify most of the uses the Alphanumeric LCD is supposed for.

The following is a short list of the provided methods in the third section, based on previous experiences. Any author can remove or change those methods as well as adding other in a similar way.

	void enable(bool s);				///< Set the display on or off
	void blink(bool set);				///< Set blink mode
	void error(String m);				///< shows an error message
	void error(String m, int x, int y);		///< shows an error message at specified coordinates
	void message(String m);				///< shows a string message
	void message(String m, int x, int y);	///< shows a string message at specified coordinates
	void clean();					///< clean the LCD screen
	void dec(int n);					///< shows an integer in decimal format
	void hex(int n);					///< shows an integer in hexadeciaml format
	void bin(int n);					///< shows an integer in binary format
	void oct(int n);					///< shows an integer in octal format
	void welcome();					///< shows the program welcome message

For a more detailed explanation of the methods and their usage, and the parameters of every function please refer to the example documentation (available in html Display_Example HtmlDoc or PDF Display_Example Manual format).

4 Comments

Join the conversation!

Error! Please fill all fields.
  • kiranshashi

    The welcome () just prints V 1.x and then disappears, Kind of hard when one is looking for some more information, so instead of that one can add code like this, to print some lines.

    void setup()
    {
    // Initializes the LCD library
    lcd.begin(16, 2); // for a 16 X 2 LCD display

    // Turn LCD On
    lcd.display();
    lcd.setCursor (0,0); // First line, 0th column
    lcd.print(“ElectroSchematics Controllers”);
    lcd.setCursor (0,1); // 2nd line, 0th column
    lcd.print (“Demo of LCD !”);
    //welcome();
    }

  • kiranshashi

    Streaming.h zip file needs to be included as part of the compilation on Arduino Uno.
    https://www.arduino.cc/en/Reference/Libraries
    Pick up the Streaming. zip file and import it.
    Or else the compiler complains about non-existence of Streaming.h library !

  • kiranshashi

    Hello,
    The AlphaLCD.cpp needs a small patch for it to print a full string.
    In the AlphaLCD.cpp write () function, a return ‘1’ is needed.
    Or else – just the first character will be printed !!!!!

    e.g: lcd.print (“hello”) will print just “h”

    /* Here is the modified snippet */
    /**
    * Helper method to send data to the device.
    */
    inline size_t AlphaLCD::write(uint8_t value) {
    send(value, true);
    return 1; <<<<<<<<<<<<<<<< See this.
    }

  • kiranshashi

    Is it possible to add the ‘Last updated’, or ‘Updated on’, or ‘Published on’ date to your article – so that I know when this was published – Just a small suggestion.
    Nice articles . ( One small correction : fix typo in this article – “mathods” )

Looking for the latest from TI?