Arduino shields allow you to quickly upgrade your projects with a wide array of useful features and tools that you can plug directly into your board. To show you how easy it is, we’ll walk through how to add a programmable LCD display to your Arduino project so you can display text, and provide input with a series of buttons.
We’ve talked a bit about Arduino shields before, but if you’re not familiar, a shield is an add-on for an Arduino project that can plug directly into your board to give it new features. Some are stackable, so you can add multiple modules to a single project. They often come with their own library of software that you can import into your sketches to control them just as easily as you do with other components you connect.
For this guide, we’ll be using an LCD display shield. There are several variations of this kind of shield out there, but the one we’ll be using is the 1602 keypad shield, which can display up to 16 characters across 2 rows (hence 1602) for up to 32 characters at a time. It also comes with six buttons: four directional buttons (left, right, up, and down) and a select and reset button.
This shield has 28 pins that align with the pins on the Arduino Uno. Shields are generally designed to snap directly onto their corresponding boards, so if you have a shield that doesn’t match the pins on your board, you might need a different shield. In this case, if you have an Arduino Uno and the correct shield, you can insert the shield directly onto the board itself (we’ll talk more about this in the wiring section below).
However, the LCD display doesn’t need to use every pin on the board. This is where some pass-through pins come in handy. You can connect wires to the LCD display board where there are open contacts, and this will connect to the Arduino. This is handy because it means you don’t lose any open pins just because you’re using a shield.
The final piece that makes this work is the LiquidCrystal library. This library provides simple commands to display text, scroll text, control a cursor position, and more. As long as your LCD display shield is compatible with this library (and most popular shields are), then you can include this library and control your display with very simple commands.
Since shields plug directly into Arduino boards, you won’t need a ton for this project. That’s what makes Arduino shields great; they can dramatically simplify your projects. However, you still need a couple of things before we get started:
Arduino Uno |
|
|||
LCD display shield | ![]() |
|||
Arduino IDE |
|
You’ll also need a USB cable!
We’ll go over how to plug in the shield in the wiring section below, but first, let’s talk about the software.
The Arduino IDE comes with a few sketches in the example book under File > Examples > Liquid Crystal. For our purposes, we’ll use the HelloWorld sketch. You can load this up in your IDE, but we’ll include the full code below:
*/
// 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);
}
Now, let’s go over a few of the things that this code does.
#include <LiquidCrystal.h>
This line tells the sketch to include the LiquidCrystal library. This must be included at the top of your sketch if you want to use the commands in this library.
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Next, these two lines will initialize variables for the six pins that are needed to control the LCD display in 4-bit mode: rs, enable, d4, d5, d6, and d7. The Arduino documentation has more information on the pins required to control the LCD display in 8-bit mode, but we won’t need that here.
The second line will assign those pins (via the variables you just created) to a new type of variable called LiquidCrystal, in this case named lcd. This lets you address the LCD display as a whole entity, rather than having to control each individual pin. This lets you use the other commands in the LiquidCrystal library with simple lines of code.
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!”);
}
In the setup() section, there are only two commands: the first, lcd.begin()—which calls the begin() command on the lcd variable we created earlier—initializes the LCD display. The second prints the phrase “hello, world!”
This print() command is different from the one you’ve used in the past. While the other, Serial.print(), prints data to the serial port, this one is part of the LiquidCrystal library and will print text to an LCD display. It can be called on any LCD object you create using the above method.
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);
}
In this section, two more lines of code will be used to change the position of the cursor and print the number of milliseconds it’s been since the device was reset. Once again, you can see how easy each task is, taking only a single line of code from the library. The setCursor() function puts the cursor where you designate. Since both rows and columns start counting at zero, the coordinates (0,1) refer to the first column on the second line. The display once again uses print() to display the number of milliseconds since the device reset.
If you’re only using the shield, you won’t need any special wiring at all for this project. Simply align the pins on the bottom of your LCD shield with the pins on your Arduino Uno board. It’s recommended to start with the pins that align with A0 and RX0 on the far end of the Arduino board.
There are natural gaps in the pin groups (for example, between pins 7 and 8, and between A0 and vin) that should help you line up the pins correctly. Once they’re aligned, gently but firmly press the shield until all the pins are snug on the board.
On the LCD shield itself, you’ll see many pins have empty contacts next to the solder points where the pins connect to the shield. If you want to add additional wires or components, this is where you can do so.