Monday, 22 July 2013

LCD Interfacing - Working with the Simple Labs' LCD Shield

The Simple Labs' LCD Shield

The LCD Shield eases the process of interfacing a LCD display with the Induino R3 / Arduino. The shield fits on top of any Arduino Board  and takes power from the Arduino Board.


The LCD shield is designed to work with 16 pin LCDs with the following pin configuration.
















LCD Pin Mappings for the Shield
The LCD shields provides a 4-bit mode interface to the Arduino. The Pins are connected as follows. 
RS => 8
E => 9
D4 =>10
D5 =>11
D6 =>12
D7 =>13


How to place the Shield on top of an Arduino?
The shield has one of set 8 pins and 1 set of 6 Pins. The trimpot on the shield is on the top left side. Now place the shield onto your arduino such that the eight pins are placed on top of pins 8,9,10,11,12,13,gnd & aref and the six pins are placed on top of pins Vin, gnd, gnd, +5V, +3.3V & reset


Working of an LCD
Here is a neat write up on this, We suggest you read it completely before proceeding and Don't worry about the programming part given in the write up, we have a library that will take care of most of it for us ;) http://joshuagalloway.com/lcd.html

Programming the LCD Shield

Arduino IDE has useful LCD library - LiquidCrystal - This library helps us interface character lcds with ease.  The library supports both 8-bit and 4-bit mode of working with LCDs. If you had read the above write up,  its our good bet that all those programming how to got to your head. Well, the LiquidCrystal library takes it all away for us.

  • Include the LiquidCrystal.h Library
  • Create a Object of Class LiquidCrystal (can have any name like a variable) / Syntax - LiquidCrystal lcd(pin numbers). pin numbers are to be in the following order 
    • 8-bit mode with R/W not being grounded => RS, R/W, E, D0, D1, D2, D3, D4, D5, D6, D7
    • 8-bit mode with R/W directly grounded => RS, E, D0, D1, D2, D3, D4, D5, D6, D7
    • 4-bit mode with R/W not being grounded => RS, R/W, E, D4, D5, D6, D7
    • 4-bit mode with R/W directly grounded (for our LCD Shield) => RS, E, D4, D5, D6, D7
  • So our initialization will look like -> LiquidCrystal lcd(8,9,10,11,12,13);
  • Initialize communication with the LCD using the lcd.begin(Characters per line,lines) function. The lcd.begin()function takes 2 parameters - the number of Characters per line and number of lines. in our case it would be 16 characters per line and 2 lines. so => lcd.begin(16,2);
  • dsiplay data on the lcd using the lcd.print(data) function. The lcd.print() function takes the data to be displayed as parameter and prints it.


Here's a program to display the LDR value onto the LCD

/*  Induino R3 User Guide - Program 10.0 - Interfacing a Character LCD using the LCD Shield to display LDR value on the LCD */
/* Pin Mappings as per the Simple Labs' LCD shield
 * LCD RS pin to digital pin 8
 * LCD Enable pin to digital pin 9
 * LCD D4 pin to digital pin 10
 * LCD D5 pin to digital pin 11
 * LCD D6 pin to digital pin 12
 * LCD D7 pin to digital pin 13
 * LCD R/W pin to ground
*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins from the above pin mappings
LiquidCrystal lcd(8,9,10,11,12,13); // 

void setup() {
  // set up the LCD's number of characters per line and lines: 
  lcd.begin(16, 2);

}

void loop() {
  
  lcd.clear();//Clears the Display - If you don't clear, previous data will remain as it is
  int ldr_val = analogRead(3); // read the LDR value
  lcd.print("LDR Value : "); // print the description text
  lcd.print(ldr_value); // print the actual value
  delay(2000); // wait before refreshing the value
  
}


There are a number of other functions available to work with the LCD. These are documented here on the official Arudino Refernce Page -> http://arduino.cc/en/Reference/LiquidCrystal

Feel free to try out all the examples in the LiquidCrystal library. Just remember to change the pin numbers during the object creation statement -> LiquidCrystal lcd(8,9,10,11,12,13);

Here's another program to display the Distance Value based on the HCSR 04 onto the LCD

/*  Induino R3 User Guide - Program 10.1 - Interfacing a Character LCD using the LCD Shield to display Distance value on the LCD */
/* Pin Mappings as per the Simple Labs' LCD shield
 * LCD RS pin to digital pin 8
 * LCD Enable pin to digital pin 9
 * LCD D4 pin to digital pin 10
 * LCD D5 pin to digital pin 11
 * LCD D6 pin to digital pin 12
 * LCD D7 pin to digital pin 13
 * LCD R/W pin to ground
*/
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,10,11,12,13);

const int pingPin = 16; // HCSR-04 is connected to the sensor Interface A2 aka 16

long ol_inches=0, ol_cm=0;

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);

}

void loop() {
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The HCSR 04 is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the HCSR 04: a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  //if(cm!=ol_cm || inches!=ol_inches)  // Check if the new dsitance values are not the same as the old values
  {
  lcd.clear(); // Clear the Dispaly
  lcd.print(inches); // Print the Distance in Inches
  lcd.setCursor(4, 0); // Position the cursor of the LCD display to print from the 5th Character on the First Line
  lcd.print("INCHES");// Print the descriptive text
  lcd.setCursor(0, 1); // Position the cursor of the LCD display to print from the 1st Character on the Second Line
  lcd.print(cm); // Print the Distance in Centimeters
  lcd.setCursor(4, 1);// Position the cursor of the LCD display to print from the 5th Character on the Second Line
  lcd.print("CMS"); // Print the descriptive text
  ol_inches = inches; // store the new inches value into a variable
  ol_cm = cm; // store the new centimeters value into a variable
  delay(200);
  
}
  
  // print the number of seconds since reset:
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

That's It For This Part! Enjoy... and feel free to drop us an email with questions you might have -> info@simplelabs.co.in

Visit www.simplelabs.co.in for more interesting products

Back to List of Contents

10 comments:

  1. Sir,
    I have been using the induino for a long time with no problems . but suddenly one day i get this error and my lcd doesnt work anymore ,,

    avrdude: verification error, first mismatch at byte 0x0000
    0x0c != 0x62
    avrdude: verification error; content mismatch

    it doesnt work on the simple hello world program also . help .

    ReplyDelete
    Replies
    1. Try Reloading Bootloader, Your Problem will get Fixed....

      Delete
  2. Prakash, whenever I add the LCD shield to my R3, I start getting sync error when I try to upload the sketch. Any ideas?

    ReplyDelete
    Replies
    1. probably the shield is not plugged in properly, remove the lcd and just plugin the shield and see what happens, then plug in the lcd and see what happens, send us an email and we can have this sorted

      Delete
  3. Thank you for the tutorials. Restarting the learning again. Noticed one minor mistake in the line - lcd.print(ldr_value); // print the actual value. The declared variable is ldr_val.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Sir,
    Whenever I connect my LCD to induino, with proper connections, the RGB LED starts to glow BLUE and sometimes red along with the the other three onboard LEDs. My LCD display is 'JHD162A'.

    I wanted to know if this is a usual thing or a problem with my wiring or board. (Wiring seems unlikely as I have checked many times).

    Thank You.

    ReplyDelete
  6. Hi, is it possible to use LCD shield with Ethernet shield?

    ReplyDelete