ECE 110/Equipment/Serial LCD
Contents
Introduction
There are several multi-character LCD displays available. For ECE 110, we will specifically be using a 16x2 display that has a Parallax Serial LCD module attached to it. Among other things, this makes the display a three-pin device versus a 16-pin device! It also allows for brightness control and sound on board.
Leads
- 1 (left): RX (receive)
- 2 (middle): 5 V
- 3 (right): Ground
The CX-Bot has a header for the three-pin serial version; the pins are assigned to Serial3.
Operation
The serial version of the LCD receives characters through a serial bus; for the CX-Bot, these are linked to Serial3 which has pin 14 as its Tx line and pin 15 as its Rx line. The header will allow you to connect Serial3's transmit line to the LCD's receive line. You will create a connection to the display using the SoftwareSerial
library, which provides an object type of the same name:
#include <SoftwareSerial.h>
#define TxPin 14
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
You can then use mySerial.write(VAL)
to send a single raw byte to the display or mySerial.print(STRING)
to send a collection of characters. The codes are given on pp. 8-13 of Product Guide; here are some of the more used ones (codes given in decimal):
- 12: clear the display and move cursor to top left (line 0 position 0). There needs to be at least a 5 ms delay after this.
- 22-25: turn the display on set the cursor (line or not under current location) and blink (blink or not at current location):
- 22 cursor off, blink off
- 23 cursor off, blink on
- 24 cursor on, blink off (default)
- 25 cursor on, blink on
- 17, 18: turn the backlight on, off
- 128-143: move cursor directly to line 0 positions 0-15
- 148-163: move cursor directly to line 1 positions 0-15
Music
The Parallax Serial LCD also has a piezoelectric speaker you can use. If you want to play a note, you basically need to send three codes: duration, octave, and note within octave.
- 208-214: duration from 1/64th to a whole note. Whole notes are 2 seconds long
- 215-219: octave; 215 starts at middle A (440 Hz), each octave doubles the frequency after that
- 220-231: note; 2201 is A, 221 is A#, etc.
- 232: no note; play silence for the duration specified.
Sample Code
Simple
The following example will use the Serial Monitor to receive input from the user; the input will be displayed on the LCD starting with the first character on the top left row (line 0, position 0). If the input has more than 16 characters, the input will wrap to the second line. If the input has more than 32 characters, the 33rd will overwrite the 1st and any more will overwrite other ones that were there before. The LCD starts at the first character every time due to the
mySerial.write(12);
line.
#include <SoftwareSerial.h>
#define TxPin 14
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
delay(100);
mySerial.write(12); // clear
delay(10);
mySerial.write(22); // no cursor no blink
delay(10);
mySerial.write(17); // backlight
delay(10);
}
void loop() {
if (Serial.available() > 0){
mySerial.write(12); delay(5);
String myInput = Serial.readString();
myInput.trim();
mySerial.print(myInput);
}
}
Turn Off
When you are done using the display for the day, you should turn it off before disconnecting it. Here is some code that will do that:
#include <SoftwareSerial.h>
#define TxPin 14
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
void setup() {
mySerial.begin(9600);
delay(100);
mySerial.write(12); // clear
delay(10);
mySerial.write(21); // display off
delay(10);
mySerial.write(18); // backlight off
delay(10);
}
void loop() {
}
Formatted Text and Music
Here is a code that shows how to use buffers and sprintf to create formatted text and how to play music. Note - this gets very annoying very fast, so be sure to have the BareMinimum script prepared for loading...
#include <SoftwareSerial.h>
#define TxPin 14
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
delay(100);
mySerial.write(12); // clear
delay(10);
mySerial.write(22); // no cursor no blink
delay(10);
mySerial.write(17); // backlight
delay(10);
}
void loop() {
int x = random(0, 10);
int y = random(0, 10);
char buffer[16];
sprintf(buffer, "%d + %d = %2d", x, y, x+y);
mySerial.write(12); // clear
delay(10);
mySerial.write(buffer);
mySerial.write(13); // cr
int note = (x+y)%12; // get a number between 0 and 11
note = note + 220; // map [0-11] to [220-231] for note
sprintf(buffer, "%d * %d %% 12 = %2d", x, y, (x+y) % 12);
mySerial.write(buffer);
mySerial.write(211); mySerial.write(216); mySerial.write(note);
delay(500);
}
Notes
References
- Parallax 2 x 16 Serial LCD with Piezo Speaker (Backlit)
- Includes link to Product Guide