Sunday, 13 October 2013

Displaying large text on LCD displays

EDIT: The constructor has changed since v1.0.0 of the BigCrystal library. See my subsequent post for details.

I have been playing around with a 20x4 character LCD display on an Arduino and discovered that it's possible to use custom characters to display large text. I wasn't that happy with the code that I saw, so I decided to write a library to make large characters easy to use.

This approach works by using the custom character functionality on Hitachi HD44780 compatible displays. There are eight characters that can be programmed to display any combination of pixels on a 5x7 matrix. Characters in adjacent positions can "tile" together these special characters to make double height characters.

Code Development

The original code on which the library is based was initially posted on a thread in the Arduino forums, and further documented in an instructable. Michael Pilcher created the original fonts and code, and this was further refined by "Mark S" that encoded the font data in arrays stored in the Arduino's program memory. These guys did all of the hard work, I've just repackaged it all to make it a bit easier.

I've enhanced the code in a number of ways. Firstly, I've changed the storage of fonts to save about 60 bytes of program memory. I did this by creating separate arrays based on character width to reduce the "placeholder" entries in the original array for characters wider than the standard three characters.

I've also added some symbols that were not present in the original code. I've added parentheses, colon, quotation marks, semicolon, plus, minus and a few others. I've also made characters slimmer than the standard three character width for symbols where it makes sense. For example: the colon, semicolon, comma and full stop are one character wide, whilst parentheses and the minus sign are two characters wide.

Most importantly, I've created two libraries to make large characters easy to use in your sketches. They are BigCrystal, for LCDs hooked up directly to an Arduino and BigCrystalTWI for LCDs that are connected using a Two Wire Interface (i.e. I2C). These libraries extend the standard Arduino LiquidCrystal library and the LiquidTWI library respectively.

Usage

As the BigCrystal libraries extended existing libraries, and add three methods:

  • writeBig to write a single large character;
  • printBig to print a string of large characters; and
  • widthBig to determine the width of a single character
A simple example using the BigCrystal library is shown below:

 #include <BigCrystal.h>  
 #include <LiquidCrystal.h>
  
 // Set up according to your LCD pins  
 BigCrystal lcd(22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32);  

 void setup() {  
  lcd.begin(20, 4);  
  lcd.printBig("10:22", 0, 0);  
 }  

 void loop() {  
 }  

The same example, using the BigCrystalTWI is:

 #include <Wire.h>  
 #include <LiquidTWI.h>  
 #include <BigCrystalTWI.h>  

 // Set up according to your LCD pins  
 BigCrystalTWI lcd(0);  

 void setup() {  
  lcd.begin(20, 4);  
  lcd.printBig("10:22", 0, 0);  
 }  

 void loop() {  
 }  


Further Work

I've had to duplicate much of the code between the two libraries. I had considered splitting out the common functions into a separate library, but this would mean that users would have to import two libraries into their Arduino IDE instead of one, so I went for ease of use. The libraries are pretty small and I'll just have to be conscientious and make changes to both libraries when I'm fixing bugs or adding features.

There is a non-standard LiquidCrystal library from Adafruit that can use SPI communication. To use SPI, the default LiquidCrystal library needs to be replaced with this non-standard version. I haven't adapted Adafruit's library for large characters.

Testing

I have done my testing on the the BigCrystal library, however, I no longer have access to an I2C backpack and so haven't tested the BigCrystalTWI library. Everything compiles and I'm confident that it will work. I'd appreciate it if somebody with compatible hardware can try it out and report back.