LCD Interfacing with Arduino 

What is an LCD?

LCD stands for Liquid Crystal Display. It is used as an output device. It is used to display messages. The LCD uses a Back-light through which information or messages passed are seen. In Liquid Crystal Display the screen size is 16*2 which means 16 columns and 2 rows.

How to program an LCD?

1. Include the header file 

 eg. #include<LiquidCrystal.h>

2. Declare the pins of the LCD using the LiquidCrystal function. 

eg. LiquidCrystral lcd( RS,EN.,RW,D1,D2,D3,D4);

3. Initialize the LCD using the begin function.

eg. lcd.begin();

4. Print the message on lcd.

eg. lcd.print("HELLO WORLD!");

Function of LCD Interfacing

1. setCursor( ):- It is used to take control of a particular place on the LCD

Syntax:- lcd.stetCursor (c,r);

Parameters:-

C:- No. of the column.

R:- No. of row.

2. blink( ) and noBlink ( ):- It is used to turn on and off blinking of a matrix in LCD.

Syntax:- lcd.blink ( ); and lcd.noBlink( );

Parameters:-

 No Parameters Used.

3. noCursor( ) and cursor( ):- It is used to turn on and off the blinking of a cursor.

Syntax:- lcd.noCursor ( ); and lcd.cursor( );

Parameters:-

 No Parameters Used.

4. noDisplay( ) and display( ):- It is used to turn on and off the Showing of a text.

Syntax:- lcd.noDisplay ( ); and lcd.display( );

Parameters:-

 No Parameters Used.

5. leftToRight( ) and rightToLeft( ):- It is used to print text from left to right and right to left.

Syntax:- lcd.leftToRight ( ); and lcd.rightToLeft( );

Parameters:-

 No Parameters Used.

6. scrollDisplayLeft( ) :- It is used to move the text on display to the left side.

Syntax:- lcd.scrollDisplayLeft( );

Parameters:-

 No Parameters Used.

7. scrollDisplayRight( ) :- It is used to move the text on display to the Right side but it performs the action only one time.

Syntax:- lcd.scrollDisplayRight( );

Parameters:-

 No Parameters Used.

8. autoscroll( ):- It is used to move the text only in one direction that is from right to left.

Syntax:- lcd.autoscroll( );

Parameters:-

 No Parameters Used.

9. clear( ):- It is used to clear the LCD.

Syntax:- lcd.clear( );

Parameters:-

 No Parameters Used.

10. write( ):- It is used to write a character on display.

Syntax:- lcd.write(data);

Parameters:-

data:- The character to write on the display.

11. print( ):- It is used to Print (char, int, or string) on display.

Syntax:- lcd.print(data);

Parameters:-

Data:- The character, integer, or string to print on the display.

Connection of Liquid Crystal Display With Arduino.



Pin 1.  Vcc:- It is connected to the 5v pin on the Arduino board.

Pin 2. GND:- It is connected to the ground pin on the Arduino board. 

Pin 3. Contrast:- It controls the contrast of LCD.

Pin 4. Register select (RS):- It is used as a control signal to tell the LCD what kind of signal we are sending command or display.

Pin 5. Read and Write (R/W):- It is used to select the operation we want to perform usually it is used for writing. 

Pin 6. Enable:- The EN (Enable) pin on lcd is a control signal used to initiate the data transfer. When the EN pin Receives a high to-low pulse, the lcd process the data or command.

Pin 7. to Pin 10 Data pins( used in 8-bit only):- The data pins are used to transfer the data from the microcontroller to the LCD.

Pin 11. to Pin 14 Data pins:- The data pins are used to transfer the data from the microcontroller to the LCD.

Pin 15 Anode:- It is a positive terminal LED and blacklight.

Pin 16 Cathode:- It is a negative terminal to LED and blacklight.

Task 1: To display a message on LCD.

 Code:-

#include<LiquidCrystal.h>

LiquidCrystal lcd(13,12,5,4,3,2);

void setup ()

{

  lcd.begin(16,2);


}

void loop ()

{

 lcd.setCursor(0,0);

  lcd.print("HELLO WORLD!");

  delay(5000);

  lcd.clear();

}.

 Output :-


Task 2: To display three different messages. When switch 1 is pressed 1st message should be displayed, switch 2 2nd message, and switch 3 3rd message.

 Code:-

#include<LiquidCrystal.h>

LiquidCrystal lcd(13,12,5,4,3,2);

int sw1=11,sw2=10,sw3=9;

void setup ()

{

  lcd.begin(16,2);

  pinMode(sw1,INPUT);

  pinMode(sw2,INPUT);

  pinMode(sw3,INPUT);

}

void loop ()

{

  if (digitalRead(sw1)==LOW)

  {

    lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("SWITCH 1 PRESSED!");

  }

   else if (digitalRead(sw2)==LOW)

  {

    lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("SWITCH 2 PRESSED!");

  }

   else if (digitalRead(sw3)==LOW)

  {

    lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("SWITCH 3 PRESSED!");

  }

}

 Output:-



Task 3: Make a stopwatch. It should start when Switch 1 is pressed and stop when Switch 2 is pressed and Switch 3 for reset.

 Code:-

  #include<LiquidCrystal.h>

LiquidCrystal lcd(13,12,5,4,3,2);

int sw1=11,sw2=10,sw3=9;

int second=0,minute=0,hour=0;

void reset()

{

  lcd.setCursor(0,1);

  lcd.print("0");

  hour=0;

  lcd.print(hour);

   lcd.print(":");

  lcd.print("0");

  minute=0;

  lcd.print(minute);

   lcd.print(":");

  lcd.print("0");

  second=0; 

  lcd.print(second);

  

}

void setclock ()

{

  delay(1000);

  second++;

  if (second>60)

  {

    second=0;

    minute++;

  }

 if (minute>60)

  {

    minute=0;

    hour++;

  }

 if (hour >12)

  {

    hour=0;

  }

    

}

void time()

{

  lcd.setCursor(0,1);

  if (hour<10)

  {

    lcd.print("0");

  }

  lcd.print(hour);

  lcd.print(":");

  if (minute<10)

  {

    lcd.print("0");

  }

    lcd.print(minute);

    lcd.print(":");

    if (second<10)

  {

    lcd.print("0");

  }

    lcd.print(second);

}

void setup ()

{

  lcd.begin(16,2);

  pinMode(sw1,INPUT);

  pinMode(sw2,INPUT);

  pinMode(sw3,INPUT);

  lcd.setCursor(0,0);

  lcd.print("Stopwatch");

}

void loop ()

{

  if (digitalRead(sw1)==LOW)

  {

    while (digitalRead(sw2)==HIGH)

    {

      setclock();

      time();

    }

  }

  if (digitalRead(sw3)==LOW)

  {

    reset();

  }

 }

 Output:-