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
Below is a mockup of the wiring connections and the output displayed on the screen
/*
LiquidCrystal Library Hobbytronics
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
*/
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:
lcd.print(millis()/1000);
}
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.