Arduino UNO Tutorial 10 - LCD

Arduino UNO Tutorial 10 - LCD

We are now going to add an LCD display to our Arduino. The Arduino IDE comes with an example LCD sketch which uses an Hitachi HD44780 compatible LCD. We will use a similar LCD (Pololu 16x2 Character LCD 773 or 772)

Just to be different, we will make a small enhancement and do away with the Potentiometer that is normally required to adjust the screen contrast. Instead we will use one of the Arduino PWM outputs, smoothed by a capacitor, to create a simple Digital to Analog output which will allow us to control the screen contrast digitally from within our program. Pin 9 is used as the PWM output and this connects to the Vo contrast pin on the LCD (pin 3). A 100uF capacitor is connected between the PWM output and ground.

The contrast pin on the LCD requires a fairly small voltage for ideal display conditions. The lower the voltage the higher the contrast and vice versa. A voltage of approx 0.5V to 1V is about right, but depends on the ambient temperature. We have set the PWM output initially to 50 (output is ON about 20% of the time) to give a value approximating 1V. You may want to increase or decrease this figure to get the correct contrast on your LCD screen.

Here are the pinouts from the LCD and the corresponding pin connection on the Arduino

LCD Pin Symbol Function Arduino Pin
1 Vss ground (0 V) ground (0 V)
2 Vdd power (4.5 – 5.5 V) +5V
3 Vo contrast adjustment 9
4 RS H/L register select signal 12
5 R/W H/L read/write signal ground (0 V)
6 E H/L enable signal 11
11 DB4 H/L data bus for 4-bit mode 5
12 DB5 H/L data bus for 4--bit mode 4
13 DB6 H/L data bus for 4-bit mode 3
14 DB7 H/L data bus for 4-bit mode 2


Below is a mockup of the wiring connections and the output displayed on the screen

And here we have a more detailed view

And here is the Arduino Sketch. The PWM output to control the contrast is done in the setup routine, however, if you wanted to be able to control the contrast manually, then the addition of two push buttons and a bit more coding would allow you to increase and decrease the contrast in simple steps within the program.

 LiquidCrystal Library - Hobbytronics
 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all 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.
 This sketch prints "Hobbytronics" to the LCD
 and shows the time.
 This sketch is based on the Arduino sample sketch at
 but with modifications  to the LCD contrast to make it 
 adjustable via software
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD R/W pin to Ground 
 * LCD VO pin (pin 3) to PWM pin 9
 * 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


// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);  
  analogWrite(9, 50);   
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("  HobbyTronics");

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:

Note on 16x4 displays. There is a well documented bug in the Arduino LCD library with regard to all 16x4 displays.  The problem is that the 16x4 has different starting addresses for lines 3 and 4 than the 20x4 for which the library is written.This means that the lcd.setCursor command doesn't work correctly for lines 3 and 4.

There is fortunately a simple fix. Instead of lcd.setCursor(0,3) to set position at the beginning of line 3 you should use lcd.setCursor(-4,3). The same applies to line 4.

Tutorial 9 - Power

Tutorial 11 - Hall Effect Switch

Shopping Cart
0 items

Free Delivery Orders over £50

UK - £2.40 orders under £50
Airmail from £2.95 (+VAT)
More Details...

 Check out our videos