Click here to Skip to main content
15,884,989 members
Articles / Internet of Things / Arduino
Tip/Trick

SerialLcd Library for Arduino

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
14 Jan 2014CPOL3 min read 25.8K   318   2   4
Arduino.SerialLcd library

Introduction

Using an LCD is one of the ways to output information in Arduino projects. The main drawback about an LCD is that it occupies too many pins on your Arduino board. But there are some serial implementations such as here. So I got one. Smile | <img src=

Background

If you visit the LiquidCrystal reference page on the Arduino website, you will see that it is based on Hitachi HD44780 (or a compatible) chipset. An LCD needs many pins like data, clock, select, contrast adjustment, etc. which consumes your Arduino resources. However, a serial LCD eliminates the necessity for so many pins. Actually, it uses another microprocessor which drives the LCD. You command this microprocessor from your Arduino board over a stream and it handles the rest for you.

Hardware Setup

Serial LCD has three lines which are GND, Vcc, and Rx. All you need is to connect the Rx (receive) line of the serial LCD to the Tx (transmit) line of the stream on Arduino board. This stream can be either hardware serial or software serial. It is up to you which type of communication to use.

If you decide to use hardware serial, you will use the digital pin1 of your Arduino board as serial transmit. Pin0 is the receive pin of Serial and Pin1 is for transmit. But in that case, I advice you to disconnect Arduino board from your computer, because Serial is used as shared resource for the serial communication between the computer and the board.

If you will use software serial, the only thing is to decide which digital pin to use as serial transmit.

One more thing; it is better to use an external power source for your Arduino board because an LCD drains more power than that computer's USB port can provide, or you take the risk of damaging your Arduino board.

Be aware that you may need to do some soldering if you decide to buy a serial LCD like mine. There is another PCB at the back of the LCD soldered to the LCD's pins.

Image 2

Fritzing designs:

 

Image 3     Image 4

Click to enlarge images.

Sorry for the connections of the LCDs on above images. I couldn't find a serial LCD part in the Fritzing editor, so I have used normal LCDs. Actually, there exist a 3-pin connector on the side of the serial LCD.

Using the Code

You can communicate with this model of serial LCD as here. As you see, commands are one-byte values with some parameters. What I did in this library is to convert them into more meaningful functions.

Assuming that you have placed the library files in the libraries folder of your Arduino IDE, you can include the header file in your sketch. If you will use SoftwareSerial as your communication infrastructure, you have to include its header, too. You can download SoftwareSerial library from the official Arduino website.

C++
#include <SoftwareSerial.h> // SoftwareSerial header
#include <SerialLcd.h>      // SerialLcd header

SoftwareSerial ss(3,4); // rx=3, tx=4
SerialLcd lcd(&ss); // serial lcd 

If you use hardware serial (Serial), you just pass the reference of it to the SerialLcd constructor.

#include <SerialLcd.h>

SerialLcd lcd(&Serial);

Let's continue with software serial.

In the setup function, you initialize the stream (in this case ss) with 9600 baud rate. Then use lcd as the output device.

C++
ss.begin(9600); 

Usage is straightforward. Displaying a string:

C++
lcd.println("first line");
lcd.println("second line");

Clear screen and move cursor to position 0:

C++
lcd.clr();  

Moving cursor to a specific location needs some explanation. If your LCD is 16x2 type, then moving cursor to location 20 places cursor on the 5th column of the second row. Position index starts from 1.

C++
lcd.setCursorPos(20);
lcd.print("X");

You can send any ASCII character and there is an extension list for the printable characters. You can find this list in this document.

C++
lcd.sendChar(0xF7); // greek pi

Moving cursor left or right:

C++
lcd.moveCursor(SL_DIRECTION_RIGHT);
lcd.moveCursor(SL_DIRECTION_LEFT);

Showing/hiding underline cursor:

C++
lcd.showUnderlineCursor(1); // show underline cursor
lcd.showUnderlineCursor(0); // hide underline cursor 

Showing/hiding blinking cursor:

C++
lcd.showBlinkCursor(1); // show blinking cursor
lcd.showBlinkCursor(0); // hide blinking cursor 

Scrolling screen left or right by 1 step:

C++
lcd.scrollScreen(SL_DIRECTION_RIGHT);
lcd.scrollScreen(SL_DIRECTION_LEFT);

Turn the display on/off:

C++
lcd.displayOn(0); // off
lcd.displayOn(1); // on 

Points of Interest

My library implementation depends on a specific product, but most of them are very similar to this one. When you buy another, you can use this library with a small or no change. Commands are defined in the header file. All you have to do is to compare these commands with yours.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Mevoo Ltd
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs there a backpack? Pin
Alain Peralta31-Mar-14 18:12
Alain Peralta31-Mar-14 18:12 
AnswerRe: Is there a backpack? Pin
Vedat Ozan Oner1-Apr-14 0:58
professionalVedat Ozan Oner1-Apr-14 0:58 
GeneralRe: Is there a backpack? Pin
Alain Peralta1-Apr-14 13:17
Alain Peralta1-Apr-14 13:17 
GeneralRe: Is there a backpack? Pin
Vedat Ozan Oner1-Apr-14 20:19
professionalVedat Ozan Oner1-Apr-14 20:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.