Arduino: LCD Displays (Part 1)
Want to use an Arduino to control an LCD display? You've come to the right place! We'll use an Arduino Uno and the LiquidCrystal library to display text.
Join the DZone community and get the full member experience.
Join For FreeWe have written plenty of Arduino tutorials, but none of them have been about displays. So we decided to do something about that. This is the first of a planned three-part tutorial series on how to use various displays with the Arduino.
In this first part, we’re going to show you how to use character displays based on the Hitachi HD44780 LCD controller. This is a much-used standard on these kinds of displays and, together with Arduino’s LiquidCrystal library, they become super easy to use. Throughout this post, we’ll use a JHD 162A LCD display and an Arduino Uno.
Wiring
To get the display up and running you can use a breadboard to make the wiring a bit easier. You’ll need a couple of resistors as well as two 5V sources, so the breadboard comes in handy.
Parallel Interface (With or Without Serial Backpack)
These screens have a parallel interface, which takes up a lot of pins on the Arduino. There are screens with a so-called serial backpack which makes it able to use UART to communicate with the display, resulting in far fewer pins being used. We will, however, not look at that in this post.
Different Setups
For the simplest HD44780 parallel displays, you will need to hook up 12 or 16 pins on the display. On the Arduino, you need to use 6, 7, 10, or 11 GPIO-pins.
The first choice you should make is whether to use 4 or 8 pins for the data. This is a trade-off between pin usage and display refresh rate:
- 4 pins
- Pro: You save 4 GPIO-pins for other stuff.
- Con: You need to use two cycles to write to (and read from) the display. This is done automatically, so it doesn’t raise the complexity, but it takes a bit more time.
- 8 pins
- Pro: Faster refresh rate.
- Con: Lots of occupied GPIO-pins.
The other choice you need to make is whether or not to be able to read from the display registers. This ability takes up one more GPIO-pin. We’re unsure if the LiquidCrystal library uses this pin for anything. So if you, like us, aren’t particularly interested in using this pin, you can just hook it up to GND and save a GPIO-pin.
Analog Pins
The display has two pins that react on analog values: contrast and background LED brightness in addition to a 5V supply pin and two GND pins.
Contrast
For the contrast pin, you should use a voltage divider to set a specific voltage on the pin.
Voltage divider schematic (source: Wikipedia).
In our case, Vin is 5 V and Vout is the voltage on the contrast pin on the display. The ratio between Z1 and Z2 defines Vout (i.e. the contrast) according to the following equation:
For our JHD 162A display, we get the maximum contrast at Vout around 0.6 V. At around 1.4V, the contrast gets so low that the text disappears completely, so that’s the range you have to work with. If you replace Z1 with a potentiometer, you can adjust the contrast on the fly.
LED Brightness
This is a bit more straightforward. We just need to limit the current flowing through the LED. Our JHD 162A has a 100Ω resistor on board, so we can apply 5V directly. However, check that your display has an LED resistor before applying voltage. If you want less brightness, you can add a resistor or a potentiometer in series between 5V and the LED pin on the display. We’re running around 400Ω total and it looks pretty good.
Digital Pins
As mentioned earlier, you need to hook up either 4 or 8 data pins. In addition to this, you need to connect an Enable pin, an RS pin, and an optional R/W pin. The datasheet gives you more info on each of these pins.
Schematics
In the illustration below, you can see how we’ve hooked up the JHD 162A to our Arduino Uno. This setup will continue over to the coding example further down.
In this setup, we use 4 data pins and a grounded R/W pin.
Which GPIO pin you use to what (upper row on the Arduino) doesn’t really matter as long as you input the correct pin numbers in the firmware.
Firmware
LiquidCrystal
The LiquidCrystal library is included in the Arduino IDE and makes everything incredibly easy. The library is for the user basically a set of functions that does all the hard work for you.
Instead of going through all the functions, we’re just going to show you the code we used in the image at the top of this post. The documentation on the Arduino website is more than adequate, with plenty of examples and overview of all the functions.
Our Code
The code we used to display “Norwegian Creations” as in the top image is super easy and doesn’t require many lines of code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(13, 12, 5, 4, 3, 2);
void setup() {
lcd.begin(16,2);
lcd.print("Norwegian");
lcd.setCursor(0, 1);
lcd.print("Creations");
}
void loop() {
}
And that’s it!
Be sure to carefully study the LiquidCrystal() constructor documentation carefully so that you get the parameter list in the right order.
To Summarize…
As you can see, hooking up an HD44780-based character display and making it show some text is really easy with an Arduino and the LiquidCrystal library.
If nothing seems to work, double check your contrast voltage. Depending on your display, you might need to have the LED backlight working to get good contrast.
The LiquidCrystal library makes coding easy. Use the documentation on Arduino’s website.
In the next part of this blog post series, we’ll look at how we can use displays with a serial backpack to limit the pin usage.
Published at DZone with permission of Mads Aasvik, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
-
RAML vs. OAS: Which Is the Best API Specification for Your Project?
-
Send Email Using Spring Boot (SMTP Integration)
-
What to Pay Attention to as Automation Upends the Developer Experience
Comments