Site icon AranaCorp

Using a 16×2 LCD Shield with Arduino

0
(0)

One of the most widely used information display elements in the Arduino world is the 16×2 LCD (Liquid Crystal Display). When manufacturing an electronic system, it can be interesting to have it give us some information about its status without having to connect it to a computer or to another system such as a smartphone. The 16×02 LCD screen is supplied with a large number of Arduino kits and is very sufficient for a large number of applications.
The 16×2 LCD screen can be found mounted on a shield with the bonus of a few buttons to create simple programmable interfaces to display values and control your Arduino project. All this while making the installation much easier.

Hardware

Principle of operation

Liquid crystal displays make use of the light modulation property of liquid crystals. Liquid crystal displays consist of two layers of polarizers, with perpendicular polarization directions, sandwiching two glass plates between which the liquid crystals are placed. On the glass plates is a matrix of electrodes for each pixel. A voltage applied between the electrodes of a pixel causes a change in the orientation of the molecules and thus the transparency of the pixel, which may or may not allow the light of the backlight to pass through.

Schematic

The LCD shield is compatible with Arduino UNO and Mega cards and is placed directly on the card.
In summary, the pins used are:

The RST button is directly connected to the reset button of the Arduino board.

Code

Once your module is correctly connected, you can modify the following code to obtain the desired functionality. In the following example, we define a function that will read the value of the buttons and execute an action according to the button pressed.
For each button pressed, the name and value of the button are displayed. Your shield may be different depending on the supplier and version. If this is the case, this code will allow you to easily modify the button detection values.

To manage the 16×2 LCD screen in the programme, the library used is LiquidCrystal.h whose functions are as follows:

//Libraries
#include <LiquidCrystal.h>//https://arduino.cc/en/Reference/LiquidCrystal

//Parameters
const int btnsPin  = A0;
int btnsVal  = 0;

//Objects
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);;

//Enums
enum {
  BUTTON_NONE,
  BUTTON_UP,
  BUTTON_DOWN,
  BUTTON_LEFT,
  BUTTON_RIGHT,
  BUTTON_SELECT,
};

void setup() {
  //Init Serial USB
  Serial.begin(9600);
  Serial.println(F("Initialize System"));
  //Init LCD16x2 Shield
  lcd.begin(16, 2);
  lcd.print(F("Hello World "));
  delay(2000);
  lcd.clear();
  lcd.print(F("Button pressed: "));
}

void loop() {
  btnListener(getBtnPressed());
}

void btnListener(byte btnStatus) { /* function btnListener */
  //// Get button value when pressed
  lcd.setCursor(0, 1);
  switch (btnStatus) {
    case BUTTON_UP:
      lcd.print(F("UP     "));
      lcd.print(btnsVal);
      break;

    case BUTTON_DOWN:
      lcd.print(F("DOWN   "));
      lcd.print(btnsVal);
      lcd.print("   ");
      break;

    case BUTTON_LEFT:
      lcd.print(F("LEFT   "));
      lcd.print(btnsVal);
      lcd.print("   ");
      break;

    case BUTTON_RIGHT:
      lcd.print(F("RIGHT  "));
      lcd.print(btnsVal);
      lcd.print("   ");
      break;

    case BUTTON_SELECT:
      lcd.print(F("SELECT "));
      lcd.print(btnsVal);
      lcd.print("   ");
      break;

    default://case BUTTON_NONE:
      //lcd.print(F("       "));
      break;
  }
  delay(100);
}

byte getBtnPressed() { /* function getBtnPressed */
  //// Get button value when pressed
  btnsVal = analogRead(btnsPin);
  if (btnsVal < 50)
    return BUTTON_RIGHT;
  else if (btnsVal < 250)
    return BUTTON_UP;
  else if (btnsVal < 350)
    return BUTTON_DOWN;
  else if (btnsVal < 450)
    return BUTTON_LEFT;
  else if (btnsVal < 650)
    return BUTTON_SELECT;
  else
    return BUTTON_NONE;
}

Applications

Sources

Find other examples and tutorials in our Automatic code generator
Code Architect

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Exit mobile version