Arduino and LCD Shield

 

LCD (Liquid Crystal Displays) are very useful in electronics projects as they are good for displaying information from the program/project.

The LCD Pinout

Following figure dhows Pin Out of LCD having 16 pins. The first and second pins are for Ground and 5 Volt power supply connection respectively. Next one is the Ve pin on which we can attach a potentiometer for controlling the contrast of the display.

The RS pin or Register Select pin is used for selecting whether commands or data is sent to the LCD.

If the RS pin is set on Low state, then commands are sent.  Commands such as, set the cursor to a specific location, clear the display, turn off the display and so on.

And if the RS pin is set on High, then Data is sent. For example, Data or characters.

LCD pin out

 

The R / W pin which selects the mode whether MCU read or write to the LCD. In write mode, MCU is writing or sending commands and data to the LCD. The read mode is used by the LCD itself when executing the program.

.E pin which enables the writing to the registers, or the next 8 data pins from D0 to D7. So through this pins MCU sends the 8 bits data when we are writing to the registers or for example if we want to see the letter uppercase A on the display we will send 0100 0001 to the registers according to the ASCII table.

And the last two pins are A and K, or anode and cathode are for the LED back light.

The 16X2 LCD Keypad Shield build for Arduino UNO, MEGA, Leonardo and other microcontrollers. This shield is fully compatible with Arduino 4-bit “LiquidCrystal” library.

It includes 16X2 LCD display with 6 push Buttons, among which 5 are connected with A0 analog input through resistors to give different voltage for each buttons and saving on input/output pins. And Pins 4, 5, 6, 7, 8, 9 and 10 are used to interface with the LCD.

LCD_KEYPAD_MODULE

Specifications:

  • Operating Voltage: +5V
  • 5 Push buttons.
  • RST button to reset arduino program.
  • Integrate a potentiometer for adjusting the backlight.
  • Expanded available I/O pins.
  • Blue light with white characters.

How Buttons are used :

5 buttons Select, Up, Down, Left and Right are connected with Analog input A0 with a series of Resistance.

LCD_BUTTON_FUNCTIONALITY

If no button is pressed the voltage on A0 will be pulled up to 5V by the 2K resistor called R1. In that situation none of the other resistors have any effect at all, and the analog reading on A0 will be hard on the upper limit of 1023.

When the “UP” button will be pressed, the 2K resistor that is trying to pull it up to 5V, and the 330R that are trying to pull it down to 0V. The voltage presented to A0 in that case is about 0.71V, So if you read a value about 145 from A0 you know the “UP” button is being pressed.

When “Down” button is pressed, (Series Resistor 330R+620R) 950 are trying to pull it down to 0V. then A0 presents 1.61V, value about 329.

2. Required Hardware:

S.No. Item Quantity
1. Arduino  1
2. LCD Keypad Shield module  1
3. Male to Female Jumper Wires  12

3. Building Circuit

Lcd_shield_arduino

Do not changed digital pin 10. This pin is used to control the back light of the LCD.

4. Library File

Following library will be required to run this sketch. Download the zip file extract the same and copy this to your Arduino library folder.

This library file should be placed at the install folder of Arduino. I have a 64 bit Win7 OS and my arduino library folder address is located at

C:\Program Files (x86)\Arduino\libraries

You may download library file from here.

5. Programming:

Once the circuit part is done, Arduino is needed to be programmed. Here is the code to get display on LCD.

//Robo India's tutorial on LCD Keypad Shield
https://www.roboindia.com/tutorials/

#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd( 8,9,4,5,6,7 );  //interfacing pins

void setup()
{

   // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("LCD Key Shield");
  lcd.setCursor(0,1);
  lcd.print("Press Key:");
}

void loop()
{
  int x;
  x = analogRead (0);
  lcd.setCursor(10,1);
  if (x < 60) {
    lcd.print ("Right ");
  }
  else if (x < 200) {
    lcd.print ("Up    ");    //analog voltage 145
  }
  else if (x < 400){
    lcd.print ("Down  ");    //analog voltage 329
  }
  else if (x < 600){
    lcd.print ("Left  ");    //analog voltage 585
  }
  else if (x < 800){
    lcd.print ("Select");    //analog voltage 741
  }
}

6. Output

lcdKeypadShieldOutput

After Uploading the code, LCD will display the text as shown in above image. If you press Right Push button, Right text will display on LCD.

Source Codes

First thing we need to do is it insert the Liquid Crystal Library. We can do that like this: Sketch > Include Library > Liquid Crystal. Then we have to create an LC object. The parameters of this object should be the numbers of the Digital Input pins of the Arduino Board respectively to the LCD’s pins as follow: (RS, Enable, D4, D5, D6, D7). In the setup we have to initialize the interface to the LCD and specify the dimensions of the display using the begin() function.In the loop we write our main program. Using the print() function we print on the LCD. The setCursor() function is used for setting the location at which subsequent text written to the LCD will be displayed. The blink() function is used for displaying a blinking cursor and the noBlink() function for turning off. The cursor() function is used for displaying underscore cursor and the noCursor() function for turning off. Using the clear() function we can clear the LCD screen.

LCD Shield Test Code:

/*************************************************************************************

Author : Ankit Modi
 Date : 28-09-2017

This program will test the LCD panel and the buttons.When you push the button on the shield,
 the screen will show the corresponding one.

 Connection: Plug the LCD Keypad to the UNO(or other controllers)

**************************************************************************************/

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel

// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;

#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

int read_LCD_buttons(){ // read the buttons
 adc_key_in = analogRead(0); // read the value from the sensor

// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 // We make this the 1st option for speed reasons since it will be the most likely result

if (adc_key_in > 1000) return btnNONE; 
/*
 // For V1.1 us this threshold
 if (adc_key_in < 50) return btnRIGHT; 
 if (adc_key_in < 250) return btnUP; 
 if (adc_key_in < 450) return btnDOWN; 
 if (adc_key_in < 650) return btnLEFT; 
 if (adc_key_in < 850) return btnSELECT; 
*/
 // For V1.0 comment the other threshold and use the one below:

 if (adc_key_in < 50) return btnRIGHT; 
 if (adc_key_in < 195) return btnUP; 
 if (adc_key_in < 380) return btnDOWN; 
 if (adc_key_in < 555) return btnLEFT; 
 if (adc_key_in < 790) return btnSELECT; 


return btnNONE; // when all others fail, return this.
}

void setup(){
 lcd.begin(16, 2); // start the library
 lcd.setCursor(0,0); // set the LCD cursor position 
 lcd.print("Push the buttons"); // print a simple message on the LCD
}

void loop(){
 lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
 lcd.print(millis()/1000); // display seconds elapsed since power-up

lcd.setCursor(0,1); // move to the begining of the second line
 lcd_key = read_LCD_buttons(); // read the buttons

switch (lcd_key){ // depending on which button was pushed, we perform an action

case btnRIGHT:{ // push button "RIGHT" and show the word on the screen
 lcd.print("RIGHT ");
 break;
 }
 case btnLEFT:{
 lcd.print("LEFT "); // push button "LEFT" and show the word on the screen
 break;
 } 
 case btnUP:{
 lcd.print("UP "); // push button "UP" and show the word on the screen
 break;
 }
 case btnDOWN:{
 lcd.print("DOWN "); // push button "DOWN" and show the word on the screen
 break;
 }
 case btnSELECT:{
 lcd.print("SELECT"); // push button "SELECT" and show the word on the screen
 break;
 }
 case btnNONE:{
 lcd.print("NONE "); // No action will show "None" on the screen
 break;
 }
 }
}

Source Code 1:

/*
* Arduino LCD Tutorial
*
* Crated by Dejan Nedelkovski,
* www.HowToMechatronics.com
*
*/

#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

void setup() { 
 lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display } 
}

void loop() { 
 lcd.print("Arduino"); // Prints "Arduino" on the LCD 
 delay(3000); // 3 seconds delay 
 lcd.setCursor(2,1); // Sets the location at which subsequent text written to the LCD will be displayed 
 lcd.print("LCD Tutorial"); 
 delay(3000); 
 lcd.clear(); // Clears the display 
 lcd.blink(); //Displays the blinking LCD cursor 
 delay(4000); 
 lcd.setCursor(7,1); 
 delay(3000); 
 lcd.noBlink(); // Turns off the blinking LCD cursor 
 lcd.cursor(); // Displays an underscore (line) at the position to which the next character will be written 
 delay(4000); 
 lcd.noCursor(); // Hides the LCD cursor 
 lcd.clear(); // Clears the LCD screen 
}

 

It is also possible to write a custom characters to the LCD. It supports up to 8 characters of 5×8 pixels. We can specify the appearance of each character by an array of 8 bytes. In the source code below we can notice how we can specify the appearance of the character by changing the 0 into 1 which represents the 5×8 pixels. In the setup we have to create the custom character using the createChar() function. The first parameter in this function is a number between 0 and 7, or we have to reserve one of the 8 supported custom characters. The second parameter is the name of the array of bytes. We write the custom character to the display using the write() function and as a parameter we use the number of the character.

Source Code 2:

#include <LiquidCrystal.h>

byte slash[8]= { // Array of bytes
 B00001, // B stands for binary formatter and the 5 numbers are the pixels
 B00010,
 B00100,
 B01000,
 B10000,
 B00000,
 B00000,
 B00000,
};
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

void setup() { 
 lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
 lcd.createChar(7, slash); // Create a custom character for use on the LCD. Up to eight characters of 5x8 pixels are supported
}

void loop() { 
 for(int i=0;i<=15;i++) {
 lcd.setCursor(i,0); // Sets the location at which subsequent text written to the LCD will be displayed
 lcd.write(7); // Writes a character to the LCD
 delay(1000); // 1 second delay
 lcd.clear(); // Write a character to the LCD
 }
}

 

Advertisements