Displaying Data to the LCD Panel with an Arduino
This web page describes how to connect the wiring harness to an Arduino, and how to display a simple message.
Other References
Basic Commands
Create an LCD instance called lcd connected to pins p1
through p6
LiquidCrystal lcd( p1, p2, p3, p4, p5, p6);
Initialize lcd by specifying the number of columns (ncol) and rows (nrow)
lcd.begin(ncol,nrow);
Move the insertion point to column jcol and row irow
lcd.cursor(jcol,irow);
Print a string
lcd.cursor("Your message here");
Print an integer
int value= 5;
lcd.cursor(value);
Print an floating point value, but with display limited to two decimal places
float value= 5.2;
lcd.cursor(value);
Refer to the HelloWorld
example that comes with the Arduino
IDE for an example implementation. Additional details are provided in the
tutorial at the
official Arduino site.
Work-around to displaying floats with variable precision
C programmers know about the sprintf
function and may be tempted
to use sprintf
to format floating point numbers with the standard
control over precision and width. Unfortunately, as of early 2013, the standard
Arduino library does not include a full implementation of the stdio
library, such that the Arduino implementation of sprintf
cannot
be used to display floating point numbers.
The following example shows some failed and some successful ways of displaying
floating point values. The key is to use the basic conversion utilities
dtostrf
and dtostre
instead of the higher
level sprintf
function.
// File: LCD_format_numbers.ino // // Use dtostrf and dtostre to format floating point numbers // for display on a 20x4 character LCD panel // // Gerald Recktenwald, gerry@pdx.edu // 2013-02-11 #include// include the library code: // Create a LiquidCrystial instance connected to pins 8 through 13 LiquidCrystal lcd(8, 9, 10, 11, 12, 13); void setup() { lcd.begin(20, 4); // set up the LCD's number of columns and rows: } void loop() { update_LCD(lcd, 1, 2, 3.14, 6.57e-3); delay(2000); lcd.clear(); delay(500); } // --------------------------------------------------------------------- // Display values in different formats. Some work, some don't // In the last row, show how dtostrf and dtostre work // void update_LCD(LiquidCrystal lcd, int v1, int v2, float v3, float v4) { char display_string[40] = ""; // Buffer for 40 character width char float_str3[16] = ""; // Buffer for string value of float char float_str4[16] = ""; // -- Row 0: display numbers in default format with one space between lcd.setCursor(0,0); lcd.print(v1); lcd.print(" "); lcd.print(v2); lcd.print(" "); lcd.print(v3); lcd.print(" "); lcd.print(v4); // -- Row 1: display all numbers as 2 digit integers with sprintf // and no conversion of floats. This does not work lcd.setCursor(0,1); sprintf(display_string,"%2d %2d %2d %2d",v1,v2,v3,v4); lcd.print( display_string ); // -- Row 2: display integers with two digits, and attempt to use the // standard sprintf notation to convert floats to strings. // This DOES NOT WORK because the Arduino stdio library does not // include a full implementation of sprintf. lcd.setCursor(0,2); sprintf(display_string,"%2d %2d %4.2f %7.3e",v1,v2,v3,v4); lcd.print( display_string ); // -- Row 3: display integers as 2 digit integers and use dtostrf // and dtostre to convert floats to strings. Note that float_str1 // and float_str2 have been declared 16 characters wide. So this // warning: no precautions against buffer overflow are taken. // Default formatting of the scientific string is obtained with // the 0x00 value for __flags in dtostre lcd.setCursor(0,3); dtostrf(v3,4,2,float_str3); // Convert v3 to string in float_str3 dtostre(v4,float_str4,3,0x00); // Convert v4 to string in float_str4 sprintf(display_string,"%2d %2d %s %s",v1,v2,float_str3,float_str4); lcd.print( display_string ); }