Sub topic 10
Defining Custom Characters on an LCD
What you need to know first
This chapter assumes that you can upload an Arduino sketch, use setup() and loop(), and wire a 16x2 HD44780 LCD in 4-bit mode. It also assumes that you have used the LiquidCrystal library to print ordinary text.
A character LCD does not draw arbitrary pixels across the whole screen. It displays characters from a built-in font. However, an HD44780 controller also provides eight temporary character slots. You can fill these slots with your own 5-by-8-pixel designs.
How custom characters work
Each custom character has eight rows. Each row contains five useful bits, so each character is 5 pixels wide and 8 pixels tall. The three unused bits in each row should be zero.
The LiquidCrystal library provides two important methods:
createChar(slot, pattern)copies a pattern into a custom-character slot.write(slot)displays the character stored in that slot.
The slot number must be from 0 through 7. The custom characters are stored in the LCD controller's character generator RAM, often called CGRAM. They are lost when the LCD loses power, so your sketch should recreate them every time it starts.
| Slot number | Meaning |
|---|---|
| `0` through `7` | The eight available custom characters |
A custom character is different from printing a number or a string. Use lcd.print() for text and lcd.write() when you want to output a character from a custom slot.
Creating a simple icon
The following sketch defines a heart icon in slot 0 and displays it beside text. In a binary row, a 1 turns on a pixel and a 0 leaves it off.
#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);
// Store the pattern in custom-character slot 0.
lcd.createChar(0, heart);
lcd.setCursor(0, 0);
lcd.print("Status: ");
lcd.write(byte(0));
}
void loop() {
}The call lcd.createChar(0, heart) must happen after lcd.begin(). The LCD needs to be initialized before the library can write the pattern into its memory.
The expression byte(0) makes the intention clear: write the character in slot 0. Without this cast, some Arduino code can treat 0 as a string terminator or select an unexpected overload.
Using several slots
You can define up to eight patterns, then place them in slots 0 through 7. For example, a signal-strength icon could use four slots with progressively more filled bars.
byte bar0[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b11111
};
byte bar1[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b11111,
0b11111
};
void setup() {
lcd.begin(16, 2);
lcd.createChar(0, bar0);
lcd.createChar(1, bar1);
lcd.setCursor(0, 0);
lcd.write(byte(0));
lcd.write(byte(1));
}In a complete sketch, declare lcd before this code and define the remaining slots as needed. Keep each row within five pixels. If you call createChar() again for an occupied slot, the old design is replaced everywhere that slot is currently displayed.
Common errors
A missing icon often comes from one of these causes:
- Calling
createChar()beforelcd.begin(). - Using a slot outside the range 0–7.
- Calling
lcd.print(0)instead oflcd.write(byte(0)). - Forgetting that a reset or power loss removes the custom patterns.
- Expecting the icon to remain after another pattern overwrites its slot.
Choose the eight slots as part of your display design. If a screen needs more than eight icons at the same time, combine related symbols into one glyph or use a different display technology.
Exercise
What happens if a sketch calls lcd.createChar(0, newPattern) after it has already displayed a character from slot 0?
Show answer
Slot 0 is replaced with newPattern, so every visible or future use of slot 0 uses the new design.