Saturday, July 7, 2012

Arduino Character LCD Custom Characters

With a character LCD panel interface constructed and connected to the Arduino, I spent some time getting familiar with the API.  I reviewed the Arduino Liquid Crystal Library Documentation, as well as spent some quality time with Google reviewing sample code.

Since one of the next sensors I'd like to experiment with reads ambient temperature values, I decided to build a custom character to display the symbol for degrees (yes, I realize there may have been a built-in character on the panel to get the job done, but my effort here was for learning, not for finding the optimal solution).  I found the following site which had a convenient 5x8 character conversion tool (HD44780 LCD User-Defined Graphics), and it really made the process easy:


With the character values in hand, I modified the HelloWorld sample file to create the character and display it on the LCD.  Most of of the code references I reviewed online used the 'byte' datatype for working with the custom character.  I found when using this, I got compilation errors for ambiguous method references.  Based on the errors from the IDE, I modified the code to use uint8_t, and that seemed to clear up the compilers complaints.  The final code is seen below.


#include <liquidcrystal.h>

LiquidCrystal lcd(6,7,8,9,10,11);

uint8_t degrees[8] =
{
  B01100,
  B10010,
  B10010,
  B01100,
  B00000,
  B00000,
  B00000,
  B00000
};

void setup()
{
  lcd.createChar(0, degrees);
  lcd.begin(20, 4);

  lcd.print("sparetimenotebook");
  lcd.setCursor(0, 1);
  lcd.print("temperature = 72");
  lcd.write((uint8_t)0);
}

void loop()
{
  lcd.setCursor(0, 2);
  lcd.print(millis()/1000);
}

After uploading the code to the Arduino, everything ran as expected.  The LCD panel is now ready to go for future projects...



No comments:

Post a Comment