Arduino: How to use custom char with lcd.print()

Post Reply
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Arduino: How to use custom char with lcd.print()

Post by tong »

Normally, the custom character will print with lcd.write() command as the example below:

Code: Select all

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup()
{
  byte customChar0[8] = {31,17,10,04,14,31,31,00};   // Clock
  byte customChar1[8] = {04,14,14,14,31,00,04,00};   // Bell

  // Do assign lcd.begin() before lcd.createChar()
  lcd.begin(20,4);                  // Initialize the lcd for 20 chars 4 lines
  lcd.createChar(0,customChar0);    // Clock
  lcd.createChar(1,customChar1);    // Bell
  lcd.print("Hello");
  lcd.write(byte(0));
  lcd.write(byte(1));
}
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Re: How to use custom char with lcd.print()

Post by tong »

We can access the custom characters with \0 to \7 (Oct).
But the \0 is treated as a string terminator in C.
We can use an \x08 instead of a \0.
It accesses the same memory location due to 'foldback' addressing (x08-x0F).
But \x08 will probably do a backspace on a serial terminal.

Code: Select all

lcd.print("Custom Char 0:\x08");  // Hex
lcd.print("Custom Char 1:\1");    // Oct
lcd.print("Custom Char 2:\2");    // Oct

//We have to force the expression to be evaluated as a String.
lcd.print("Hello World");
lcd.print(String("Hello") + " " + "World");
lcd.print("Hello" + String(" ") + "World");
lcd.print(String("Hello") + "\x32" + "World");
lcd.print("Hello" + String("\x08") + "World");
lcd.print("Hello" + String("\1") + "World");
Post Reply