Connect a Sparkfun Serial LCD to your project

Most applications have some kind of user feedback. In many cases a simple led is all you need. But in some cases you want more verbose feedback. This is where a display comes in.
Although you can use a fancy OLED display that can even show video all we want in most cases is just plain characters. This tuturial shows how you can easily add a 2 x 16 character LCD display to your A-blocks application.

For this tutorial we use a SparkFunLcd serial LCD screen with 2 x 16 characters and a backlight. The LCD only has only 3 connections on the back. The connections are exposed through a 3 pin jst connector and 3 standard 0.1" header holes as follows.

1. 5V
2. GND
3. RX

Connecting to a digital port
To connect to the serial LCD we use a small A-blocks prototype board. First we solder 3 pin headers on the serial LCD. On the prototype board we solder a 3 pin female receptor. We then simply stick the prototype board on the back of the serial LCD. Like this:
The code to drive the LCD looks like this:

#include <NewSoftSerial.h>

NewSoftSerial lcd(5, 2); // Connected to D5

void setup()
{
  lcd.begin(9600);
  backlightOn();
  clearScreen();
}

void loop()
{  
  selectLineOne();
  delay(100);
  lcd.print("Hello world");
  selectLineTwo();
  delay(100);
  lcd.print(millis()/2);
  delay(100);
}


/******************** Helper functions *******************************/

void selectLineOne(){  //puts the cursor at line 0 char 0.
   lcd.print(0xFE, BYTE);   //command flag
   lcd.print(128, BYTE);    //position
}

void selectLineTwo(){  //puts the cursor at line 0 char 0.
   lcd.print(0xFE, BYTE);   //command flag
   lcd.print(192, BYTE);    //position
}

void goTo(int position) 
{ //position = line 1: 0-15, line 2: 16-31, 31+ defaults back to 0
  if (position<16){ 
      lcd.print(0xFE, BYTE);   //command flag
      lcd.print((position+128), BYTE);    //position
  }
  else if (position<32){
       lcd.print(0xFE, BYTE);   //command flag
       lcd.print((position+48+128), BYTE);    //position 
  }
  else { goTo(0); }
}

void clearLCD(){
   lcd.print(0xFE, BYTE);   //command flag
   lcd.print(0x01, BYTE);   //clear command.
}

void backlightOn(){  //turns on the backlight
    lcd.print(0x7C, BYTE);   //command flag for backlight stuff
    lcd.print(157, BYTE);    //light level.
}

void backlightOff(){  //turns off the backlight
    lcd.print(0x7C, BYTE);   //command flag for backlight stuff
    lcd.print(128, BYTE);     //light level for off.
}

void serCommand(){   //a general function to call the command flag for issuing all other commands   
  lcd.print(0xFE, BYTE);
}

void clearScreen(){   //a general function to call the command flag for issuing all other commands   
  lcd.print(0xFE, BYTE);
  lcd.print(0x01, BYTE);
  delay(10);
  lcd.print(0xFE, BYTE);
  lcd.print(0x0C, BYTE);
}

    
That's all there is to it. More information about the Sparkfun serial LCD we used can be found in this datasheet Sparkfun Serial LCD
Shopping cart