Lesson 6

Lesson 6: Displaying Characters on 8x2 LCD

In this lesson you will learn how to display characters on an optional 8x2 Liquid Crystal Display (LCD) by using the ApeirosIoExpander class library.

Prerequisites

Before starting this lesson be sure to follow the Initial Setup & Configuration. Also, we strongly recommend that you take some time to read "Getting Started with Arduino and Genuino products". You must purchase and install an 8x2 LCD by plugging it into the JP18 header as shown below.

Introduction

Apeiros provides a surface mount 7x2 female header so that users can attach an optional, low cost 8x2 LCD. A factory-installed multi-turn trim pot can be used to adjust the LCD contrast. The LCD is capable of displaying up to (8) characters across (2) lines for a total of (16) characters. However, the LCD is capable of storing up to a total of (40) characters for each line by using its built-in memory. By default, the LCD is initialized to shift the cursor to the right each time a character is printed.

Apeiros Class Library Functions

The ApeirosIoExpander class library contains (8) functions for controlling an attached 8x2 LCD.

void lcdAutoScroll();
void lcdNoAutoScroll();
void lcdScrollToLeft();
void lcdScrollToRight();
void lcdClear();
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t * buffer,  size_t size);
void lcdGotoXY(uint8_t x,uint8_t y);

Recall that we need to first declare an instance of the ApeirosIoExpander class library and then call the begin() function from within the setup() function. Calling the begin() function will properly initialize the library along with a connected LCD.

// Declare Apeiros class instances.//
/***********************************/
ApeirosIoExpander io;
/***********************************/

void setup() {
  // put your setup code here, to run once:

  // Begin ApeirosIoExpander.
  io.begin();
  
}

The ApeirosIoExpander library is derived from the Arduino Print class and so we can print directly to the LCD by calling the print() function. In the code snippet listed below a string of characters, "Hello", is printed to the LCD. When printing a string of characters you must encapsulate the string of characters in double quotes, "".

void loop() {
  // put your main code here, to run repeatedly:
 
  // Call print to write a string of characters to the LCD.
  io.print("Hello");
  
  delay(500);  // Delay for a specified amount of time in milliseconds
}

If you want to print a single character, then encapsulate the character using single quotes, ''. For example, let's print the dollar sign character, $, to the LCD.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Call print to write a single character to the LCD.
  io.print('$');
  
  delay(500);  // Delay for a specified amount of time in milliseconds
}

The 8x2 LCD has (2) lines and we can control the location of its cursor by using the lcdGotoXY(x,y) function. Suppose we want to print "Hello" on the first line and "World!" on the second line. The value of x specifies the character position which can be any integer from 0 to 7. The value of y specifies the line position which can be either 0 or 1.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Call print to write a string of characters to the LCD.
  io.print("Hello");

  // Call lcdGotoXY() to change cursor position.
  io.lcdGotoXY(0,1)

  io.print("World!");
  
  delay(500);  // Delay for a specified amount of time in milliseconds
}

Now suppose that you want to clear out the LCD in preparation for printing new characters. Simply call the lcdClear() function.

void loop() {
  // put your main code here, to run repeatedly:

  // Call print to write a string of characters to the LCD.
  io.print("Hello");

  // Call lcdGotoXY() to change cursor position.
  io.lcdGotoXY(0,1)

  io.print("World!");

  delay(500);  // Delay for a specified amount of time in milliseconds

  // Clear the LCD.
  io.lcdClear()
  
  delay(500);  // Delay for a specified amount of time in milliseconds
}

The LCD's built-in cursor is automatically incremented each time you print a character. A total of (8) characters can be displayed on each of the (2) lines. However, the LCD is capable of storing up to a total of (40) characters for each line by using its built-in memory.

EXPERIMENT


 

What happens when you print the following string of (10) numbers: :"0123456789" to the LCD? Can you think of a way to display all (10) numbers? HINT: Take a look at the Apeiros_LCD example sketch. More specifically, examine the usage of the lcdScrollToRight() and lcdScrollToLeft() functions.

Can you create a custom sketch that displays the state of the front center IR sensor? More specifically, display the word "Blocked" when the center sensor is blocked and the word "Clear" when the sensor is clear.