LiquidCrystal Library Functions Tour Part 2

What good is an LCD if you can’t scroll text, make stuff blink or do some fancy showmanship? Let’s dive into some of the fun and useful functions in the LiquidCrystal library.

This lesson is an overview of some useful functions to control your LCD with an Arduino using the LiquidCrystal library.

It will cover:

  1. How to scroll text left and right across the display
  2. The difference between the write() and print() functions
  3. How to clear the display
  4. Using the Autoscroll() function

How to scroll text left and right across the display

Have you ever seen Time Square in New York City? Those awesome bigger than life displays scrolling gigantic images across the screen. Or those awesome news tickers wrapping text across the corner of a building?

Well, let’s do that on a smaller scale.

The easy way to make this happen is the scrollDisplayLeft() and scrollDisplayRight() functions. As the name of the function implies – it moves the ENTIRE displayed image to the left (or right) – and it moves it exactly ONE segment over.

Lets say we have the word “MAMA” displayed on the screen.

mama displayed tight left

..and we use scrollDisplayRight() – and we only use it once. It will basically pick up the screen display and shift it right one segment.

Mama shifted Right

But usually we will want to shift the display more than one segment – we want it to continuously scroll across the screen. To do that, we could use the code below:

// include the library code: #include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("MAMA"); delay(1000); } void loop() { //move the display one segment to the right lcd.scrollDisplayRight(); //Delay to see the movement happen delay(250); }

Since the loop() runs over and over again it will continually invoke the scrollDisplayRight() function – thus every time through the loop, the display shifts to the right – creating the continuous scroll effect we are looking for.

It will even scroll text right off the screen – eventually the text will re-appear again though – like a PacMan from one side of the screen to the other.

As you would imagine, scrollDisplayLeft() will do the same thing, but in the opposite direction.

Now let’s look at an alternate method of displaying text on the screen.

The difference between the write() and print() functions

So far we have been using the print() function to display text on the screen, but there is also a write() function. The difference between the two is somewhat subtle, but bares a discussion.

The print() function will display whatever data you put into the parenthesis. Let’s take the following example:

int myNum = 97; lcd.print(myNum);

This would print the value of ledPin -> the number 97 would be displayed.

Now, what would happen if we tried to do this with the write() function?

int myNum = 97; lcd.write(myNum);

In this case we get a lowercase “a” sent to the LCD. What the heck is going on you ask?

The write() command sends a character to the LCD display. Characters are a data type, just like integer is a data type. What is cool about the character data type is that each character is actually encoded with a number. The encoding used is called ASCII (American Standard Code for Information Interchange).

If we look at the ASCII table, we will find that 97 actually encodes for a lowercase “a”. This is why write(97) will display the letter “a”.

So let’s cut to the short of this before it blows up in our face. You should almost always use the print() function unless you plan to send data over the serial port. This includes if you need to use the serial monitor window to see the LCD output. Why this is the case is out of the scope of this tutorial – so you are just going to have to trust my poor judgement…

How to Clear the Display

So you have displayed a bunch of stuff on the display and you want to get rid of it all – easy as pie. Just use the clear() function to wipe the display clean. This function also returns the cursor to the home position back in the top left corner of the display.

Using the Autoscroll() function

If you recall from the previous tutorial, you know that the cursor auto-advances to the end of the displayed text. What if we wanted to have the cursor stay put and new text to “push-out” from where the cursor is positioned?

The Autoscroll() function will allow you to do just that. This is how it works. First you use setCursor() to pick the location of the cursor. Then you use the autoscroll() function.

// include the library code: #include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { // set up the LCD's number of columns and rows: lcd.cursor(); lcd.begin(16,2); } void loop() { // set the cursor to (16,1): lcd.setCursor(16,1); // set the display to automatically scroll: lcd.autoscroll(); // print from 0 to 9: for (int i = 0; i < 10; i++) { lcd.print( i ); delay(500); } // clear screen for the next loop: lcd.clear(); } //close loop

So what is going on here? Well, we set the cursor to the far right of the LCD on the second row using:

lcd.setCursor(16,1);

Then we invoke autoscroll():

lcd.autoscroll();

The next block of code is a for loop that prints one number at a time to the LCD.

for (int i = 0; i < 10; i++) { lcd.print( i ); delay(500); }

The first time through this loop, the LCD would look like this:

autoscroll 0 (1)

The second time through the loop, you can see that the next number (#1) has “pushed”, the first number over to the left.

autoscroll 1

A couple more times through the loop, I think you are getting the gist. The cursor acts as the “origin” point, and all new text send to the display is shifted left.

autoscroll infi

To stop the autoscrolling functionaltiy, simply use:

noAutoscroll(); //stop autoscrolling

Now, according to the autoscroll() documentation, the direction in which the new text scroll is set using the leftToRight() function or the rightToLeft() – these functions ( which we have not covered) set the direction you want your text to display.

Unfortunately – I was unable to get text scrolling to the right of the cursor. Very likely I am the one all jacked up – test it yourself and let me know!

Conclusion:

Well, that pretty much sums up the Arduino LiquidCrystal library functions. The amount of work the LiquidCrystal library does behind the scenes is enormous, and we have David Mellis and Limor Fried to thank for creating and modifying this library to make our lives easier.

The real power of the library comes when using all these functions together to build interface devices with your LCD – for example, making a button selector to pick options on the LCD. Future tutorials will cover some specific examples.

Challenge:

  1. Try using all of the function sin the library in a single sketch
  2. See if you can figure out the behaviour of autoscroll and let me know why I am so incompetent.

Further Reading:

  • write()
  • print()
  • autoscroll()
  • LCD Addressing – This provides a great in depth discussion about some what goes on in the background

Tag » Arduino Liquidcrystal.h Commands