DEV Community

Cover image for LCD 1602 Keypad Shield: How do you feel?
a.infosecflavour
a.infosecflavour

Posted on

LCD 1602 Keypad Shield: How do you feel?

This simple project is meant to display the functionality of a Liquid Crystal Display 1602 Keypad Shield.
How do you feel? mimics a self- assessment tool- user can choose from 5 emotions according to the pressed button, represented by different characters:
happy (button: UP; char: smiley)
thrilled (button: RIGHT; char: heart)
sad (button: DOWN; char: sad)
surprised (button: LEFT; char: surprised)
playful (button: SELECT; char: playful).

The LCD is connected to the Arduino compatible UNO R3 board without using wires. A USB A-B cable makes the connection between the board and a source of electricity (this case, own laptop).
Here is the correspondence between the command pins:

pins

"d" stands for data . On the LCD, it's displayed with "D" (from D0 to D7).

The correspondence pins of the board are digital .
There is also a backlight control pin (not identifiable on the LCD), corresponding to digital pin 10. The backlight transistor is under the LCD module shield.

Extra info:
There are also analogue pins, from A0 to A5. The only pin used is A0: it is used by the buttons (except for RST).

  • RS stands for Register Select.

  • RS sends signals to I nstruction R egister (IR) and D ata R egister (DR).

  • IR stores the instruction codes (initialize display, clear display, cursor shift etc.), the address information for display data RAM, and the character generator RAM
    DR processes the instruction.

  • If RS= 0, instruction register selected. If RS=1, data register selected.

EN stands for Enable.

  • Starts data read/ write.
#include <LiquidCrystal.h> // is used to include outside libraries in your sketch

// LCD pin to Arduino
const int RS = 8; 
const int EN = 9; 
const int d4 = 4; 
const int d5 = 5; 
const int d6 = 6; 
const int d7 = 7; 


// create a variable of type LiquidCrystal
LiquidCrystal lcd (RS, EN, d4, d5, d6, d7); // LiquidCrystal lcd(8, 9, 4, 5, 6, 7); also works, without declaring the variables as above. The variant used in the code does not work without declaration.

void setup() { // put your setup code here, to run once:

  lcd.begin(16, 2); // initialize the interface on the LCD screen and specify the dimensions in width and height. 16 is the number of columns, 2 is the number of rows

  lcd.setCursor(0,0); // set the location at which subsequent text written to the LCD will be displayed; 0 is the starting point for column and row; in this case, the cursor is set in the first column and first row

  lcd.print("How do you feel?"); // print the text to the display. in this case, the text is on the first column and first row, as it was set earlier in lcd.setCursor(0,0);

  lcd.setCursor(0,1); // set the location at which subsequent text written to the LCD will be displayed; 0 is the starting point for column and row; in this case, the cursor is set in the first column and second row

  lcd.print("I feel:"); // print the text to the display. in this case, the text is on the first column and second row, as it was set earlier in lcd.setCursor(0,1);



 // make custom characters
 byte thrilled[8] = { // make a heart character of 8 bits
  0b00000,
  0b01010,
  0b11111,
  0b11111,
  0b11111,
  0b01110,
  0b00100,
  0b00000
};


byte happy[8] = { // make a smiley chararacter of 8 bits
  0b00000,
  0b00000,
  0b01010,
  0b00000,
  0b00000,
  0b10001,
  0b01110,
  0b00000
};

byte sad[8] = { // make a sad character of 8 bits
    0b00000,
    0b00000,
    0b01010,
    0b00000,
    0b00000,
    0b01110,
    0b10001,
    0b00000
};

byte surprised[8] = { // make a surprised character of 8 bits
    0b11011,
    0b00000,
    0b01010,
    0b00000,
    0b11111,
    0b10001,
    0b11111,
    0b00000
};

byte playful[8] = { // make a playful character of 8 bits
    0b00000,
    0b00000,
    0b01010,
    0b00000,
    0b00000,
    0b11111,
    0b10001,
    0b01110
};

  // create a new character
  lcd.createChar(0, thrilled); // create character 0 with the correspondent character data, which is named thrilled
  // create a new character
  lcd.createChar(1, happy); // create character 1 with the correspondent character data, which is named happy
  // create a new character
  lcd.createChar(2, sad); // create character 2 with the correspondent character data, which is named sad
  // create a new character
  lcd.createChar(3, surprised); // create character 3 with the correspondent character data, which is named surprised
  // create a new character
  lcd.createChar(4, playful); // create character 4 with the correspondent character data, which is named playful
}


void loop() { // put your main code here, to run repeatedly:
  int x; // declare x as an integer variable
  x = analogRead (0); // analog reading of pin A0
  lcd.setCursor(10,1); // set the cursor at the 11th column on the second row; take into account that 0 is the starting point
  if (x < 50) { // right button is pressed
    lcd.write(byte(0)); // right = thrilled
  }
  else if (x < 195) { // up button is pressed
    lcd.write (byte(1)); // up = happy
  }
  else if (x < 380){ // down button is pressed
    lcd.write (byte(2)); // down =  sad
  }
  else if (x < 590){ // left button is pressed
    lcd.write (byte(3)); // left = surprised
  }
  else if (x < 790){ // select button is pressed
    lcd.write (byte(4)); // select = playful
  }
}
Enter fullscreen mode Exit fullscreen mode

Credits:

Top comments (0)