Serial LCD tutorial

by djmatic

With a simple four wire connection and the code below, the Arduino can drive serial LCD displays.

The main advantage of serial LCD's is that they are easier to wire up, since the Arduino talks to the LCD over a single pair of wires.

This tutorial shows you how to connect a serial LCD made by Matrix Orbital to the Arduino. The code below lets you

  • write text to the display,
  • move the cursor around,
  • turn the backlight on and off, or on for a certain amount of time, and
  • do housekeeping functions like clearing the display or starting a new line.

Connecting the hardware

Only a few connenctions are necessary between the Arduino and the LCD:

Data:

  • connect the Arduino TX pin to the LCD's RX pin (the orange wire in this picture).
  • connect the Arduino GND to the LCD's data ground pin (the purple wire).

Power:

  • connect +5V from the Arduino to the display's +5V power input (the red wire in this picture).
  • connect GND form the Arduino to the display's GND pin (the black wire).

The display shown uses less than 100ma with the backlight on, so it can be connected straight to the Arduino's power supply.

Note: the code below is for a non-inverted TTL serial signal. Some displays, like the one pictured above, have a jumper (J6 in this case) that you have to set in order for this to work.

The Code

While this code is for a Matrix Orbital LCD0821 display, most other serial displays (Scott Edwards' "Serial Backpack", for example) will work in similar fashion, although you may have to change the serial speed, and the "XX" in the "254 XX" command sequence, for clearing the screen and the like. If you bought a SparkFun LED, you should look at http://playground.arduino.cc/Learning/SparkFunSerLCD for the correct command codes. You will need to edit the clearLCD and the newLine methods and change the command codes.

Once you wire the LCD to the Arduino and Upload this code, you'll see "Hello Arduino" on the display.

//  *
//  * ------------
//  *  Control a Serial LCD Display
//  *  
//  *  Tested on a Matrix Orbital model LCD0821 display.
//  *  Other diplays will work but may have slightly different 
//  *  command codes and hardware setups. 
//  *   
//  *  Copyleft 2006 by djmatic
//  *  Updated to be Arduino 1.0 compatible by Dave Hrynkiw, Solarbotics
//  *  July 17 2013
//  *  
//  *   ------------
//  *
//  *


// Declare your program variables here


// Arduino and LCD setup 

void setup()
{
  Serial.begin(19200); // era beginSerial


//       LCD setup commands: uncomment the ones you want to use
//       Note: These codes (i.e. the ones following 254) may have to be changed for 
//       different manufacturer's displays

//       Turn Auto scroll ON
//         Serial.write(254);
//         Serial.write(81);     
//       
//       Turn Auto scroll OFF
//         Serial.write(254);
//         Serial.write(82); 

//       Turn ON AUTO line wrap
//         Serial.write(254); 
//         Serial.write(67);              

//       Turn OFF AUTO line wrap
//         Serial.write(254); 
//         Serial.write(68); 

//       Turn OFF the block cursor    
//       Note that setting both block and underline 
//       cursors may give unpredictable results. 
           Serial.write(254);
           Serial.write(84);               

//       Turn ON the block cursor
//         Serial.write(254);
//         Serial.write(83);  

//       Turn ON the underline cursor
//         Serial.write(254);
//         Serial.write(74);               

//       Turn OFF the underline cursor
//         Serial.write(254);
//         Serial.write(75);               
}




//  MAIN CODE

void loop()
{ 
  //backlightOn(0);  // turn the backlight on all the time

  clearLCD();
  Serial.write(" Hello");  // print text to the current cursor position
  newLine();              // start a new line
  Serial.write("Arduino");
  delay(1000);
}




//  LCD  FUNCTIONS-- keep the ones you need. 

// clear the LCD
void clearLCD(){
  Serial.write(12);
}


// start a new line
void newLine() { 
  Serial.write(10); 
}


// move the cursor to the home position
void cursorHome(){
  Serial.write(254);
  Serial.write(72);
}


// move the cursor to a specific place
// e.g.: cursorSet(3,2) sets the cursor to x = 3 and y = 2
void cursorSet(int xpos, int ypos){  
  Serial.write(254);
  Serial.write(71);               
  Serial.write(xpos);   //Column position   
  Serial.write(ypos); //Row position 
} 


// backspace and erase previous character
void backSpace() { 
  Serial.write(8); 
}


// move cursor left
void cursorLeft(){    
  Serial.write(254); 
  Serial.write(76);   
}


// move cursor right
void cursorRight(){
  Serial.write(254); 
  Serial.write(77);   
}


// set LCD contrast
void setContrast(int contrast){
  Serial.write(254); 
  Serial.write(80);   
  Serial.write(contrast);   
}


// turn on backlight
void backlightOn(int minutes){
  Serial.write(254); 
  Serial.write(66);   
  Serial.write(minutes); // use 0 minutes to turn the backlight on indefinitely   
}


// turn off backlight
void backlightOff(){
  Serial.write(254); 
  Serial.write(70);   
}

Share