Tag Archives: Liquid Crystal Displays

The 16×02 LCD with Arduino

LCD 16x02 with Arduino

It is often useful to display numerical or textual information produced by an Arduino board (a measurement made by a sensor, for example). As long as the Arduino is plugged into a computer, we can use the serial monitor built into the IDE to display this information. But if we want to develop a stand-alone device that can work away from the computer, a liquid crystal display (or LCD) is a practical and relatively simple solution. In this tutorial, we will learn how to use the 16×02 LCD with the Arduino board.

Equipment

  • Arduino UNO.
  • USB cable to connect the Arduino to the computer.
  • 16×2 LCD screen.
  • 220 Ohms resistor.
  • 1k Ohms potentiometer.
  • Jumpers.

What is a 16×2 LCD ?

Liquid crystal displays (LCD) use the light modulating property of liquid crystals. Liquid crystal displays are composed of two layers of polarizers, with perpendicular polarization directions,. These displays contain two glass plates between which the liquid crystals are placed. On the glass plates is an array of electrodes for each pixel. A voltage applied between the electrodes of a pixel causes a change in orientation of the molecules and therefore the transparency of the pixel which can then let, or not, pass the light of the backlight.

Wiring and explanations

Pins 1 and 2 are used to power the display:

  • Pin1 of the display — GND of the Arduino board.
  • Pin2 of the display — 5 V of the Arduino board.

Pin3 is used to adjust the contrast of the display: it is connected to a 10 kΩ potentiometer which allows its voltage to be adjusted to a value between 0 and 5 V.

  • Pin3 of the display — Cursor of the 1k Ohms potentiometer whose two other pins are connected respectively to GND and to 5 V.
  • Pin4 “RS” of the display — Pin12 of the Arduino
  • Pin5 “R/W” of the display — GND of the Arduino
  • Pin6 “Enable” of the display — Pin11 of the Arduino

Pins 7, 8, 9 and 10 of the display are not connected, because we will use the display in 4-bit mode rather than in 8-bit mode (which saves the inputs/outputs of the Arduino ).

  • Pin11 “D4” of the display — Pin5 of the Arduino.
  • Pin12 “D5” of the display — Pin4 of the Arduino.
  • Pin13 “D6” of the display — Pin3 of the Arduino.
  • Pin14 “D7” of the display — Pin2 of the Arduino.

Pins 15 and 16 are used to power the backlight. On some display models, the backlight is not essential (except to read the display in the dark), while for other models the display is unreadable if the backlight is not activated.

  • Pin15 from display — 5 V from Arduino through a 220 Ohms resistor.
  • Pin16 of the display — GND of the Arduino.

The Liquid Crystal Library

To display the information on the display, we will use the “LiquidCrystal” library, which was specially designed for this purpose. No need to install it, since it comes with the IDE.

To quickly check that your display is indeed functional, download the “Hello World” example to your Arduino. You should see the message “Hello, world!” appear on the first line of the display, while on the second line a counter is incremented every second.
If nothing is displayed, don’t panic! Turn the potentiometer knob that controls the contrast. Or you might need recheck your wiring.

To display a text, we use the “print” command. The text always starts at the cursor position. When you write something, the cursor automatically moves to the position immediately following the displayed text.

The position of the cursor is set using the “setCursor(column, row)” command. It is important to note that the first line at the top has the number 0, and the first column on the left has the number 0. The instruction “lcd.setCursor(0,0)” will therefore position the cursor at the top left.

If you want the user to enter text that will appear on the display, it may be useful to show a blinking symbol that indicates the cursor position, using the “blink” command.

Example code

Once your module is correctly connected, you can modify the following code to obtain the desired functionality. In the following example, we are creating a countdown timer.

As mentioned before, to manage the 16×2 LCD screen in the program, you can use the LiquidCrystal.h library.

  • LiquidCrystal lcd(rs, en, d4, d5, d6, d7): set 4bit i2c communication
  • lcd. begin(16, 2): displays text on both lines.
  • lcd.print(): display a character string in ASCII.
  • lcd.write(): display data, one byte at a time.
  • lcd.setCursor(x, y): set cursor (column x: 0-16, row y:0-2).
  • lcd.clear(): clear what is displayed on the screen
/*
The Liquid Cristal counter
www.bekyelectronics.com/
*/

// Library
#include <LiquidCrystal.h>

// Library initialization
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

long timeInit = 0; //min
int timeHour = 1;
int timeMin = 26;
int timeSec = 35;

void setup() {
 // set up the LCD's number of columns and rows: 
 lcd.begin(16, 2);
 analogWrite(8, 15);

 // Display a message
 lcd.print("Next alarm in: ");
 timeInit=timeToCount(timeHour, timeMin, timeSec);
}

void loop() {
 // Digital time display
 countTo(timeInit);
 
 // places the cursor at the 1st character of the 2nd line
 lcd.setCursor(0, 1);

 lcd.print(timeHour);
 lcd.print(" h");  
 lcd.print(timeMin);  
 lcd.print(" m");
 lcd.print(timeSec);
 lcd.print(" s");
 
 // Wait 1 second
 delay(1000); 

 timeInit=timeInit - 1;
}

void countTo(long cntr){
 timeHour=(cntr / 3600);
 timeMin= (cntr % 3600) / 60;
 timeSec = ((cntr % 3600) % 60);
}

int timeToCount(int h,int m,int s){
 return h * 3600 + m * 60 + s;
}

Congratulations, you just made your first clock.

If you want to see more of our tutorials, make sure to visit this link.

You may be interested in this article which shows how to use IMU MPU6050 with the Raspberry Pi Pico.