Find out how to wire an LCD to an Arduino, and how to use the LiquidCrystal library through a set of useful examples. Show
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. Output of the sketch on a 16x2 LCDThe 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:
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
CircuitNote 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:
Additionally, wire a 10k potentiometer to +5V and GND, with it's wiper (output) to LCD screens VO pin (pin3). The circuit (made using Fritzing).SchematicThe schematic (made using Fritzing).Hello World ExampleThis example sketch prints
to the LCD and shows the time in seconds since the Arduino was reset.
Autoscroll ExampleThis example sketch shows how to use the
and
methods to move all the text on the display left or right.
This sketch prints the characters
to
with autoscroll off, then moves the cursor to the bottom right, turns autoscroll on, and prints them again.
Blink ExampleThis example sketch shows how to use the
and
methods to blink a block-style cursor.
CursorThis example sketch shows how to use the
and
methods to control an underscore-style cursor.
Display ExampleThis example sketch shows how to use the
and
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.
Scroll ExampleThis example sketch shows how to use the
and
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.
Serial to Display ExampleThis 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.
Set Cursor ExampleThis example sketch shows how to use the
method to reposition the cursor. To move the cursor, just call
with a row and column position. For example, for a 2x16 display:
Here is the full example:
Text Direction ExampleThis example sketch shows how to use the
and
methods. These methods control which way text flows from the cursor.
This sketch prints
through
right to left, then
through
left to right, then
through
right to left again.
Custom CharacterThis example demonstrates how to add custom characters on an LCD display. Note that this example requires an additional potentiometer:
This potentiometer controls the
variable. |