Pages

Friday, December 10, 2010

Arduino Output : LCD Modules, Part 2 - Software

On the previous posting, I talk about LCD Modules on the hardware and how to hook up a LCD module. On this posting, I will talk about setting up the display using Arduino LCD library to drive the LCD module. Almost all my sketches and testing are done with a breadboard version of Arduino with a LCD template.

Here are my template of the LCD library for Arduino :-


#include <LiquidCrystal.h>
// My wiring for LCD on breadboard
// format is RS, Enable, DB4, DB5, DB6, DB7
LiquidCrystal lcd(12, 11, 5, 6, 7, 8);


void setup() { 
     // LCD format is Col,Row for 16 columns and 2 rows
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("LCD Ready...");




  lcd.setCursor(0,1);
  lcd.print("Version 1.0.0");
  delay(1000); // Display the message for 1 sec
  lcd.clear(); // clear the screen

}


void loop() {
  // format is col,row
  lcd.setCursor(0,0);
  lcd.print("Arduino LCD");
  lcd.setCursor(0,1);
  lcd.print("rocks!!");
}

Explanation of the template codes :-


It starts with including the standard Liquid Crystal library from Arduino. The second line tells the Arduino software how the pins are connected to match the hardware hookups.

Most of the LCD library commands are located at : Arduino LCD Reference. The common ones are :-

Print
lcd.print(data, BASE);

The BASE is optional, tell the Arduino what format to display on the LCD Module.
BIN - binary or base 2
DEC - decimal or base 10
OCT - octal or base 8
HEX - hexadecimal or base 16

Note - If you do a search and replace Serial.print() to lcd.print(), there are no lcd.println() as you would need to use the setCursor() to move to the next line.

Examples of lcd.print()  :-
lcd.print("Hello World");

lcd.print(variable);

lcd.print(variable,DEC);

*** Make sure you do not exceed the number of columns of the LCD module.

Set Cursor
lcd.setCursor();

This command set the location of the cursor, the format is column, row.
The column and row starts with 0, so a 16x2 LCD, valid columns would be 0 to 15, valid rows would be from 0 to 1.

Examples of setCursor() :-
lcd.setCursor(0,0);   // Jump to the top left corner
lcd.setCursor(9,1);  // Jump to column 10, second row

Clear Screen
lcd.clear();

This command will clear the screen as the LCD LiquidCrystal library would just overwrite from a previous display.


To make the Arduino more interactive, I usually output useful information ( status, temperature, date or time ) to the LCD module instead of sending the data over to the serial monitor. The advantage of this is that you do not need to have Arduino connected to the PC as most of the Arduino project, it should be as stand alone as possible disconnected from the PC USB port.

What else can you do with a 16x2 LCD Module ? Someone wrote a cool Truck Lane game with custom charactor. For the steering wheel, you can use a potentiometer (POT) to steer the truck up and down the screen. Just hook the POT to Analog Pin 1 to the voltage divider ( middle connector ) of the POT. Give it a try.


3 comments:

  1. Very detailed and useful. Thank you!

    ReplyDelete
  2. Sir, I'm using Sensor PWM , LCD, Arduino Uno. I'm only familiar with lcd.print("Hello World") but how i can display the input of my sensor to the screen, any new code??

    ReplyDelete
  3. A very good explanation is available @ http://www.youtube.com/watch?v=oIiDseJO4dM

    http://www.techiesinfo.com

    ReplyDelete