Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using a SparkFun™ serial LCD with an Arduino

Similar presentations


Presentation on theme: "Using a SparkFun™ serial LCD with an Arduino"— Presentation transcript:

1 Using a SparkFun™ serial LCD with an Arduino
living with the lab Using a SparkFun™ serial LCD with an Arduino SparkFun Serial Enabled 20x4 LCD (part number: LCD-09568) with Arduino Uno

2 Transmits serial data from digital I/O pin to LCD (plugged into Pin 2)
living with the lab LCD wiring back view RX = Receives serial data from Arduino GND = ground wire 5V = power wire Transmits serial data from digital I/O pin to LCD (plugged into Pin 2) Ground 5V Power front view

3 LCD and Arduino together
living with the lab LCD and Arduino together

4 Programming living with the lab
Text: “Louisiana Tech University” is displayed, with character locations chosen to center the text on the first two lines of the LCD Integers: The text “i=“ is printed followed by an integer that varies from 1 to 10 Floating Point Numbers: The text “x=“ is printed followed by a number with four numerals printed to the right of the decimal; the value here ranges from to

5 Cursor Position & Printing to the 20x4 LCD
living with the lab Cursor Position & Printing to the 20x4 LCD Row: 1 Column: 5 mySerial.write(197); // move cursor to row 1, position 5 mySerial.write("University"); // print a text string starting at (1,5) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

6 A Complete Sample Sketch
See the Parallax website ( for more details. Custom characters and other features are available for this LCD. When developing your sketch, print things to the LCD screen only once if they do not change. For example, the text in this sketch is printed once in setup(), while the variables are printed repeatedly in loop(). A Complete Sample Sketch living with the lab /*****************************************************************************/ /* Example program using a Sparkfun 4x20 LCD to output text and numbers. */ /* The following screen is printed: */ /* */ /* | Louisiana Tech | */ /* | University | */ /* | | */ /* | i=8 x= | where 1<=i<=10 and <=x<= */ /* The program prints out both integers and floating point numbers. The */ /* floating point number is printed to four decimal places */ /* */ /* A partial listing of commands recognized by the LCD is provided below. */ /* See the data sheet for the LCD on the Parallax web site for more details. */ /* mySerial.write(254) - control command must be sent before commands below */ /* mySerial.write(1) - clear display and move cursor to top left */ /* mySerial.write(8) - turn display off */ /* mySerial.write(12) - turn display on / hide cursor */ /* mySerial.write(13) - blinking cursor on */ /* mySerial.write(14) - underline cursor on */ /* mySerial.write(16) - move cursor left one space */ /* mySerial.write(20) - move cursor right one space */ /* mySerial.write(24) - scroll left one space */ /* mySerial.write(28) - scroll right one space */ /* mySerial.write(124) - control command must be sent before commands below */ /* mySerial.write(128) - backlight off */ /* mySerial.write(157) - backlight fully on */ /* row and position commands are shown below for all 80 character positions */ /* ROW 0: 128=(0,0) 129=(0,1) 130=(0,2) 131=(0,3) 132=(0,4) */ /* =(0,5) 134=(0,6) 135=(0,7) 136=(0,8) 137=(0,9) */ /* =(0,10) 139=(0,11) 140=(0,12) 141=(0,13) 142=(0,14) */ /* =(0,15) 144=(0,16) 145=(0,17) 146=(0,18) 147=(0,19) */ /* ROW 1: 192=(2,0) 193=(2,1) 194=(2,2) 195=(2,3) 196=(2,4) */ /* =(2,5) 198=(2,6) 199=(2,7) 200=(2,8) 201=(2,9) */ /* =(2,10) 203=(2,11) 204=(2,12) 205=(2,13) 206=(2,14) */ /* =(2,15) 208=(2,16) 209=(2,17) 210=(2,18) 211=(2,19) */ /* ROW 2: 148=(1,0) 149=(1,1) 150=(1,2) 151=(1,3) 152=(1,4) */ /* =(1,5) 154=(1,6) 155=(1,7) 156=(1,8) 157=(1,9) */ /* =(1,10) 159=(1,11) 160=(1,12) 161=(1,13) 162=(1,14) */ /* =(1,15) 164=(1,16) 165=(1,17) 166=(1,18) 167=(1,19) */ /* ROW 3: 212=(3,0) 213=(3,1) 214=(3,2) 215=(3,3) 216=(3,4) */ /* =(3,5) 218=(3,6) 219=(3,7) 220=(3,8) 221=(3,9) */ /* =(3,10) 223=(3,11) 224=(3,12) 225=(3,13) 226=(3,14) */ /* =(3,15) 228=(3,16) 229=(3,17) 230=(3,18) 231=(3,19) */ /* To print variables to the LCD, you need to use the mySerial.print() */ /* command instead of the mySerial.write() command. For example, the command */ /* mySerial.print(x,4) will send variable "x" to the LCD with four digits */ /* beyond the decimal point */ /* Note that you may need to unplug the ground wire to the LCD and reset */ /* the Arduino after uploading sketch to make it display correctly */ #include <SoftwareSerial.h> // use the software serial library SoftwareSerial mySerial(3,2); // receive data at pin 3; transmit data at pin 2 void setup() { mySerial.begin(9600); delay(500); // set data rate to 9600 baud; wait for bootup mySerial.write(254); mySerial.write(1); // clear screen & move to top left position mySerial.write(254); mySerial.write(131); // move cursor to row 0, position 3 mySerial.write("Louisiana Tech"); // write a text string starting at (0,3) mySerial.write(254); mySerial.write(197); // move cursor to row 1, position 5 mySerial.write("University"); // write a text string starting at (1,5) mySerial.write(254); mySerial.write(212); // move cursor to row 3, position 0 mySerial.write("i= x="); // write text string at (3,0) } void loop() { float x=0.0; for (int i=1;i<=10;i++) { x=x ; // add to variable x mySerial.write(254); mySerial.write(216); // move cursor to row 3, position 4 mySerial.print(i); // print i at the current cursor position mySerial.write(" "); // write blanks to cover previous printing mySerial.write(254); mySerial.write(224); // move cursor to row 3, position 12 mySerial.print(x,4); // print x to 4 decimal places delay(1000); // delay 1 second between printing numbers

7 Looking at Sections of the Sketch
See the Parallax website ( for more details. Custom characters and other features are available for this LCD. When developing your sketch, print things to the LCD screen only once if they do not change. For example, the text in this sketch is printed once in setup(), while the variables are printed repeatedly in loop(). Looking at Sections of the Sketch living with the lab /*****************************************************************************/ /* Example program using a Sparkfun 4x20 LCD to output text and numbers. */ /* The following screen is printed: */ /* */ /* */ /* | Louisiana Tech | */ /* | University | */ /* | | */ /* | i=8 x= | where 1<=i<=10 and <=x<= */ /* The program prints out both integers and floating point numbers. The */ /* floating point number is printed to four decimal places */ /* A partial listing of commands recognized by the LCD is provided below. */ /* See the data sheet for the LCD on the Parallax web site for more details. */ /* mySerial.write(254) - control command must be sent before commands below */ /* mySerial.write(1) - clear display and move cursor to top left */ /* mySerial.write(8) - turn display off */ /* mySerial.write(12) - turn display on / hide cursor */ /* mySerial.write(13) - blinking cursor on */ /* mySerial.write(14) - underline cursor on */ /* mySerial.write(16) - move cursor left one space */ /* mySerial.write(20) - move cursor right one space */ /* mySerial.write(24) - scroll left one space */ /* mySerial.write(28) - scroll right one space */ /* mySerial.write(124) - control command must be sent before commands below */ /* mySerial.write(128) - backlight off */ /* mySerial.write(157) - backlight fully on */ Explains what sketch is expected to do Control Command that must be written before any LCD programming commands. Commands for programming the LCD Control Command that must be written before any LCD backlight programming commands. Commands for programming the LCD backlight Sketch Continues on Next Slide #include <SoftwareSerial.h> // use the software serial library SoftwareSerial mySerial(3,2); // receive data at pin 3; transmit data at pin 2 void setup() { mySerial.begin(9600); delay(500); // set data rate to 9600 baud; wait for bootup mySerial.write(254); mySerial.write(1); // clear screen & move to top left position mySerial.write(254); mySerial.write(131); // move cursor to row 1, position 2 mySerial.print("Louisiana Tech"); // print a text string starting at (0,3) mySerial.write(254); mySerial.write(197); // move cursor to row 1, position 5 mySerial.write("University"); // print a text string starting at (1,5) mySerial.write(254); mySerial.write(212); // move cursor to row 1, position 5 mySerial.write("i= x="); // print text string at (3,2) } void loop() { float x=0.0; for (int i=1;i<=10;i++) { x=x ; // add to variable x mySerial.write(254); mySerial.write(216); // move cursor to row 3, position 4 mySerial.print(i); // print i at the current cursor position mySerial.write(" "); // print blanks to cover previous printing mySerial.write(254); mySerial.write(224); // move cursor to row 3, position 12 mySerial.print(x,4); // print x to 4 decimal places delay(1000); // delay 1 second between printing numbers

8 Sketch Continues on Next Slide
See the Parallax website ( for more details. Custom characters and other features are available for this LCD. When developing your sketch, print things to the LCD screen only once if they do not change. For example, the text in this sketch is printed once in setup(), while the variables are printed repeatedly in loop(). living with the lab Provides a list of command values for each cursor position on the LCD screen /* */ /* row and position commands are shown below for all 80 character positions */ /* ROW 0: 128=(0,0) 129=(0,1) 130=(0,2) 131=(0,3) 132=(0,4) */ /* =(0,5) 134=(0,6) 135=(0,7) 136=(0,8) 137=(0,9) */ /* =(0,10) 139=(0,11) 140=(0,12) 141=(0,13) 142=(0,14) */ /* =(0,15) 144=(0,16) 145=(0,17) 146=(0,18) 147=(0,19) */ /* ROW 1: 192=(2,0) 193=(2,1) 194=(2,2) 195=(2,3) 196=(2,4) */ /* =(2,5) 198=(2,6) 199=(2,7) 200=(2,8) 201=(2,9) */ /* =(2,10) 203=(2,11) 204=(2,12) 205=(2,13) 206=(2,14) */ /* =(2,15) 208=(2,16) 209=(2,17) 210=(2,18) 211=(2,19) */ /* ROW 2: 148=(1,0) 149=(1,1) 150=(1,2) 151=(1,3) 152=(1,4) */ /* =(1,5) 154=(1,6) 155=(1,7) 156=(1,8) 157=(1,9) */ /* =(1,10) 159=(1,11) 160=(1,12) 161=(1,13) 162=(1,14) */ /* =(1,15) 164=(1,16) 165=(1,17) 166=(1,18) 167=(1,19) */ /* ROW 3: 212=(3,0) 213=(3,1) 214=(3,2) 215=(3,3) 216=(3,4) */ /* =(3,5) 218=(3,6) 219=(3,7) 220=(3,8) 221=(3,9) */ /* =(3,10) 223=(3,11) 224=(3,12) 225=(3,13) 226=(3,14) */ /* =(3,15) 228=(3,16) 229=(3,17) 230=(3,18) 231=(3,19) */ /* To print variables to the LCD, you need to use the mySerial.print() */ /* command instead of the mySerial.write() command. For example, the command */ /* mySerial.print(x,4) will send variable "x" to the LCD with four digits */ /* beyond the decimal point */ /* Note that you may need to unplug the ground wire to the LCD and reset */ /* the Arduino after uploading sketch to make it display correctly */ /*****************************************************************************/ mySerial.print() is used instead of mySerial.write() when you are trying to display variables on the LCD. For example, to display “Tech” on the LCD you would use mySerial.write(), but if you wanted to display an integer that increases by one each iteration, you would used mySerial.print(). Sketch Continues on Next Slide

9 Library used for LCD & setting pins to recieve/transmit
living with the lab Library used for LCD & setting pins to recieve/transmit #include <SoftwareSerial.h> // use the software serial library SoftwareSerial mySerial(3,2); // receive data at pin 3; transmit data at pin 2 void setup() { mySerial.begin(9600); delay(500); // set data rate to 9600 baud; wait for bootup mySerial.write(254); mySerial.write(1); // clear screen & move to top left position mySerial.write(254); mySerial.write(131); // move cursor to row 0, position 3 mySerial.write("Louisiana Tech"); // write a text string starting at (0,3) mySerial.write(254); mySerial.write(197); // move cursor to row 1, position 5 mySerial.write("University"); // write a text string starting at (1,5) mySerial.write(254); mySerial.write(212); // move cursor to row 3, position 0 mySerial.write("i= x="); // write text string at (3,0) } void loop() { float x=0.0; for (int i=1;i<=10;i++) { x=x ; // add to variable x mySerial.write(254); mySerial.write(216); // move cursor to row 3, position 4 mySerial.print(i); // print i at the current cursor position mySerial.write(" "); // write blanks to cover previous printing mySerial.write(254); mySerial.write(224); // move cursor to row 3, position 12 mySerial.print(x,4); // print x to 4 decimal places delay(1000); // delay 1 second between printing numbers Control Command that must be written before LCD display commands Set baud rate for LCD (related to speed of data transmissions) Tells the cursor which position LCD to go to Text strings use mySerial.write() with text in “ ”. Command changed from mySerial.write() to mySerial.print() because displaying variable values NOTE: When developing your sketch, print things to the LCD screen only once if they do not change. For example, the text in this sketch is printed once in setup(), while the variables are printed repeatedly in loop(). See the SparkFun™ website ( for more details. Custom characters and other features are available for this LCD.

10 living with the lab After downloading your sketch, you may need to press the Arduino reset button to remove “gibberish” that may be printed on the LCD. reset button


Download ppt "Using a SparkFun™ serial LCD with an Arduino"

Similar presentations


Ads by Google