Sub topic 10
Defining custom characters on an LCD
A 16x2 HD44780 character LCD ships with a fixed set of letters, digits, and symbols. It has no arrows, battery icons, or hearts, and the degree sign and accented letters depend on the ROM variant of your module. The controller lets you define up to eight glyphs of your own at run time. This chapter covers how those glyphs are stored, how to design and draw them, and where they go wrong.
This chapter assumes you can already print text with LiquidCrystal as shown in the earlier chapter on 4-bit parallel wiring. You should also be comfortable with binary literals such as 0b01110 and with for loops over arrays.
How the controller stores a custom character
The HD44780 has three memory areas that matter here:
- CGROM is read-only memory holding the built-in glyphs. You cannot change it.
- DDRAM holds the character codes currently shown on screen, one byte per cell.
- CGRAM is writable memory holding eight user-defined glyphs.
Each cell on the display is a grid of 5 columns by 8 rows of pixels. A glyph is therefore eight bytes, one per pixel row, from top to bottom. Only the lower five bits of each byte are used. Bit 4 is the leftmost pixel and bit 0 is the rightmost. A 1 turns the pixel on and a 0 leaves it off.
Putting a character on screen takes two steps. First you write the eight bytes into a CGRAM slot numbered 0 to 7 with createChar(). Then you write the slot number to the display as if it were a character code, with write(). The controller sees code 0 to 7 in DDRAM and reads the pixels from CGRAM instead of CGROM.
Designing a glyph
Sketch the glyph on a 5x8 grid first. The heart from the earlier example looks like this, with # for a lit pixel:
| Row | Pixels | Byte |
|---|---|---|
| 0 | `.....` | `0b00000` |
| 1 | `.#.#.` | `0b01010` |
| 2 | `#####` | `0b11111` |
| 3 | `#####` | `0b11111` |
| 4 | `#####` | `0b11111` |
| 5 | `.###.` | `0b01110` |
| 6 | `..#..` | `0b00100` |
| 7 | `.....` | `0b00000` |
The 0b binary literal lets the byte array match the picture, which makes mistakes easy to spot. Rows 0 and 7 are left blank here. Most built-in glyphs leave the bottom row empty because the cursor underline is drawn there. If you fill row 7, the cursor will merge with your glyph when it is enabled.
The basic pattern
This is the complete sketch for the heart. It matches the pattern used in the rest of the tutorial.
#include <LiquidCrystal.h>
// RS, E, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000
};
void setup() {
lcd.begin(16, 2);
lcd.createChar(0, heart); // store the glyph in slot 0
lcd.setCursor(0, 0);
lcd.write(byte(0)); // draw slot 0
}
void loop() {}Three details in this sketch matter.
- Call
createChar()afterbegin().begin()initializes the controller. Glyphs written before it can be lost. - Use
write(byte(0)), notprint(0).print(0)converts the number to the text "0" and draws the digit.write()sends one raw byte. Thebyte(0)cast also avoids an ambiguity error on older Arduino cores, where a barewrite(0)could match a pointer overload. - Call
setCursor()aftercreateChar(). To write glyph data, the library switches the controller's address pointer to CGRAM. It does not always switch back. If you print immediately aftercreateChar(), your text may land in CGRAM and corrupt a glyph. CallingsetCursor()orclear()restores the pointer to display memory. The sketch above does this.
Eight slots, and the mirror at 8 to 15
The controller reserves eight slots, numbered 0 to 7. Character codes 8 to 15 are a mirror of 0 to 7, so write(byte(8)) draws the same glyph as write(byte(0)). This matters for one reason: the code 0x00 is a null byte, and some string functions stop at it. If you embed a custom character in a C string, use code 8 instead of 0. For example, "Temp\x08" is safer than "Temp\x00", which ends the string early.
If you need more than eight different glyphs, you can redefine slots while the program runs. Pixels already on screen are not frozen, though. The display redraws a cell from CGRAM every refresh cycle. If you redefine slot 3, every cell currently showing code 3 changes immediately. This is the basis for simple animations, and it is also a source of surprises when two screens share a slot.
A practical example: a bar graph
A 16-cell row gives only 16 steps of resolution if each cell is on or off. Custom characters raise it to 80 steps. You define five glyphs, with 1, 2, 3, 4, and 5 columns filled. A bar is then made of full cells, then one partial cell, then blanks.
Connect a potentiometer's wiper to A0, with its outer pins to 5V and ground. Wire the LCD as before.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
// Slot n holds a glyph with (n + 1) columns filled from the left.
for (byte cols = 1; cols <= 5; cols++) {
byte row = 0;
for (byte c = 0; c < cols; c++) {
row |= (1 << (4 - c));
}
byte glyph[8];
for (byte r = 0; r < 8; r++) {
glyph[r] = row;
}
lcd.createChar(cols - 1, glyph);
}
lcd.setCursor(0, 0);
lcd.print("Level");
}
void loop() {
int value = analogRead(A0);
int total = map(value, 0, 1023, 0, 80); // 16 cells x 5 columns
lcd.setCursor(0, 1);
for (byte cell = 0; cell < 16; cell++) {
int filled = total - cell * 5; // columns to fill in this cell
if (filled >= 5) {
lcd.write(byte(4)); // completely filled cell
} else if (filled > 0) {
lcd.write(byte(filled - 1)); // partial cell
} else {
lcd.write(' '); // empty cell
}
}
delay(100);
}Every pass through loop() rewrites all 16 cells of row 1, including the blank ones. That is what removes leftover bar segments when the value drops. The earlier chapter on updating readings covers this overwrite-everything approach in more detail.
The glyph loop builds each pattern with bit arithmetic: 1 << (4 - c) sets bit 4 for the first column, bit 3 for the second, and so on. It would work equally well with five hand-written arrays. The loop is shorter and shows how a pattern can be computed.
Storing many glyphs: a lookup table
When you have several designs, keep them in a two-dimensional array and load them in one loop.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte GLYPHS[3][8] = {
{ 0b00100, 0b01110, 0b11111, 0b00100, 0b00100, 0b00100, 0b00100, 0b00000 }, // up arrow
{ 0b00100, 0b00100, 0b00100, 0b00100, 0b11111, 0b01110, 0b00100, 0b00000 }, // down arrow
{ 0b01110, 0b11011, 0b10001, 0b10001, 0b10001, 0b10001, 0b11111, 0b00000 } // empty battery
};
void setup() {
lcd.begin(16, 2);
for (byte i = 0; i < 3; i++) {
byte buffer[8];
for (byte r = 0; r < 8; r++) {
buffer[r] = GLYPHS[i][r];
}
lcd.createChar(i, buffer);
}
lcd.setCursor(0, 0);
lcd.write(byte(0));
lcd.write(byte(1));
lcd.write(byte(2));
}
void loop() {}The copy into buffer is deliberate. Some library versions declare createChar(uint8_t, uint8_t[]) with a non-const parameter. Passing a const array directly would then fail to compile. Copying into a local array works with all of them. If you want to save RAM, you can store the table in flash with PROGMEM and read it with pgm_read_byte() during the copy. On a board with 2 KB of RAM, 24 bytes of glyph data is rarely worth the extra code.
Using custom characters with an I2C backpack
LiquidCrystal_I2C provides the same createChar() method, and the design rules are identical. The usual call order is init(), backlight(), then createChar(), then setCursor(). Method names and argument types differ slightly between forks of the library. Check the header file of the version you installed if the sketch does not compile. Some forks use write(0) and some require write(byte(0)). The byte cast works in both.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| A digit `0` or other text appears instead of the glyph | `print(0)` was used | Use `write(byte(0))` |
| A random or built-in character appears in place of the glyph | `createChar()` ran before `begin()`, or the display was reset | Call `createChar()` after `begin()` and again after any re-initialization |
| Text appears in the wrong place or the glyph is corrupted right after `createChar()` | Address pointer still points at CGRAM | Call `setCursor()` or `clear()` after the last `createChar()` |
| A string is cut off at the glyph | Code `0x00` ends a C string | Use code 8 for slot 0 inside strings |
| Glyph looks too wide, with a stray column | A bit above bit 4 was set, or a hand-typed byte is wrong | Check each row has at most five bits, such as `0b01110` |
| Underline cursor merges with the glyph | Row 7 has pixels set | Leave row 7 blank, or turn the cursor off with `noCursor()` |
| Glyphs vanish after a brownout or `begin()` call | CGRAM is volatile | Reload all glyphs whenever you re-initialize the display |
CGRAM is lost when the display loses power. Nothing is stored between runs, so your sketch must define the glyphs on every start.
Summary of the working model
- A glyph is eight bytes, one per row, using the lower five bits of each.
createChar(slot, array)writes it to CGRAM slot 0 to 7.write(byte(slot))displays it.- Redefining a slot changes every cell that currently shows it.
- Always call
setCursor()orclear()aftercreateChar(). - CGRAM is volatile. Reload glyphs after every initialization.
Exercise
You define a custom glyph in slot 2 with createChar(2, arrow), then run lcd.print(2) at the start of loop(). The screen shows the digit "2" instead of your arrow. What is wrong, and what is the correct call? Also, after you fix this, you redefine slot 2 as a different shape later in the program. What happens to cells that already show it?
Show answer
print(2) converts the number to text and draws the digit character "2" from the built-in ROM. To draw the custom glyph, send the raw slot number with lcd.write(byte(2)). When slot 2 is redefined with createChar(2, newShape), every cell that already contains code 2 changes to the new shape right away, because the display reads pixels from CGRAM on every refresh and does not keep a copy of the old glyph.
Exercise
Modify the bar graph sketch so that the bar fills from the left using only 8 cells (40 steps of resolution) and shows a percentage from 0 to 100 in cells 9 to 12 of the same row. Which line of the sketch must change in the map() call, and why must the percentage text be padded or overwritten?
Show answer
Change the map() output range to 0 to 40 (8 cells x 5 columns) and loop over 8 cells instead of 16. Print the percentage after the bar with lcd.setCursor(9, 1) and lcd.print(percent), where percent = map(value, 0, 1023, 0, 100). The text must be padded, for example by printing spaces after the number or by printing it with a fixed width, because a shorter number such as "7" does not erase the leftover digits from an earlier "100" in the same cells.