Consider this Arduino code snippet that creates a custom character and displays it on an LCD:
byte smiley[8] = {
0b00000,
0b01010,
0b01010,
0b00000,
0b10001,
0b01110,
0b00000,
0b00000
};
void setup() {
lcd.begin(16, 2);
lcd.createChar(0, smiley);
lcd.setCursor(0, 0);
lcd.write(byte(0));
}
void loop() {}
What will appear on the LCD screen?
byte smiley[8] = { 0b00000, 0b01010, 0b01010, 0b00000, 0b10001, 0b01110, 0b00000, 0b00000 }; void setup() { lcd.begin(16, 2); lcd.createChar(0, smiley); lcd.setCursor(0, 0); lcd.write(byte(0)); } void loop() {}
Custom characters are stored in CGRAM and displayed by writing their index as a byte.
The createChar function stores the custom pattern in CGRAM slot 0. Writing byte(0) prints that custom character, which looks like a smiling face.
When using the LiquidCrystal library on a standard 16x2 LCD, how many custom characters can you create and store at the same time?
Think about the CGRAM memory size in HD44780 compatible LCDs.
Standard HD44780 LCD controllers have 64 bytes of CGRAM, enough for 8 characters of 8 bytes each.
Look at this Arduino code snippet:
byte heart[7] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100
};
void setup() {
lcd.begin(16, 2);
lcd.createChar(1, heart);
lcd.setCursor(0, 0);
lcd.write(byte(1));
}
void loop() {}The heart shape does not appear as expected on the LCD. What is the most likely cause?
Custom characters require exactly 8 bytes for 8 rows.
The custom character array must have 8 bytes, one for each row. Missing one row causes the LCD to display garbage or nothing.
Choose the correct syntax to define a custom character pattern for an Arduino LCD:
Remember how to declare arrays in Arduino (C++).
Option A correctly declares an array of 8 bytes with curly braces. Option A misses the array size, C has only 7 elements, and D uses parentheses instead of braces.
You want to display two different custom characters side by side on a 16x2 LCD using Arduino. You have created two custom characters at indexes 0 and 1. Which code snippet correctly displays both characters on the first row?
Use write to print custom characters by their byte index.
The lcd.write(byte(n)) function prints the custom character stored at index n. Using print with byte values prints their ASCII codes, not custom characters.
