LCD: SparkFun’s serLCD and NewSoftSerial

http://www.youtube.com/watch?v=skqVepTQMRU

The video above shows SparkFun’s serLCD in action using a library (serLCD.h) which inherits all of the NewSoftSerial library functions and then adds a few of its own.

The library is set out in full below (with download) but first up, my first sketch using the serLCD (which might help others get a feel for the device). This is a super simple sketch which just runs through most of the major functions of the serLCD.

As simple serLCD sketch

 
/*
 * Serial LCD Test
 *
 * created by Ben @ Cape Ealing
 * 30 January 2010
 *
 */

// include the 'newsoftserial' library
#include "NewSoftSerial.h"

// define the pin through to send the data
#define lcdTransmitPin 16

// define the data rate
#define lcdDataRate 9600 

// Line 1 and 2 character positions
// 1: character positions from from 128 (char 1) to 143 (char 16)
// 2: character positions from from 191 (char 1) to 207 (char 16)
#define Line1 127
#define Line2 191

// create the lcdSerial object
NewSoftSerial lcdSerial =  NewSoftSerial(255, lcdTransmitPin);

void setup()
{

  // start the serial connection
  lcdSerial.begin(lcdDataRate);

  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print(1, BYTE);  // clear the LCD 0x01 or 1
  delay(500);
  
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print((Line1+1), BYTE); // 1st line, character position 1
  lcdSerial.print("Hello world");
  delay(2000);
  
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print((Line2+1), BYTE); // 2nd line, character position 1
  lcdSerial.print("from capeMirror");
  delay(2000);
  
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print((Line2+16), BYTE); // 1st line, character position 16
  lcdSerial.print("!");
  delay(3000);

}



void loop()
{

 
  // clear the LCD and print a line
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print(1, BYTE);  // clear the LCD 0x01 or 1
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print((Line1+1), BYTE); // 1st line, character position 1
  lcdSerial.print("Testing...");
  delay(1000);
  
  /////////////////////////////////////////////  
  // backlight testing  
  /////////////////////////////////////////////  
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print((Line2+1), BYTE); // 2nd line, character position 1
  lcdSerial.print("backlight");
  delay(1000);

  int backlightpc = 0;   // run through the 30 different backlight settings
  for (int backlightvar=128; backlightvar < 158; backlightvar++){
    lcdSerial.print(124, BYTE);  // backlight instruction
    lcdSerial.print(backlightvar, BYTE);  // backlight instruction  
    delay(100);
  }
  
  /////////////////////////////////////////////  
  // cursor testing  
  /////////////////////////////////////////////  
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print((Line2+1), BYTE); // 2nd line, character position 1
  lcdSerial.print("line cursor");
  delay(2000);
  
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print(14, BYTE);  // line cursor 0X0E or 14
  delay(2000);

  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print((Line2+1), BYTE); // 2nd line, character position 1
  lcdSerial.print("block cursor");
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print(13, BYTE);  // block cursor: 0X0D or 13
  delay(2000);
  
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print(12, BYTE);  // cursor off 0X0C or 12

  /////////////////////////////////////////////  
  // scroll testing  
  /////////////////////////////////////////////  

  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print((Line2+1), BYTE); // 2nd line, character position 1
  lcdSerial.print("scrolling screen");
  delay(2000);
  
  for (int scrollvar = 1; scrollvar < 16; scrollvar++){
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print(28, BYTE);  // scroll right 0X1C or 28
  delay(250);
  }

  for (int scrollvar = 1; scrollvar < 16; scrollvar++){
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print(24, BYTE);  // scroll left 0X18 or 24
  delay(250);
  }
 
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print(1, BYTE);  // clear the LCD 0x01 or 1
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print((Line1+1), BYTE); // 1st line, character position 1
  lcdSerial.print("Hello world");
  lcdSerial.print(254, BYTE);  // instruction
  lcdSerial.print((Line2+1), BYTE); // 2nd line, character position 1
  lcdSerial.print("from capeMirror!");
  delay(5000);

}


A serLCD library

This library is designed to use Mikal Hart's excellent NewSoftSerial library. In fact, the library inherit all of the NewSoftSerial functions and then adds a few of its own. The relevant code to 'inherit' the NewSoftSerial library is the following section in the cpp file:

 
/*
	serLCD.h
	v. 1
	by Ben @ Cape Ealing

	A library to interface with SparkFun's serLCD modules
	with the NewSoftSerial library.
*/

#include "WProgram.h"
#include "NewSoftSerial.h"
#include "serLCD.h"

/*
 *  Initialise the class
 *
 *	The class initialises and passes the transmit pin
 *	value to the NewSoftSerial class
 */

serLCD::serLCD(uint8_t transmitPin)
: NewSoftSerial(255, transmitPin, false)
{
}

As can be seen, the library is initiated by calling serLCD(SerialTransmitPin). This serLCD library then inherits the NewSoftSerial library and initiates a NewSoftSerial object with a receive pin of 255 (ie a non-existent pin because the LCD will not be sending data that needs to be 'received' by the arduino), and the transmit pin that is going to be used for the LCD.

The full range of commands which can be used by the library (like backlight(50%)) are detailed in this sketch - which is the one running in the video. The primary one is: display(line number, character position, text message) - although the library (because it inherits NewSoftSerial which in turn inherits Print) can also just receive the print command (as demonstrated in the sketch).

/*
 * Serial LCD libary 
 * for use with NewSoftSerial and SparkFun's serLCDs
 *
 * created by Ben @ Cape Ealing
 * 30 January 2010
 *
*/

// include the libraries
#include "NewSoftSerial.h"
#include "serLCD.h"

// define the pin through to send the data
#define lcdTransmitPin 16

// create the lcdSerial object
serLCD mySerLCD(lcdTransmitPin);

void setup()
{

  // start at the required baud rate
  mySerLCD.begin(9600);

  mySerLCD.clearLCD();
  delay(500);
  mySerLCD.display(1, 1, "Hello world!");
  delay(2000);

//  mySerLCD.saveSplashScreen();
//  mySerLCD.toggleSplashScreen();

//  BEWARE: setting the baudrate higher than 14400 can 
//  mean the device fails.  Then you need to read the 
//  documentation and use the mySerLCD.controlR() method
//  to reset.
//  mySerLCD.setBaudRate(14400);  
//  delay(2000);
//  mySerLCD.begin(14400);
//  delay(2000);
}

void loop()
{

  /////////////////////////////////////////////  
  // Hello world  
  /////////////////////////////////////////////  

  mySerLCD.clearLCD();
  delay(500);
  
  // display(line number, character position, text)
  mySerLCD.display(1, 1, "Hello world");
  delay(2000);

  // display(line number, character position, text)
  mySerLCD.display(2, 1, "from my serLCD");
  delay(2000);

  // display(line number, character position, text)
  mySerLCD.display(2, 16, "!");
  delay(1000);

  /////////////////////////////////////////////  
  // On and Off  
  /////////////////////////////////////////////  

  mySerLCD.on();
  delay(1000);

  mySerLCD.off();
  delay(1000);
  
  mySerLCD.on();
  delay(1000);

  /////////////////////////////////////////////  
  // cursor testing  
  /////////////////////////////////////////////  

  mySerLCD.clearLCD();
  mySerLCD.display(1, 1, "Cursor testing");
  delay(2000);

  mySerLCD.display(2, 1, "Line");
  mySerLCD.position(2, 5);
  mySerLCD.lineCursor();
  delay(2000);

  mySerLCD.display(2, 1, "Block");
  mySerLCD.position(2, 6);
  mySerLCD.blockCursor();
  delay(2000);

  mySerLCD.display(2, 1, "No cursor");
  mySerLCD.noCursor();
  delay(2000);

  mySerLCD.display(2, 1, "Move cursor:");
  mySerLCD.position(2, 13);
  mySerLCD.lineCursor();
  delay(1000);
  mySerLCD.moveCursorRight();
  delay(1000);
  mySerLCD.moveCursorRight();
  delay(1000);
  mySerLCD.moveCursorRight();
  delay(1000);
  mySerLCD.moveCursorLeft();
  delay(500);
  mySerLCD.moveCursorLeft();
  delay(500);
  mySerLCD.moveCursorLeft();
  delay(500);
  mySerLCD.noCursor();

  /////////////////////////////////////////////  
  // backlight testing  
  /////////////////////////////////////////////  
  
  mySerLCD.clearLCD();
  delay(500);

  mySerLCD.display(1, 1, "Backlight test");
  delay(1000);

  // turn off backlight display (0%)
  mySerLCD.backlight(0);  
  delay(1000);

  // turn backlight on full (100%)
  mySerLCD.backlight(100);  
  delay(1000);

  // move to particular position (line, character position)
  mySerLCD.position(2, 4);
  // print at that position
  mySerLCD.print("% brightness");

  // run through setting of 0 - 100%
  for (int backlightpc=0; backlightpc < 101; backlightpc++){
    mySerLCD.backlight(backlightpc);  
    delay(200);
    mySerLCD.display(2, 1, backlightpc);
  }
  delay(1000);

  /////////////////////////////////////////////  
  // scroll testing  
  /////////////////////////////////////////////  

  mySerLCD.clearLCD();
  delay(500);

  mySerLCD.display(1, 1, "Scroll testing");
  mySerLCD.display(2, 1, "Right, then left");
  delay(1000);

  delay(2000);
  
  for (int scrollvar = 1; scrollvar < 16; scrollvar++){
  mySerLCD.scrollRight();  // instruction
  delay(250);
  }

  for (int scrollvar = 1; scrollvar < 16; scrollvar++){
  mySerLCD.scrollLeft();  // instruction
  delay(250);
  }
}


Download the serLCD library

The full library can be downloaded here.

ben

post a comment...

you must be logged in to post a comment.