아두 이노 lcd - adu ino lcd

Find out how to wire an LCD to an Arduino, and how to use the LiquidCrystal library through a set of useful examples.

This article was revised on 2021/11/18 by Karl Söderby.

The LiquidCrystal library allows you to control LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and you can usually tell them by the 16-pin interface.

아두 이노 lcd - adu ino lcd
Output of the sketch on a 16x2 LCD

The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins:

  • A register select (RS) pin that controls where in the LCD's memory you're writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, which is where the LCD's controller looks for instructions on what to do next.
  • A Read/Write (R/W) pin that selects reading mode or writing mode
  • An Enable pin that enables writing to the registers
  • 8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you're writing to a register when you write, or the values you're reading when you read.

There's also a display contrast pin (Vo), power supply pins (+5V and GND) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the LCD, control the display contrast, and turn on and off the LED backlight, respectively.

The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register. The LiquidCrystal Library simplifies this for you so you don't need to know the low-level instructions.

The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins. For displaying text on the screen, you can do most everything in 4-bit mode, so example shows how to control a 16x2 LCD in 4-bit mode.

Hardware Required

  • Arduino Board
  • LCD Screen (compatible with Hitachi HD44780 driver)
  • pin headers to solder to the LCD display pins
  • 10k ohm potentiometer
  • 220 ohm resistor
  • hook-up wires
  • breadboard

Circuit

Note that this circuit was originally designed for the Arduino UNO. As the Arduino is communicating with the display using SPI, pin 11 & 12 will change depending on what board you are using. For example, on a MKR WiFi 1010, the SPI bus is attached to pin 8 & 11.

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14 (or 16) pin count connector of the LCD screen, as you can see in the image further up.

To wire your LCD screen to your board, connect the following pins:

  • LCD RS pin to digital pin 12
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2
  • LCD R/W pin to GND
  • LCD VSS pin to GND
  • LCD VCC pin to 5V
  • LCD LED+ to 5V through a 220 ohm resistor
  • LCD LED- to GND

Additionally, wire a 10k potentiometer to +5V and GND, with it's wiper (output) to LCD screens VO pin (pin3).

아두 이노 lcd - adu ino lcd
The circuit (made using Fritzing).

Schematic

아두 이노 lcd - adu ino lcd
The schematic (made using Fritzing).

Hello World Example

This example sketch prints

Hello World!

to the LCD and shows the time in seconds since the Arduino was reset.

1/*

2 LiquidCrystal Library - Hello World

3

4 Demonstrates the use a 16x2 LCD display. The LiquidCrystal

5 library works with all LCD displays that are compatible with the

6 Hitachi HD44780 driver. There are many of them out there, and you

7 can usually tell them by the 16-pin interface.

8

9 This sketch prints "Hello World!" to the LCD

10 and shows the time.

11

12 The circuit:

13 * LCD RS pin to digital pin 12

14 * LCD Enable pin to digital pin 11

15 * LCD D4 pin to digital pin 5

16 * LCD D5 pin to digital pin 4

17 * LCD D6 pin to digital pin 3

18 * LCD D7 pin to digital pin 2

19 * LCD R/W pin to ground

20 * LCD VSS pin to ground

21 * LCD VCC pin to 5V

22 * 10K resistor:

23 * ends to +5V and ground

24 * wiper to LCD VO pin (pin 3)

25

26 Library originally added 18 Apr 2008

27 by David A. Mellis

28 library modified 5 Jul 2009

29 by Limor Fried (http://www.ladyada.net)

30 example added 9 Jul 2009

31 by Tom Igoe

32 modified 22 Nov 2010

33 by Tom Igoe

34 modified 7 Nov 2016

35 by Arturo Guadalupi

36

37 This example code is in the public domain.

38

39 http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld

40

41*/

42

43// include the library code:

44#include <LiquidCrystal.h>

45

46// initialize the library by associating any needed LCD interface pin

47// with the arduino pin number it is connected to

48const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

49LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

50

51void setup() {

52 // set up the LCD's number of columns and rows:

53 lcd.begin(16, 2);

54 // Print a message to the LCD.

55 lcd.print("hello, world!");

56}

57

58void loop() {

59 // set the cursor to column 0, line 1

60 // (note: line 1 is the second row, since counting begins with 0):

61 lcd.setCursor(0, 1);

62 // print the number of seconds since reset:

63 lcd.print(millis() / 1000);

64}

Autoscroll Example

This example sketch shows how to use the

autoscroll()

and

noAutoscroll()

methods to move all the text on the display left or right.

  • autoscroll()
    moves all the text one space to the left each time a letter is added
  • noAutoscroll()
    turns scrolling off

This sketch prints the characters

0

to

9

with autoscroll off, then moves the cursor to the bottom right, turns autoscroll on, and prints them again.

1/*

2

3 LiquidCrystal Library - Autoscroll

4

5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal

6

7 library works with all LCD displays that are compatible with the

8

9 Hitachi HD44780 driver. There are many of them out there, and you

10

11 can usually tell them by the 16-pin interface.

12

13 This sketch demonstrates the use of the autoscroll()

14

15 and noAutoscroll() functions to make new text scroll or not.

16

17 The circuit:

18

19 * LCD RS pin to digital pin 12

20

21 * LCD Enable pin to digital pin 11

22

23 * LCD D4 pin to digital pin 5

24

25 * LCD D5 pin to digital pin 4

26

27 * LCD D6 pin to digital pin 3

28

29 * LCD D7 pin to digital pin 2

30

31 * LCD R/W pin to ground

32

33 * 10K resistor:

34

35 * ends to +5V and ground

36

37 * wiper to LCD VO pin (pin 3)

38

39 Library originally added 18 Apr 2008

40

41 by David A. Mellis

42

43 library modified 5 Jul 2009

44

45 by Limor Fried (http://www.ladyada.net)

46

47 example added 9 Jul 2009

48

49 by Tom Igoe

50

51 modified 22 Nov 2010

52

53 by Tom Igoe

54

55 modified 7 Nov 2016

56

57 by Arturo Guadalupi

58

59 This example code is in the public domain.

60

61 http://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll

62

63*/

64

65// include the library code:

66#include <LiquidCrystal.h>

67

68// initialize the library by associating any needed LCD interface pin

69// with the arduino pin number it is connected to

70

71const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

72

73LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

74

75void setup() {

76

77 // set up the LCD's number of columns and rows:

78

79 lcd.begin(16, 2);

80}

81

82void loop() {

83

84 // set the cursor to (0,0):

85

86 lcd.setCursor(0, 0);

87

88 // print from 0 to 9:

89

90 for (int thisChar = 0; thisChar < 10; thisChar++) {

91

92 lcd.print(thisChar);

93

94 delay(500);

95

96 }

97

98 // set the cursor to (16,1):

99

100 lcd.setCursor(16, 1);

101

102 // set the display to automatically scroll:

103

104 lcd.autoscroll();

105

106 // print from 0 to 9:

107

108 for (int thisChar = 0; thisChar < 10; thisChar++) {

109

110 lcd.print(thisChar);

111

112 delay(500);

113

114 }

115

116 // turn off automatic scrolling

117

118 lcd.noAutoscroll();

119

120 // clear screen for the next loop:

121

122 lcd.clear();

123}

This example sketch shows how to use the

blink()

and

noBlink()

methods to blink a block-style cursor.

1/*

2

3 LiquidCrystal Library - Blink

4

5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal

6

7 library works with all LCD displays that are compatible with the

8

9 Hitachi HD44780 driver. There are many of them out there, and you

10

11 can usually tell them by the 16-pin interface.

12

13 This sketch prints "Hello World!" to the LCD and makes the

14

15 cursor block blink.

16

17 The circuit:

18

19 * LCD RS pin to digital pin 12

20

21 * LCD Enable pin to digital pin 11

22

23 * LCD D4 pin to digital pin 5

24

25 * LCD D5 pin to digital pin 4

26

27 * LCD D6 pin to digital pin 3

28

29 * LCD D7 pin to digital pin 2

30

31 * LCD R/W pin to ground

32

33 * 10K resistor:

34

35 * ends to +5V and ground

36

37 * wiper to LCD VO pin (pin 3)

38

39 Library originally added 18 Apr 2008

40

41 by David A. Mellis

42

43 library modified 5 Jul 2009

44

45 by Limor Fried (http://www.ladyada.net)

46

47 example added 9 Jul 2009

48

49 by Tom Igoe

50

51 modified 22 Nov 2010

52

53 by Tom Igoe

54

55 modified 7 Nov 2016

56

57 by Arturo Guadalupi

58

59 This example code is in the public domain.

60

61 http://www.arduino.cc/en/Tutorial/LiquidCrystalBlink

62

63*/

64

65// include the library code:

66#include <LiquidCrystal.h>

67

68// initialize the library by associating any needed LCD interface pin

69// with the arduino pin number it is connected to

70

71const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

72

73LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

74

75void setup() {

76

77 // set up the LCD's number of columns and rows:

78

79 lcd.begin(16, 2);

80

81 // Print a message to the LCD.

82

83 lcd.print("hello, world!");

84}

85

86void loop() {

87

88 // Turn off the blinking cursor:

89

90 lcd.noBlink();

91

92 delay(3000);

93

94 // Turn on the blinking cursor:

95

96 lcd.blink();

97

98 delay(3000);

99}

Cursor

This example sketch shows how to use the

cursor()

and

noCursor()

methods to control an underscore-style cursor.

1/*

2

3 LiquidCrystal Library - Cursor

4

5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal

6

7 library works with all LCD displays that are compatible with the

8

9 Hitachi HD44780 driver. There are many of them out there, and you

10

11 can usually tell them by the 16-pin interface.

12

13 This sketch prints "Hello World!" to the LCD and

14

15 uses the cursor() and noCursor() methods to turn

16

17 on and off the cursor.

18

19 The circuit:

20

21 * LCD RS pin to digital pin 12

22

23 * LCD Enable pin to digital pin 11

24

25 * LCD D4 pin to digital pin 5

26

27 * LCD D5 pin to digital pin 4

28

29 * LCD D6 pin to digital pin 3

30

31 * LCD D7 pin to digital pin 2

32

33 * LCD R/W pin to ground

34

35 * 10K resistor:

36

37 * ends to +5V and ground

38

39 * wiper to LCD VO pin (pin 3)

40

41 Library originally added 18 Apr 2008

42

43 by David A. Mellis

44

45 library modified 5 Jul 2009

46

47 by Limor Fried (http://www.ladyada.net)

48

49 example added 9 Jul 2009

50

51 by Tom Igoe

52

53 modified 22 Nov 2010

54

55 by Tom Igoe

56

57 modified 7 Nov 2016

58

59 by Arturo Guadalupi

60

61 This example code is in the public domain.

62

63 http://www.arduino.cc/en/Tutorial/LiquidCrystalCursor

64

65*/

66

67// include the library code:

68#include <LiquidCrystal.h>

69

70// initialize the library by associating any needed LCD interface pin

71// with the arduino pin number it is connected to

72

73const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

74

75LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

76

77void setup() {

78

79 // set up the LCD's number of columns and rows:

80

81 lcd.begin(16, 2);

82

83 // Print a message to the LCD.

84

85 lcd.print("hello, world!");

86}

87

88void loop() {

89

90 // Turn off the cursor:

91

92 lcd.noCursor();

93

94 delay(500);

95

96 // Turn on the cursor:

97

98 lcd.cursor();

99

100 delay(500);

101}

Display Example

This example sketch shows how to use the

display()

and

noDisplay()

methods to turn on and off the display. The text to be displayed will still be preserved when you use noDisplay() so it's a quick way to blank the display without losing everything on it.

1/*

2 LiquidCrystal Library - display() and noDisplay()

3

4 Demonstrates the use a 16x2 LCD display. The LiquidCrystal

5 library works with all LCD displays that are compatible with the

6 Hitachi HD44780 driver. There are many of them out there, and you

7 can usually tell them by the 16-pin interface.

8

9 This sketch prints "Hello World!" to the LCD and uses the

10 display() and noDisplay() functions to turn on and off

11 the display.

12

13 The circuit:

14 * LCD RS pin to digital pin 12

15 * LCD Enable pin to digital pin 11

16 * LCD D4 pin to digital pin 5

17 * LCD D5 pin to digital pin 4

18 * LCD D6 pin to digital pin 3

19 * LCD D7 pin to digital pin 2

20 * LCD R/W pin to ground

21 * 10K resistor:

22 * ends to +5V and ground

23 * wiper to LCD VO pin (pin 3)

24

25 Library originally added 18 Apr 2008

26 by David A. Mellis

27 library modified 5 Jul 2009

28 by Limor Fried (http://www.ladyada.net)

29 example added 9 Jul 2009

30 by Tom Igoe

31 modified 22 Nov 2010

32 by Tom Igoe

33 modified 7 Nov 2016

34 by Arturo Guadalupi

35

36 This example code is in the public domain.

37

38 http://www.arduino.cc/en/Tutorial/LiquidCrystalDisplay

39

40*/

41

42// include the library code:

43#include <LiquidCrystal.h>

44

45// initialize the library by associating any needed LCD interface pin

46// with the arduino pin number it is connected to

47const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

48LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

49

50void setup() {

51 // set up the LCD's number of columns and rows:

52 lcd.begin(16, 2);

53 // Print a message to the LCD.

54 lcd.print("hello, world!");

55}

56

57void loop() {

58 // Turn off the display:

59 lcd.noDisplay();

60 delay(500);

61 // Turn on the display:

62 lcd.display();

63 delay(500);

64}

Scroll Example

This example sketch shows how to use the

scrollDisplayLeft()

and

scrollDisplayRight()

methods to reverse the direction the text is flowing. It prints "Hello World!", scrolls it offscreen to the left, then offscreen to the right, then back to home.

1/*

2 LiquidCrystal Library - scrollDisplayLeft() and scrollDisplayRight()

3

4 Demonstrates the use a 16x2 LCD display. The LiquidCrystal

5 library works with all LCD displays that are compatible with the

6 Hitachi HD44780 driver. There are many of them out there, and you

7 can usually tell them by the 16-pin interface.

8

9 This sketch prints "Hello World!" to the LCD and uses the

10 scrollDisplayLeft() and scrollDisplayRight() methods to scroll

11 the text.

12

13 The circuit:

14 * LCD RS pin to digital pin 12

15 * LCD Enable pin to digital pin 11

16 * LCD D4 pin to digital pin 5

17 * LCD D5 pin to digital pin 4

18 * LCD D6 pin to digital pin 3

19 * LCD D7 pin to digital pin 2

20 * LCD R/W pin to ground

21 * 10K resistor:

22 * ends to +5V and ground

23 * wiper to LCD VO pin (pin 3)

24

25 Library originally added 18 Apr 2008

26 by David A. Mellis

27 library modified 5 Jul 2009

28 by Limor Fried (http://www.ladyada.net)

29 example added 9 Jul 2009

30 by Tom Igoe

31 modified 22 Nov 2010

32 by Tom Igoe

33 modified 7 Nov 2016

34 by Arturo Guadalupi

35

36 This example code is in the public domain.

37

38 http://www.arduino.cc/en/Tutorial/LiquidCrystalScroll

39

40*/

41

42// include the library code:

43#include <LiquidCrystal.h>

44

45// initialize the library by associating any needed LCD interface pin

46// with the arduino pin number it is connected to

47const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

48LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

49

50void setup() {

51 // set up the LCD's number of columns and rows:

52 lcd.begin(16, 2);

53 // Print a message to the LCD.

54 lcd.print("hello, world!");

55 delay(1000);

56}

57

58void loop() {

59 // scroll 13 positions (string length) to the left

60 // to move it offscreen left:

61 for (int positionCounter = 0; positionCounter < 13; positionCounter++) {

62 // scroll one position left:

63 lcd.scrollDisplayLeft();

64 // wait a bit:

65 delay(150);

66 }

67

68 // scroll 29 positions (string length + display length) to the right

69 // to move it offscreen right:

70 for (int positionCounter = 0; positionCounter < 29; positionCounter++) {

71 // scroll one position right:

72 lcd.scrollDisplayRight();

73 // wait a bit:

74 delay(150);

75 }

76

77 // scroll 16 positions (display length + string length) to the left

78 // to move it back to center:

79 for (int positionCounter = 0; positionCounter < 16; positionCounter++) {

80 // scroll one position left:

81 lcd.scrollDisplayLeft();

82 // wait a bit:

83 delay(150);

84 }

85

86 // delay at the end of the full loop:

87 delay(1000);

88

89}

Serial to Display Example

This example sketch accepts serial input from a host computer and displays it on the LCD. To use it, upload the sketch, then open the Serial Monitor and type some characters and click Send. The text will appear on your LCD.

1/*

2 LiquidCrystal Library - Serial Input

3

4 Demonstrates the use a 16x2 LCD display. The LiquidCrystal

5 library works with all LCD displays that are compatible with the

6 Hitachi HD44780 driver. There are many of them out there, and you

7 can usually tell them by the 16-pin interface.

8

9 This sketch displays text sent over the serial port

10 (e.g. from the Serial Monitor) on an attached LCD.

11

12 The circuit:

13 * LCD RS pin to digital pin 12

14 * LCD Enable pin to digital pin 11

15 * LCD D4 pin to digital pin 5

16 * LCD D5 pin to digital pin 4

17 * LCD D6 pin to digital pin 3

18 * LCD D7 pin to digital pin 2

19 * LCD R/W pin to ground

20 * 10K resistor:

21 * ends to +5V and ground

22 * wiper to LCD VO pin (pin 3)

23

24 Library originally added 18 Apr 2008

25 by David A. Mellis

26 library modified 5 Jul 2009

27 by Limor Fried (http://www.ladyada.net)

28 example added 9 Jul 2009

29 by Tom Igoe

30 modified 22 Nov 2010

31 by Tom Igoe

32 modified 7 Nov 2016

33 by Arturo Guadalupi

34

35 This example code is in the public domain.

36

37 http://www.arduino.cc/en/Tutorial/LiquidCrystalSerialDisplay

38

39*/

40

41// include the library code:

42#include <LiquidCrystal.h>

43

44// initialize the library by associating any needed LCD interface pin

45// with the arduino pin number it is connected to

46const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

47LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

48

49void setup() {

50 // set up the LCD's number of columns and rows:

51 lcd.begin(16, 2);

52 // initialize the serial communications:

53 Serial.begin(9600);

54}

55

56void loop() {

57 // when characters arrive over the serial port...

58 if (Serial.available()) {

59 // wait a bit for the entire message to arrive

60 delay(100);

61 // clear the screen

62 lcd.clear();

63 // read all the available characters

64 while (Serial.available() > 0) {

65 // display each character to the LCD

66 lcd.write(Serial.read());

67 }

68 }

69}

Set Cursor Example

This example sketch shows how to use the

setCursor()

method to reposition the cursor. To move the cursor, just call

setCursor()

with a row and column position. For example, for a 2x16 display:

1lcd.setCursor(0, 0); // top left

2lcd.setCursor(15, 0); // top right

3lcd.setCursor(0, 1); // bottom left

4lcd.setCursor(15, 1); // bottom right

Here is the full example:

1/*

2

3 LiquidCrystal Library - setCursor

4

5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal

6

7 library works with all LCD displays that are compatible with the

8

9 Hitachi HD44780 driver. There are many of them out there, and you

10

11 can usually tell them by the 16-pin interface.

12

13 This sketch prints to all the positions of the LCD using the

14

15 setCursor() method:

16

17 The circuit:

18

19 * LCD RS pin to digital pin 12

20

21 * LCD Enable pin to digital pin 11

22

23 * LCD D4 pin to digital pin 5

24

25 * LCD D5 pin to digital pin 4

26

27 * LCD D6 pin to digital pin 3

28

29 * LCD D7 pin to digital pin 2

30

31 * LCD R/W pin to ground

32

33 * 10K resistor:

34

35 * ends to +5V and ground

36

37 * wiper to LCD VO pin (pin 3)

38

39 Library originally added 18 Apr 2008

40

41 by David A. Mellis

42

43 library modified 5 Jul 2009

44

45 by Limor Fried (http://www.ladyada.net)

46

47 example added 9 Jul 2009

48

49 by Tom Igoe

50

51 modified 22 Nov 2010

52

53 by Tom Igoe

54

55 modified 7 Nov 2016

56

57 by Arturo Guadalupi

58

59 This example code is in the public domain.

60

61 http://www.arduino.cc/en/Tutorial/LiquidCrystalSetCursor

62

63*/

64

65// include the library code:

66#include <LiquidCrystal.h>

67

68// initialize the library by associating any needed LCD interface pin

69// with the arduino pin number it is connected to

70

71const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

72

73LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

74

75// these constants won't change. But you can change the size of

76// your LCD using them:

77

78const int numRows = 2;

79

80const int numCols = 16;

81

82void setup() {

83

84 // set up the LCD's number of columns and rows:

85

86 lcd.begin(numCols, numRows);

87}

88

89void loop() {

90

91 // loop from ASCII 'a' to ASCII 'z':

92

93 for (int thisLetter = 'a'; thisLetter <= 'z'; thisLetter++) {

94

95 // loop over the columns:

96

97 for (int thisRow = 0; thisRow < numRows; thisRow++) {

98

99 // loop over the rows:

100

101 for (int thisCol = 0; thisCol < numCols; thisCol++) {

102

103 // set the cursor position:

104

105 lcd.setCursor(thisCol, thisRow);

106

107 // print the letter:

108

109 lcd.write(thisLetter);

110

111 delay(200);

112

113 }

114

115 }

116

117 }

118}

Text Direction Example

This example sketch shows how to use the

leftToRight()

and

rightToLeft()

methods. These methods control which way text flows from the cursor.

  • rightToLeft()
    causes text to flow to the left from the cursor, as if the display is right-justified.
  • leftToRight()
    causes text to flow to the right from the cursor, as if the display is left-justified.

This sketch prints

a

through

l

right to left, then

m

through

r

left to right, then

s

through

z

right to left again.

1/*

2

3 LiquidCrystal Library - TextDirection

4

5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal

6

7 library works with all LCD displays that are compatible with the

8

9 Hitachi HD44780 driver. There are many of them out there, and you

10

11 can usually tell them by the 16-pin interface.

12

13 This sketch demonstrates how to use leftToRight() and rightToLeft()

14

15 to move the cursor.

16

17 The circuit:

18

19 * LCD RS pin to digital pin 12

20

21 * LCD Enable pin to digital pin 11

22

23 * LCD D4 pin to digital pin 5

24

25 * LCD D5 pin to digital pin 4

26

27 * LCD D6 pin to digital pin 3

28

29 * LCD D7 pin to digital pin 2

30

31 * LCD R/W pin to ground

32

33 * 10K resistor:

34

35 * ends to +5V and ground

36

37 * wiper to LCD VO pin (pin 3)

38

39 Library originally added 18 Apr 2008

40

41 by David A. Mellis

42

43 library modified 5 Jul 2009

44

45 by Limor Fried (http://www.ladyada.net)

46

47 example added 9 Jul 2009

48

49 by Tom Igoe

50

51 modified 22 Nov 2010

52

53 by Tom Igoe

54

55 modified 7 Nov 2016

56

57 by Arturo Guadalupi

58

59 This example code is in the public domain.

60

61 http://www.arduino.cc/en/Tutorial/LiquidCrystalTextDirection

62

63*/

64

65// include the library code:

66#include <LiquidCrystal.h>

67

68// initialize the library by associating any needed LCD interface pin

69// with the arduino pin number it is connected to

70

71const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

72

73LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

74

75int thisChar = 'a';

76

77void setup() {

78

79 // set up the LCD's number of columns and rows:

80

81 lcd.begin(16, 2);

82

83 // turn on the cursor:

84

85 lcd.cursor();

86}

87

88void loop() {

89

90 // reverse directions at 'm':

91

92 if (thisChar == 'm') {

93

94 // go right for the next letter

95

96 lcd.rightToLeft();

97

98 }

99

100 // reverse again at 's':

101

102 if (thisChar == 's') {

103

104 // go left for the next letter

105

106 lcd.leftToRight();

107

108 }

109

110 // reset at 'z':

111

112 if (thisChar > 'z') {

113

114 // go to (0,0):

115

116 lcd.home();

117

118 // start again at 0

119

120 thisChar = 'a';

121

122 }

123

124 // print the character

125

126 lcd.write(thisChar);

127

128 // wait a second:

129

130 delay(1000);

131

132 // increment the letter:

133

134 thisChar++;

135}

Custom Character

This example demonstrates how to add custom characters on an LCD display.

Note that this example requires an additional potentiometer:

  • Outer pins connected to 5V and GND.
  • Inner pin (wiper) connected to A0.

This potentiometer controls the

delayTime

variable.

1/*

2 LiquidCrystal Library - Custom Characters

3

4 Demonstrates how to add custom characters on an LCD display.

5 The LiquidCrystal library works with all LCD displays that are

6 compatible with the Hitachi HD44780 driver. There are many of

7 them out there, and you can usually tell them by the 16-pin interface.

8

9 This sketch prints "I <heart> Arduino!" and a little dancing man

10 to the LCD.

11

12 The circuit:

13 * LCD RS pin to digital pin 12

14 * LCD Enable pin to digital pin 11

15 * LCD D4 pin to digital pin 5

16 * LCD D5 pin to digital pin 4

17 * LCD D6 pin to digital pin 3

18 * LCD D7 pin to digital pin 2

19 * LCD R/W pin to ground

20 * 10K potentiometer:

21 * ends to +5V and ground

22 * wiper to LCD VO pin (pin 3)

23 * 10K poterntiometer on pin A0

24

25 created 21 Mar 2011

26 by Tom Igoe

27 modified 11 Nov 2013

28 by Scott Fitzgerald

29 modified 7 Nov 2016

30 by Arturo Guadalupi

31

32 Based on Adafruit's example at

33 https://github.com/adafruit/SPI_VFD/blob/master/examples/createChar/createChar.pde

34

35 This example code is in the public domain.

36 http://www.arduino.cc/en/Tutorial/LiquidCrystalCustomCharacter

37

38 Also useful:

39 http://icontexto.com/charactercreator/

40

41*/

42

43// include the library code:

44#include <LiquidCrystal.h>

45

46// initialize the library by associating any needed LCD interface pin

47// with the arduino pin number it is connected to

48const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

49LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

50

51// make some custom characters:

52byte heart[8] = {

53 0b00000,

54 0b01010,

55 0b11111,

56 0b11111,

57 0b11111,

58 0b01110,

59 0b00100,

60 0b00000

61};

62

63byte smiley[8] = {

64 0b00000,

65 0b00000,

66 0b01010,

67 0b00000,

68 0b00000,

69 0b10001,

70 0b01110,

71 0b00000

72};

73

74byte frownie[8] = {

75 0b00000,

76 0b00000,

77 0b01010,

78 0b00000,

79 0b00000,

80 0b00000,

81 0b01110,

82 0b10001

83};

84

85byte armsDown[8] = {

86 0b00100,

87 0b01010,

88 0b00100,

89 0b00100,

90 0b01110,

91 0b10101,

92 0b00100,

93 0b01010

94};

95

96byte armsUp[8] = {

97 0b00100,

98 0b01010,

99 0b00100,

100 0b10101,

101 0b01110,

102 0b00100,

103 0b00100,

104 0b01010

105};

106

107void setup() {

108 // initialize LCD and set up the number of columns and rows:

109 lcd.begin(16, 2);

110

111 // create a new character

112 lcd.createChar(0, heart);

113 // create a new character

114 lcd.createChar(1, smiley);

115 // create a new character

116 lcd.createChar(2, frownie);

117 // create a new character

118 lcd.createChar(3, armsDown);

119 // create a new character

120 lcd.createChar(4, armsUp);

121

122 // set the cursor to the top left

123 lcd.setCursor(0, 0);

124

125 // Print a message to the lcd.

126 lcd.print("I ");

127 lcd.write(byte(0)); // when calling lcd.write() '0' must be cast as a byte

128 lcd.print(" Arduino! ");

129 lcd.write((byte)1);

130

131}

132

133void loop() {

134 // read the potentiometer on A0:

135 int sensorReading = analogRead(A0);

136 // map the result to 200 - 1000:

137 int delayTime = map(sensorReading, 0, 1023, 200, 1000);

138 // set the cursor to the bottom row, 5th position:

139 lcd.setCursor(4, 1);

140 // draw the little man, arms down:

141 lcd.write(3);

142 delay(delayTime);

143 lcd.setCursor(4, 1);

144 // draw him arms up:

145 lcd.write(4);

146 delay(delayTime);

147}