Skip to content
Home » Arduino Mega Lcd 16X2 | More Information

Arduino Mega Lcd 16X2 | More Information

16x2 LCD interface with Arduino Mega at Chips

References

[1] https://www.arduino.cc/en/Reference/LiquidCrystal

[2] https://www.arduino.cc/en/Reference/LiquidCrystalConstructor

Hardware components
Software apps and online services

Connecting an LCD screen was never this easy before. Connect an LCD (16*2) to Arduino MEGA 2560 without literally using any wires.

You just need the two things, and you have completed this tutorial’s requirement.

There is no need to explain much in this tutorial here!!!

you have to do nothing. Just take the LCD screen and connect it as follows.

Just connect VSS to A0 using the image as reference. The other pins will go in naturally.

Congrats! your circuit is complete under 10 seconds!!

(don’t worry that the d2 (of lcd)pin did not go inside any header. D3 to D0 are not required actually for this tutorial.)

But yes the main thing that differs here is the code. I will just give you the code, No need to explain it also. Only change the pins in code with few extra addition.

For eg. Here is the original Liquid crystal example to display “hello world”:


// include the library code: #include // initialize the library by associating any needed LCD interface pin // with the arduino pin number it is connected to const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("hello, world!"); } void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // print the number of seconds since reset: lcd.print(millis() / 1000); }

And here is the modified version:


#include // initialize the library by associating any needed LCD interface pin // with the arduino pin number it is connected to const int rs = A3, en = A5, d4 = A9, d5 = A10, d6 = A11, d7 = A12; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { pinMode(A14,OUTPUT); pinMode(A13,OUTPUT); pinMode(A4,OUTPUT); pinMode(A0,OUTPUT); pinMode(A2,OUTPUT); pinMode(A1,OUTPUT); digitalWrite(A14,LOW); digitalWrite(A13,HIGH); digitalWrite(A4,LOW); digitalWrite(A0,LOW); digitalWrite(A2,LOW); digitalWrite(A1,HIGH); lcd.begin(16, 2); // Print a message to the LCD. lcd.print("hello, world!"); } void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // print the number of seconds since reset: lcd.print(millis() / 1000); }

Likewise, just remove the part of the code to print seconds and “hello world”, And now you can use this code as a snippet.

Please follow, share and comment if you liked the project!

Liquid Crystal Displays (LCD) with Arduino

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.

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 16×2 LCD in 4-bit mode.

Parts Required

  • Arduino Mega 2560;
  • USB 2.0 Cable Type A/B;
  • 1 Liquid Crystal Display (LCD);
  • 1 220Ω resistor;
  • 1 10kΩ potentiometer;
  • Male to male jumper wires;
  • 1 Breadboard.

Note: Since only 6 digital pins are required for you to follow this tutorial, an ESP8266 NodeMCU board or any similar board can be used instead. Just be sure to change the pin numbers in the code if you use other boards.

16x2 LCD interface with Arduino Mega at Chips
16×2 LCD interface with Arduino Mega at Chips

Schematic

Hello World Example

This example sketch prints

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

Hello World!

1/*2 LiquidCrystal Library – Hello World34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch prints “Hello World!” to the LCD10 and shows the time.1112 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * LCD VSS pin to ground21 * LCD VCC pin to 5V22 * 10K resistor:23 * ends to +5V and ground24 * wiper to LCD VO pin (pin 3)2526 Library originally added 18 Apr 200827 by David A. Mellis28 library modified 5 Jul 200929 by Limor Fried (http://www.ladyada.net)30 example added 9 Jul 200931 by Tom Igoe32 modified 22 Nov 201033 by Tom Igoe34 modified 7 Nov 201635 by Arturo Guadalupi3637 This example code is in the public domain.3839 https://docs.arduino.cc/learn/electronics/lcd-displays4041*/4243// include the library code:44#include 4546// initialize the library by associating any needed LCD interface pin47// with the arduino pin number it is connected to48const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;49LiquidCrystal lcd(rs, en, d4, d5, d6, d7);5051void 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}5758void loop() {59 // set the cursor to column 0, line 160 // (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

and

autoscroll()

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

noAutoscroll()

  • moves all the text one space to the left each time a letter is added


    autoscroll()

  • turns scrolling off


    noAutoscroll()

This sketch prints the characters

to

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

1/*23 LiquidCrystal Library – Autoscroll45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch demonstrates the use of the autoscroll()1415 and noAutoscroll() functions to make new text scroll or not.1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475void setup() {7677 // set up the LCD’s number of columns and rows:7879 lcd.begin(16, 2);80}8182void loop() {8384 // set the cursor to (0,0):8586 lcd.setCursor(0, 0);8788 // print from 0 to 9:8990 for (int thisChar = 0; thisChar < 10; thisChar++) {9192 lcd.print(thisChar);9394 delay(500);9596 }9798 // set the cursor to (16,1):99100 lcd.setCursor(16, 1);101102 // set the display to automatically scroll:103104 lcd.autoscroll();105106 // print from 0 to 9:107108 for (int thisChar = 0; thisChar < 10; thisChar++) {109110 lcd.print(thisChar);111112 delay(500);113114 }115116 // turn off automatic scrolling117118 lcd.noAutoscroll();119120 // clear screen for the next loop:121122 lcd.clear();123}

Blink Example

This example sketch shows how to use the

and

blink()

methods to blink a block-style cursor.

noBlink()

1/*23 LiquidCrystal Library – Blink45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch prints “Hello World!” to the LCD and makes the1415 cursor block blink.1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalBlink6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475void setup() {7677 // set up the LCD’s number of columns and rows:7879 lcd.begin(16, 2);8081 // Print a message to the LCD.8283 lcd.print(“hello, world!”);84}8586void loop() {8788 // Turn off the blinking cursor:8990 lcd.noBlink();9192 delay(3000);9394 // Turn on the blinking cursor:9596 lcd.blink();9798 delay(3000);99}

Cursor

This example sketch shows how to use the

and

cursor()

methods to control an underscore-style cursor.

noCursor()

1/*23 LiquidCrystal Library – Cursor45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch prints “Hello World!” to the LCD and1415 uses the cursor() and noCursor() methods to turn1617 on and off the cursor.1819 The circuit:2021 * LCD RS pin to digital pin 122223 * LCD Enable pin to digital pin 112425 * LCD D4 pin to digital pin 52627 * LCD D5 pin to digital pin 42829 * LCD D6 pin to digital pin 33031 * LCD D7 pin to digital pin 23233 * LCD R/W pin to ground3435 * 10K resistor:3637 * ends to +5V and ground3839 * wiper to LCD VO pin (pin 3)4041 Library originally added 18 Apr 20084243 by David A. Mellis4445 library modified 5 Jul 20094647 by Limor Fried (http://www.ladyada.net)4849 example added 9 Jul 20095051 by Tom Igoe5253 modified 22 Nov 20105455 by Tom Igoe5657 modified 7 Nov 20165859 by Arturo Guadalupi6061 This example code is in the public domain.6263 http://www.arduino.cc/en/Tutorial/LiquidCrystalCursor6465*/6667// include the library code:68#include 6970// initialize the library by associating any needed LCD interface pin71// with the arduino pin number it is connected to7273const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7475LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7677void setup() {7879 // set up the LCD’s number of columns and rows:8081 lcd.begin(16, 2);8283 // Print a message to the LCD.8485 lcd.print(“hello, world!”);86}8788void loop() {8990 // Turn off the cursor:9192 lcd.noCursor();9394 delay(500);9596 // Turn on the cursor:9798 lcd.cursor();99100 delay(500);101}

Display Example

This example sketch shows how to use the

and

display()

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.

noDisplay()

1/*2 LiquidCrystal Library – display() and noDisplay()34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch prints “Hello World!” to the LCD and uses the10 display() and noDisplay() functions to turn on and off11 the display.1213 The circuit:14 * LCD RS pin to digital pin 1215 * LCD Enable pin to digital pin 1116 * LCD D4 pin to digital pin 517 * LCD D5 pin to digital pin 418 * LCD D6 pin to digital pin 319 * LCD D7 pin to digital pin 220 * LCD R/W pin to ground21 * 10K resistor:22 * ends to +5V and ground23 * wiper to LCD VO pin (pin 3)2425 Library originally added 18 Apr 200826 by David A. Mellis27 library modified 5 Jul 200928 by Limor Fried (http://www.ladyada.net)29 example added 9 Jul 200930 by Tom Igoe31 modified 22 Nov 201032 by Tom Igoe33 modified 7 Nov 201634 by Arturo Guadalupi3536 This example code is in the public domain.3738 http://www.arduino.cc/en/Tutorial/LiquidCrystalDisplay3940*/4142// include the library code:43#include 4445// initialize the library by associating any needed LCD interface pin46// with the arduino pin number it is connected to47const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;48LiquidCrystal lcd(rs, en, d4, d5, d6, d7);4950void 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}5657void 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

and

scrollDisplayLeft()

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.

scrollDisplayRight()

1/*2 LiquidCrystal Library – scrollDisplayLeft() and scrollDisplayRight()34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch prints “Hello World!” to the LCD and uses the10 scrollDisplayLeft() and scrollDisplayRight() methods to scroll11 the text.1213 The circuit:14 * LCD RS pin to digital pin 1215 * LCD Enable pin to digital pin 1116 * LCD D4 pin to digital pin 517 * LCD D5 pin to digital pin 418 * LCD D6 pin to digital pin 319 * LCD D7 pin to digital pin 220 * LCD R/W pin to ground21 * 10K resistor:22 * ends to +5V and ground23 * wiper to LCD VO pin (pin 3)2425 Library originally added 18 Apr 200826 by David A. Mellis27 library modified 5 Jul 200928 by Limor Fried (http://www.ladyada.net)29 example added 9 Jul 200930 by Tom Igoe31 modified 22 Nov 201032 by Tom Igoe33 modified 7 Nov 201634 by Arturo Guadalupi3536 This example code is in the public domain.3738 http://www.arduino.cc/en/Tutorial/LiquidCrystalScroll3940*/4142// include the library code:43#include 4445// initialize the library by associating any needed LCD interface pin46// with the arduino pin number it is connected to47const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;48LiquidCrystal lcd(rs, en, d4, d5, d6, d7);4950void 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}5758void loop() {59 // scroll 13 positions (string length) to the left60 // 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 }6768 // scroll 29 positions (string length + display length) to the right69 // 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 }7677 // scroll 16 positions (display length + string length) to the left78 // 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 }8586 // delay at the end of the full loop:87 delay(1000);8889}

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 Input34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch displays text sent over the serial port10 (e.g. from the Serial Monitor) on an attached LCD.1112 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * 10K resistor:21 * ends to +5V and ground22 * wiper to LCD VO pin (pin 3)2324 Library originally added 18 Apr 200825 by David A. Mellis26 library modified 5 Jul 200927 by Limor Fried (http://www.ladyada.net)28 example added 9 Jul 200929 by Tom Igoe30 modified 22 Nov 201031 by Tom Igoe32 modified 7 Nov 201633 by Arturo Guadalupi3435 This example code is in the public domain.3637 http://www.arduino.cc/en/Tutorial/LiquidCrystalSerialDisplay3839*/4041// include the library code:42#include 4344// initialize the library by associating any needed LCD interface pin45// with the arduino pin number it is connected to46const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;47LiquidCrystal lcd(rs, en, d4, d5, d6, d7);4849void 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}5556void loop() {57 // when characters arrive over the serial port…58 if (Serial.available()) {59 // wait a bit for the entire message to arrive60 delay(100);61 // clear the screen62 lcd.clear();63 // read all the available characters64 while (Serial.available() > 0) {65 // display each character to the LCD66 lcd.write(Serial.read());67 }68 }69}

Set Cursor Example

This example sketch shows how to use the

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

setCursor()

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

setCursor()

1lcd.setCursor(0, 0); // top left2lcd.setCursor(15, 0); // top right3lcd.setCursor(0, 1); // bottom left4lcd.setCursor(15, 1); // bottom right

Here is the full example:

1/*23 LiquidCrystal Library – setCursor45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch prints to all the positions of the LCD using the1415 setCursor() method:1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalSetCursor6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475// these constants won’t change. But you can change the size of76// your LCD using them:7778const int numRows = 2;7980const int numCols = 16;8182void setup() {8384 // set up the LCD’s number of columns and rows:8586 lcd.begin(numCols, numRows);87}8889void loop() {9091 // loop from ASCII ‘a’ to ASCII ‘z’:9293 for (int thisLetter = ‘a’; thisLetter <= ‘z’; thisLetter++) {9495 // loop over the columns:9697 for (int thisRow = 0; thisRow < numRows; thisRow++) {9899 // loop over the rows:100101 for (int thisCol = 0; thisCol < numCols; thisCol++) {102103 // set the cursor position:104105 lcd.setCursor(thisCol, thisRow);106107 // print the letter:108109 lcd.write(thisLetter);110111 delay(200);112113 }114115 }116117 }118}

Text Direction Example

This example sketch shows how to use the

and

leftToRight()

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.


    rightToLeft()

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


    leftToRight()

This sketch prints

through

right to left, then

through

left to right, then

through

right to left again.

1/*23 LiquidCrystal Library – TextDirection45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch demonstrates how to use leftToRight() and rightToLeft()1415 to move the cursor.1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalTextDirection6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475int thisChar = ‘a’;7677void setup() {7879 // set up the LCD’s number of columns and rows:8081 lcd.begin(16, 2);8283 // turn on the cursor:8485 lcd.cursor();86}8788void loop() {8990 // reverse directions at ‘m’:9192 if (thisChar == ‘m’) {9394 // go right for the next letter9596 lcd.rightToLeft();9798 }99100 // reverse again at ‘s’:101102 if (thisChar == ‘s’) {103104 // go left for the next letter105106 lcd.leftToRight();107108 }109110 // reset at ‘z’:111112 if (thisChar > ‘z’) {113114 // go to (0,0):115116 lcd.home();117118 // start again at 0119120 thisChar = ‘a’;121122 }123124 // print the character125126 lcd.write(thisChar);127128 // wait a second:129130 delay(1000);131132 // increment the letter:133134 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

variable.

delayTime

1/*2 LiquidCrystal Library – Custom Characters34 Demonstrates how to add custom characters on an LCD display.5 The LiquidCrystal library works with all LCD displays that are6 compatible with the Hitachi HD44780 driver. There are many of7 them out there, and you can usually tell them by the 16-pin interface.89 This sketch prints “I

Arduino!” and a little dancing man10 to the LCD.1112 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * 10K potentiometer:21 * ends to +5V and ground22 * wiper to LCD VO pin (pin 3)23 * 10K poterntiometer on pin A02425 created 21 Mar 201126 by Tom Igoe27 modified 11 Nov 201328 by Scott Fitzgerald29 modified 7 Nov 201630 by Arturo Guadalupi3132 Based on Adafruit’s example at33 https://github.com/adafruit/SPI_VFD/blob/master/examples/createChar/createChar.pde3435 This example code is in the public domain.36 https://docs.arduino.cc/learn/electronics/lcd-displays#custom-character3738 Also useful:39 http://icontexto.com/charactercreator/4041*/4243// include the library code:44#include 4546// initialize the library by associating any needed LCD interface pin47// with the arduino pin number it is connected to48const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;49LiquidCrystal lcd(rs, en, d4, d5, d6, d7);5051// make some custom characters:52byte heart[8] = {53 0b00000,54 0b01010,55 0b11111,56 0b11111,57 0b11111,58 0b01110,59 0b00100,60 0b0000061};6263byte smiley[8] = {64 0b00000,65 0b00000,66 0b01010,67 0b00000,68 0b00000,69 0b10001,70 0b01110,71 0b0000072};7374byte frownie[8] = {75 0b00000,76 0b00000,77 0b01010,78 0b00000,79 0b00000,80 0b00000,81 0b01110,82 0b1000183};8485byte armsDown[8] = {86 0b00100,87 0b01010,88 0b00100,89 0b00100,90 0b01110,91 0b10101,92 0b00100,93 0b0101094};9596byte armsUp[8] = {97 0b00100,98 0b01010,99 0b00100,100 0b10101,101 0b01110,102 0b00100,103 0b00100,104 0b01010105};106107void setup() {108 // initialize LCD and set up the number of columns and rows:109 lcd.begin(16, 2);110111 // create a new character112 lcd.createChar(0, heart);113 // create a new character114 lcd.createChar(1, smiley);115 // create a new character116 lcd.createChar(2, frownie);117 // create a new character118 lcd.createChar(3, armsDown);119 // create a new character120 lcd.createChar(4, armsUp);121122 // set the cursor to the top left123 lcd.setCursor(0, 0);124125 // 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 byte128 lcd.print(” Arduino! “);129 lcd.write((byte)1);130131}132133void 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}

Suggested changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.

    • Tổng tiền thanh toán:
Code test cơ bản

#include

void loop()

Code Đếm số 0 đến 9 LCD 16×2 I2C

#include

void loop()

Code test cơ bản

#include

void loop()

Code Đếm số 0 đến 9 LCD 16×2 I2C

#include

void loop()

Arduino Mega 2560 LCD Display Control Code

HD44780 character LCD displays are monochrome visualisation devices. You can get them in 16×1, 16×2 and 20×4 dimensions, which represents columns x rows.
It is able to display 240 different built in characters, but you can create your own characters if you wish.
Set the cursor anywhere and display character strings or clear display anytime. The backlight can be turned on or off depending on your choice (Figure 1).On Figure 2 you can see a connection where the backlight is continuously powered by 5V. The backlight color can differ depending on the LCD’s specification. Scroll below to see some codes for both Figure 1 and 2. Upload the code for your wiring, so Ozeki 10 can use your LCD connection.

Lời kết

Qua bài hôm nay các bạn biết cách làm thế nào để hiển thị các ký tự và chuỗi ký tự lên LCD 16×2 và biết cách giao tiếp I2C.

Để nhận được nhiều kiến thức mới các bạn Đăng ký để nhận được thông báo sớm nhất.

Tham gia Cộng đồng Arduino KIT để cùng nhau thảo luận và chia sẽ kiến thức về lập trình Arduino.

Nếu các bạn thấy bài viết bổ ích nhớ Like và Share cho mọi người cùng đọc nhé.

Chúc các bạn thành công.

Trân trọng.

In this tutorial, the goal is to guide you into the basic functions of a Liquid Crystal Display (LCD). Besides learning how to set it up on an Arduino, this tutorial will also show you how to print text, scroll it, position it anywhere in your LCD and even create custom characters. This type of hardware is very useful whenever we need to output some information like time and date, the temperature inside a room or the values measured by any sensor you are using. Has you can imagine, LCDs can make our projects a lot more interactive, so let´s get to work!

How to connect an I2C LCD Display to an Arduino MEGA 2560
How to connect an I2C LCD Display to an Arduino MEGA 2560

Code mẫu

#include

#include LiquidCrystal_I2C lcd(0x3F,16,2); void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(2,0); lcd.print(“Arduinokit.vn”); lcd.setCursor(0,1); lcd.print(“Xin chao cac ban”); } void loop() { }

Giải thích code

LiquidCrystal_I2C lcd(0x3F,16,2);

  • Đặt địa chỉ LCD là 0x3F cho màn hình LCD 16×2.
  • 16 là số cột của màn hình (nếu dùng loại màn hình 20×4) thì thay bằng 20.
  • 2 là số dòng của màn hình (nếu dùng loại màn hình 20×4) thì thay bằng 4.

lcd.init();

Khởi động màn hình LCD, bắt đầu cho phép Arduino sử dụng màn hình.

lcd.backlight();

Bật đèn nền LCD 16×2.

lcd.setCursor(2,0);

Đưa con trỏ tới hàng 1, cột 3.

Lưu ý: giá trị hàng và cột bắt đầu từ số 0 có nghĩa 0 là hàng(cột) 1.

lcd.print(“Arduinokit.vn”);

Xuất ra dòng chữ Arduinokit.vn tại vị trí con trỏ ở hàng 1, cột 3.

lcd.setCursor(0,1); lcd.print(“Xin chao cac ban”);

Đoạn code này thì tương tự như trên, xuất ra dòng chữ “Xin chao cac ban” tại vị trí con trỏ ở hàng 2, cột 1.

Bây giờ thì các bạn upload chương trình và xem kết quả nhé.

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).

Arduino mega lcd tutorial
Arduino mega lcd tutorial

The 4 pin source code to install on controller (Figure 1)

Before you upload this code to your Arduino, please format the EEPROM…

#include

#include

// global pointers OzIDManager* manager; OzLcdController* lcdController; const int backlight_pin = 3; const int RS = 6; const int RW = 7; const int EN = 8; const int D4 = 9; const int D5 = 10; const int D6 = 11; const int D7 = 12; void setup() { Serial.begin(115200); manager = new OzIDManager; manager->_sendACK = true; manager->_checksum = true; OzCommunication::setIDManager(manager); lcdController = new OzLcdController(RS, RW, EN, D4, D5, D6, D7); lcdController->SetBacklight(backlight_pin, 100); //100% backlight int x=1; manager->sendLinkSetup(); manager->PrintWelcomeLine(lcdController, x++, “MyLCD”); lcdController->LoginDisplaySize(16, 2); } void loop() { OzCommunication::communicate(); }

Các lỗi thường gặp khi sử dụng I2C LCD

  • Hiển thị một dãy ô vuông.
  • Màn hình chỉ in ra một ký tự đầu.
  • Màn hình nhấp nháy.

Các lỗi này chủ yếu là do sai địa chỉ bus, để fix lỗi các bạn thay địa chỉ mặc định là “0x27” thành “0x3F.

Trong trường hợp vẫn không được các bạn fix lỗi bằng cách nạp code tìm địa chỉ bus của I2C.

Sau khi tìm xong các bạn thay địa chỉ vừa tìm được vào vị trí “0x27” là xong.

  • Các bạn có thể tải code tìm địa chỉ bus ở đây. Tải ngay.
How to Use I2C LCD with Arduino | Very Easy Arduino LCD I2C Tutorial | Arduino 16x2 LCD I2C Tutorial
How to Use I2C LCD with Arduino | Very Easy Arduino LCD I2C Tutorial | Arduino 16×2 LCD I2C Tutorial

Giao tiếp I2C LCD Arduino

Module I2C LCD 16×2 Arduino UNO
GND GND
VCC 5V
SDA A4/SDA
SCL A5/SCL

Sơ đồ đấu nối

Các linh kiện cần thiết cho dự án:

Tên linh kiện Số lượng Shopee
Arduino UNO R3 Mua ngay
Dây cáp nạp Mua ngay
Màn hình LCD 16×2 Mua ngay
Module I2C LCD 16×2 Mua ngay
Dây cắm (Đực – Cái) Mua ngay

Bạn sẽ học được gì

  • Có kiến thức cơ bản về Robotics
  • Chế tạo Robot dò đường thông minh
  • Đánh thức nhà khoa học bên trong bạn
  • Tìm hiểu thêm về Robotics, các thuật toán Robot tự động
  • Kiến thức nền tảng để chế tạo các máy móc tự động phục vụ đời sống sinh hoạt, lao động sản xuất
  • Kiến thức để chế tạo sản phẩm, tham gia các cuộc thi khoa học công nghệ trong nước và quốc tế

Code

At last, we can start developing our code. To do so, we have to use the LiquidCrystal library, which allows our Arduino to control any LCD based on the Hitachi HD44780 LCD controller, which is found on most text-based LCDs [1]. Fortunately for us, this library is pre-installed in the Arduino IDE.

After including it in our code, we must create an LCD object (lcd) in order to define its parameters, meaning that we need to indicate which Arduino pins are going to be connected to the LCD pins. To do so, we have to know the syntax to set these parameters [2], which is shown below:

LiquidCrystal lcd(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7)

Considering that we are using the 4-bit mode and that the LCD pin RW is connected to ground, we can omit pins d0 to d3 and rw. The resulting code is as follows:

LiquidCrystal lcd(rs, enable, d4, d5, d6, d7)

With all the parameters defined, we can then start to write what we want to display. In this tutorial, I will display in a loop an introductory message, followed by our web site’s name with a smile blinking an eye in the middle of the LCD’s first row and then an ending message scrolling from right to left. All the library functions used in this tutorial and more are presented here, so go on and explore all of them!

If, alternatively, you are using an ESP8266 NodeMCU board, you may need to change the pin numbers in the line of code where the parameters are defined (line 5). In case you need help with that, you may found useful to check our tutorial “ESP8266 NodeMCU – Blinking a LED” to see the corresponding pin numbers for this board.

// Includes the library code #include // Defines the LCD’s parameters: (rs, enable, d4, d5, d6, d7) LiquidCrystal lcd(9, 8, 5, 4, 3, 2); // Defines the first user defined character – 🙂 byte smile1[8] = { 0b00000, 0b11010, 0b11001, 0b00001, 0b00001, 0b11001, 0b11010, 0b00000 }; // Defines the second user defined character – 😉 byte smile2[8] = { 0b00000, 0b11010, 0b11001, 0b00001, 0b01001, 0b01001, 0b10010, 0b00000 }; void setup() { // Set up the LCD’s number of columns and rows lcd.begin(16, 2); } void loop() { // Creates the user defined characters – 🙂 and 😉 lcd.createChar(1, smile1); lcd.createChar(2, smile2); // Prints the introductory message // Prints the first part of the message to the LCD lcd.print(“Hello everyone!”); // Sets the cursor to column 0, row 2 // (note: the counting of rows and columns begins with 0) lcd.setCursor(0, 1); // Prints the second part of the message to the LCD lcd.print(“We are …”); delay(2000); // Clears the LCD screen lcd.clear(); // Prints the website’s name and the user defined // characters to the LCD // Sets the cursor to column 3, row 1 (so that it appears // in the middle of the LCD) lcd.setCursor(2, 0); // Prints the website’s name and characters separately lcd.print(“GEE”); delay(500); lcd.print(“KE”); delay(500); lcd.print(“RING”); delay(500); lcd.setCursor(12, 0); // Prints the first character lcd.write(byte(1)); delay(500); // Prints the second character (only for a short period // of time to make a blinking efect) lcd.setCursor(12, 0); lcd.write(byte(2)); delay(300); lcd.setCursor(12, 0); // Prints the first character again lcd.write(byte(1)); delay(2000); lcd.clear(); // Prints the ending message lcd.setCursor(15, 0); lcd.print(” visit us on”); lcd.setCursor(15, 1); lcd.print(“www.geekering.com”); // Loop for scrolling the message since it begins (from // the right side of the LCD) until it desapears for (int i = 0; i < 32; i++) { lcd.scrollDisplayLeft(); delay(400); } }

If you enjoyed this tutorial, you can visit our YouTube channel and watch this and many other videos. Thanks for following us and be sure to rate, comment and share our content. Have fun!!

Arduino MEGA I2C LCD Tutorial
Arduino MEGA I2C LCD Tutorial

Module I2C Arduino

LCD có quá nhiều nhiều chân gây khó khăn trong quá trình đấu nối và chiếm dụng nhiều chân trên vi điều khiển.

Module I2C LCD ra đời và giải quyết vấn để này cho bạn.

Thay vì phải mất 6 chân vi điều khiển để kết nối với LCD 16×2 (RS, EN, D7, D6, D5 và D4) thì module IC2 bạn chỉ cần tốn 2 chân (SCL, SDA) để kết nối.

Module I2C hỗ trợ các loại LCD sử dụng driver HD44780(LCD 16×2, LCD 20×4, …) và tương thích với hầu hết các vi điều khiển hiện nay.

Ưu điểm

  • Tiết kiệm chân cho vi điều khiển.
  • Dễ dàng kết nối với LCD.

Thông số kĩ thuật

  • Điện áp hoạt động: 2.5-6V DC.
  • Hỗ trợ màn hình: LCD1602,1604,2004 (driver HD44780).
  • Giao tiếp: I2C.
  • Địa chỉ mặc định: 0X27 (có thể điều chỉnh bằng ngắn mạch chân A0/A1/A2).
  • Tích hợp Jump chốt để cung cấp đèn cho LCD hoặc ngắt.
  • Tích hợp biến trở xoay điều chỉnh độ tương phản cho LCD.

Để sử dụng màn hình LCD giao tiếp I2C sử dụng Arduino thì ta cần cài đặt thư viện Liquidcrystal_I2C. Tại đây

The 8 pin source code to install on controller (Figure 2)

Before you upload this code to your Arduino, please format the EEPROM…

#include

#include

// global pointers OzIDManager* manager; OzLcdController* lcdController; const int RS = 2; const int RW = 3; const int EN = 4; const int D0 = 5; const int D1 = 6; const int D2 = 7; const int D3 = 8; const int D4 = 9; const int D5 = 10; const int D6 = 11; const int D7 = 12; void setup() { // wait for serial port Serial.begin(115200); // instantiate objects manager = new OzIDManager; manager->_sendACK = true; manager->_checksum = true; OzCommunication::setIDManager(manager); lcdController = new OzLcdController(RS, RW, EN, D0, D1, D2, D3, D4, D5, D6, D7); // welcome devices int x = 1; manager->sendLinkSetup(); manager->PrintWelcomeLine(lcdController, x++, “MyLCD”); lcdController->LoginDisplaySize(16, 2); } void loop() { OzCommunication::communicate(); }

How to use 16*2 lcd display with arduino mega 2560?
How to use 16*2 lcd display with arduino mega 2560?

More information

  • How to setup a lcd display on arduino nano
  • How to setup a lcd display on arduino uno
  • How to setup a lcd display on Ozeki matrix
  • How to setup a lcd display on Raspberry Pi
  • LCD display protocol for arduino
  • How to control a lcd display using chat messages
  • LCD Arduino Desc
  • LCD 4 pin Arduino
  • LCD Matrix Desc
  • LCD 8 pin Arduino

Tư vấn: 0979.466.469 / 0938.128.290 Email: [email protected]

Giao tiếp module LCD 16×2 với Arduino, bài viết sẽ nói rõ chức năng từng chân của LCD và cách kết nối module LCD với LCD

GIAO TIẾP MODULE LCD VỚI ARDUINO

Trên thị trường có rất nhiều loại module LCD, các loại màn hình LCD được sử dụng phổ biến có thể bạn biết là LCD 16×2 ký tự, LCD 20×4 ký tự, module LCD Nokia 5110, Màn hình LCD đồ họa 128 × 64 và màn hình LCD cảm ứng LCD 2,4 inch. Bài viết này sẽ tập trung vào đối tượng LCD 16×2, là loại LCD gồm 2 hàng hiển thị, có thể hiển thị tổng cộng 32 ký tự trong mã ASCII lên màn hình, nghĩa là một hàng có thể hiển thị tối đa 16 ký tự. Tương tự với loại LCD 20×4, sẽ có 4 hàng hiển thị, mỗi hàng hiển thị được 20 ký tự

Sơ đồ các chân của LCD 16×2:

Module LCD JHD162A có 16 chân và có thể được vận hành ở chế độ 4 bit hoặc 8 bit. Ở đây chúng ta đang sử dụng module LCD ở chế độ 4 bit. Các chân của module bao gồm các chân được đánh số từ 1 đến 16 theo chiều từ trái sang phải, chúng ta lần lượt quan sát các chân từ trái sang phải nhé:

– Vss: Chân nối mass (GND)

– Vcc: Chân nối nguồn cấp điện (5VDC)

– Vee: Là chân điều chỉnh độ tương phản, độ tương phản sẽ thay đổi tương ứng với các giá trị điện áp được cấp vào trong này, thường thì mức cài đặt chuẩn là 0.4V đến 0.9V

– RS: Chân chọn thanh ghi, module này có 2 thanh ghi để lựa chọn (thanh ghi lệnh và thanh ghi giữ liệu), mức logic cao (mức 1) là tín hiệu chọn thanh ghi dữ liệu, mức thấp (mức 0) là tín hiệu chọn thanh ghi lệnh. Ví dụ nếu chân RS ở mức cao và các chân dữ liệu của module LCD từ DB0 đến DB7 đồng thời được truyền tín hiệu kỹ thuật số (tín hiệu mức logic 0 và 1) thì dữ liệu tương ứng từ các chân tín hiệu sẽ được lập tức hiển thị trên màn hình LCD. Còn nếu RS ở mức 0 và ta truyền tín hiệu vào các chân tín hiệu thì đồng nghĩa chúng ta đang ra lệnh cho LCD thực thi một lệnh nào đó (lệnh này sẽ được ghi vào bộ điều khiển của LCD, ví dụ như lệnh sắp đặt vị trí con trỏ, lệnh xoá màn hình,…)

– R/W: lựa chọn chế độ đọc hoặc ghi, R/W ở mức cao sẽ kích hoạt chế độ đọc, R/W ở mức thấp thì kích hoạt chế độ ghi

– E: cấp vào chân tín hiệu tích cực cạnh xuống (tín hiệu mức cao chuyển về mức thấp) sẽ kích hoạt và cho phép module LCD hoạt động

– Các chân từ DB0 đến DB7: là các chân tín hiệu ngõ vào LCD

– LED+: Chân anode của các đèn led hiển thị của LCD, khi cấp nguồn vào chân này nên mắc kèm theo điện trở để hạn dòng

– LED- : Chân cathode của các đèn led hiển thị của LCD

Sơ đồ giao tiếp giữa LCD với Arduino

Chân RS của module LCD được kết nối với chân số 12 (chân Digital) của arduino. Chân R / W của LCD được nối đất. Chân E của module LCD được kết nối với chân số 11 (là chân Digital) của arduino. Trong mạch này, module LCD và arduino được giao tiếp ở chế độ 4 bit. Điều này có nghĩa là chỉ có bốn trong số chân Digital đầu vào hay còn gọi chân tín hiệu số (DB4 đến DB7) của LCD được sử dụng. Phương pháp này rất đơn giản, yêu cầu ít kết nối hơn và bạn gần như có thể sử dụng toàn bộ các chức năng của module LCD. Các chân tín hiệu số DB4, DB5, DB6 và DB7 được giao tiếp (kết nối) với các chân tín hiệu số (Digital) số 5, 4, 3 và 2 của Arduino. Chân Vee được mắc với mạch chiết áp 10K, biến trở 10K được sử dụng để điều chỉnh độ tương phản của màn hình. Điện trở 560 ohm R1 giới hạn dòng điện đi qua đèn LED phía sau màn hình LCD. Chương trình đầy đủ để giao tiếp LCD với arduino được hiển thị bên dưới:

#include

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // khai báo các chân giao tiếp của Arduino

void setup()

lcd.begin(16, 2); // khởi tạo module 16×2 LCD

void loop()

lcd.setCursor(0,0); //đặt con trỏ ở hàng 0 cột 0

lcd.print(“16×2 LCD MODULE”); // in ra chuỗi ký tự 16×2 LCD MODULE

lcd.setCursor(2,1); //đặt con trỏ ở hàng 1 cột 2

lcd.print(“HELLO WORLD”); // in ra màn hình lcd chuỗi ký tự HELLO WORLD

Để thuận tiện cho việc giao tiếp giữa module Arduino và LCD, chúng ta sử dụng thư viện tích hợp trong Arduino là – được viết cho các module LCD sử dụng chipset Hitachi HD44780 (hoặc chipset tương thích). Thư viện này có thể xử lý cả chế độ 4 bit và hệ thống dây 8 bit của LCD.

Tham khảo – tài liệu của Thư viện LiquidCrystal – trước khi bạn tiếp tục!

Thư viện Liquid LiquidCrystal.h được sử dụng để dễ dàng điều khiển module LCD bằng Arduino nhờ sự trợ giúp của các lệnh đã được định nghĩa sẵn trong thư viện. Ví dụ, chuỗi dữ liệu có thể được in trên module LCD chỉ bằng cách gọi một lệnh lcd.print ();. Nếu bạn muốn in ra Hello Hello World ở hàng 1, bắt đầu từ cột 3; đầu tiên đặt con trỏ ở vị trí mong muốn bằng lệnh lcd.setCoder (1,3); và sau đó viết lệnh để in các ký tự dưới dạng lcd.print (“Hello World”); .Thư viện có hỗ trợ Arduino IDE (vì đây là thư viện chuẩn được cài đặt sẵn.

Thư viện LiquidCstall.h cung cấp các chức năng / lệnh cho hầu hết tất cả các thao tác như in chuỗi, đặt con trỏ, khởi tạo LCD, cuộn màn hình, cuộn tự động, xóa LCD, nhấp nháy v.v.

Code: 8302-022 Còn hàng

Code: 8302-024 Còn hàng

Code: 8302-027 Còn hàng

Code: 8302-023 Còn hàng

Hotline: 0979 466 469

Circuit

From the LCD’s datasheet, we have access to its pins’ interface description. To facilitate, a diagram of the LCD pins is shown in Figure 2. If you are using a different LCD display, these pins may not be the same, so be sure to check them in their corresponding datasheet before you connect them to your Arduino.

After the power pins (VSS and VDD), we can see that there is a pin (VE) used to adjust the contrast of the character pixels, which can be very helpful depending on the light conditions where we are using our LCD. Then, the register select pin (RS) is responsible for selecting the command and data registers. The command register stores the command instructions were given to the LCD (e.g. initialize it, clear the screen, scroll the screen, etc.), while the data register stores the data to be displayed in its ASCII value. Next, the read/write (RW) pin is the pin that either enables us to write the characters we want to the LCD (write mode) or to read the characters from it (read mode). After that, the enable (E) pin functions like a trigger, sending the information to the data pins of the LCD when a high to low pulse is given, followed by the 8 data pins (D0-D7). Finally, the backlight anode and backlight cathode pins are used to connect the background LED of the display.

With this in consideration, we can begin to connect our circuit, which schematic is represented in Figure 3.

First, connect the VSS and VDD to ground and 5 Volts, respectively. Then, in order to adjust the contrast of the character pixels, we can use a 10kΩ potentiometer. Connect the first terminal to 5 Volts, the middle terminal to the LCD’s VE pin and the third pin of the potentiometer to ground. Next, connect the digital pins 9 and 8 of the Arduino to the RS and E pins of the LCD, respectively. Due to the fact that, in this tutorial, we only want to write something to be displayed in the LCD, we can connect pin RW of the LCD directly to ground, since, when the signal in this pin is zero (low), it is set to write to the register, whereas, when the signal is one (high), the LCD is set to read from the register.

Since we are using the 4-bit mode, we can omit the data pins D0 to D3. Connect pins 5 to 2 of the Arduino to pins D4 to D7 of the LCD, respectively. Regarding the background LED of the LCD, in order to limit the current flowing through it, connect a terminal of the 220Ω resistor to the backlight anode pin of the LCD and the remaining terminal to 5 Volts. Finally, connect the backlight cathode pin to ground.

How To Connect An LCD Display To Arduino (Elegoo Mega 2560 Project Lesson 22 From Start To Finish)
How To Connect An LCD Display To Arduino (Elegoo Mega 2560 Project Lesson 22 From Start To Finish)

Liquid Crystal Display (LCD)

The LCD used in this tutorial is known as a 16×2 LCD display (Figure 1). Its name derives from the fact that the LCD has 16 columns and 2 lines (rows), meaning it can display 16 characters per line at once. However, this does not mean we can not show more than 32 characters if we need. We will see how to do it later in this tutorial.

One useful thing we always should have when working with any piece of hardware is its corresponding datasheet since it contains all the hardware’s important information. From this LCD’s datasheet, we can see that it can be connected considering two possible modes: 4-bit mode or 8-bit mode. In the 8-bit mode, the 8 data pins of the LCD are required so that the 8-bit ASCII value of the character can be sent simultaneously to the LCD. Regarding the 4-bit mode, only 4 data pins are required, but the 8 bits of the character have to be divided in two. Hence, the LCD receives only 4 bits at a time, starting with the high order bits and followed by the low order bits. After that, the character is displayed on the LCD.

Although the 8-bit mode seems straightforward, it requires four additional digital pins from our microcontroller to work, which can be a problem for people using other boards with less digital pins, like ESP8266 NodeMCU or similar. For that reason, in this tutorial, we are going to use the 4-bit mode, which, besides its inherent latency, does not present a significant difference in performance.

Giới thiệu LCD 16×2

Thông số kỹ thuật LCD 16×2

LCD 16×2 được sử dụng để hiển thị trạng thái hoặc các thông số.

  • LCD 16×2 có 16 chân trong đó 8 chân dữ liệu (D0 – D7) và 3 chân điều khiển (RS, RW, EN).
  • 5 chân còn lại dùng để cấp nguồn và đèn nền cho LCD 16×2.
  • Các chân điều khiển giúp ta dễ dàng cấu hình LCD ở chế độ lệnh hoặc chế độ dữ liệu.
  • Chúng còn giúp ta cấu hình ở chế độ đọc hoặc ghi.

LCD 16×2 có thể sử dụng ở chế độ 4 bit hoặc 8 bit tùy theo ứng dụng ta đang làm.

I2C LCD not showing Text | I2C LCD Errors Fixing || 16x2 LCD not displaying Text || 1602 LCD
I2C LCD not showing Text | I2C LCD Errors Fixing || 16×2 LCD not displaying Text || 1602 LCD

Keywords searched by users: arduino mega lcd 16×2

Easiest Way To Connect Lcd Screen To Arduino Mega! - Hackster.Io
Easiest Way To Connect Lcd Screen To Arduino Mega! – Hackster.Io
Easiest Way To Connect Lcd Screen To Arduino Mega! - Hackster.Io
Easiest Way To Connect Lcd Screen To Arduino Mega! – Hackster.Io
Arduino Mega Lcd Tutorial - Youtube
Arduino Mega Lcd Tutorial – Youtube
How To Connect An I2C Lcd Display To An Arduino Mega 2560 - Youtube
How To Connect An I2C Lcd Display To An Arduino Mega 2560 – Youtube
Mua Sunfounder Iic I2C Twi 1602 Serial Lcd Module Display Compatible With  Arduino R3 Mega 2560 16X2 Trên Amazon Mỹ Chính Hãng 2024 | Giaonhan247
Mua Sunfounder Iic I2C Twi 1602 Serial Lcd Module Display Compatible With Arduino R3 Mega 2560 16X2 Trên Amazon Mỹ Chính Hãng 2024 | Giaonhan247
Giao Tiếp Lcd 16X2 I2C Với Arduino Uno | Điện Tử Dat
Giao Tiếp Lcd 16X2 I2C Với Arduino Uno | Điện Tử Dat
Como Usar O Módulo I2C Com Arduino E Display Lcd 16X2 - Arduino E Cia - Loja
Como Usar O Módulo I2C Com Arduino E Display Lcd 16X2 – Arduino E Cia – Loja
Tổng Quan Lcd 16X2 Và Giao Tiếp I2C Lcd Sử Dụng Arduino | Arduino Kit
Tổng Quan Lcd 16X2 Và Giao Tiếp I2C Lcd Sử Dụng Arduino | Arduino Kit
Haris Prasetyo Site: Cara Mudah Menampilkan Karakter Pada Modul Serial Lcd  16X2 I2C Dengan Semua Jenis Arduino
Haris Prasetyo Site: Cara Mudah Menampilkan Karakter Pada Modul Serial Lcd 16X2 I2C Dengan Semua Jenis Arduino
Arduino Mega 2560 – Getting Started With Lcds
Arduino Mega 2560 – Getting Started With Lcds
Arduino Lcd 16X2 Interfacing With Arduino Uno | Arduino
Arduino Lcd 16X2 Interfacing With Arduino Uno | Arduino
In-Depth Tutorial To Interface 16X2 Character Lcd Module With Arduino
In-Depth Tutorial To Interface 16X2 Character Lcd Module With Arduino
How To Use 16X2 Lcd Display With Arduino Without I2C - Arduino Circuit
How To Use 16X2 Lcd Display With Arduino Without I2C – Arduino Circuit
Mega 2650 Dht11 Sensor & Lcd 16X2 Display - Programming Questions - Arduino  Forum
Mega 2650 Dht11 Sensor & Lcd 16X2 Display – Programming Questions – Arduino Forum

See more here: kientrucannam.vn

Leave a Reply

Your email address will not be published. Required fields are marked *