LCD Keypad Shield (B4R)

Experiments & prototyping with the Arduino.
rwblinn
Site Admin
Posts: 216
Joined: 07 Oct 2017, 12:16

LCD Keypad Shield (B4R)

Postby rwblinn » 06 Feb 2018, 09:51

Objectives
Test the use of an LCD Keypad Shield connected to an Arduino UNO.
The LCD Keypad Shield is 16x2 HD44780 compatible LCD with white character & blue back light and input buttons.

Parts Needed
  • 1x Arduino UNO
  • 1x LCD Keypad Shield - used a shield from DFRobot
Wiring
The LCD Keypad Shield is placed on top of the Arduino UNO.
As an indicator, to ensure the shield is rightly placed, check the LCD Keypad Shield 5V pin is matching the Arduino UNO 5V pin slot.

Solution Arduino Program (B4R)
The code used to test the LCD Keypad Shield.
The library rLiquidCrystal is required (included in the B4R Core Libraries).
Notes
  • Initialize the LCD uses the data pins D4-D7. If using another board ensure to set the correct pin mapping (see example below).
  • Pin A0 is used to determine which key is pressed. If the debounce response is too hight, change the delay in Sub KeyPadHandler.
  • Pin A0 AnalogRead return value depends on the board using. The values worked for the Arduino UNO tested. For other boards, values might differ (see example below).

Code: Select all

Sub Process_Globals
    Public Serial1 As Serial
   'LCD
   Private LCD As LiquidCrystal
   Private LCDKeyPadPin As Pin
   Private LCDKeyPressed As Int = 0
   Private LCDAnalogValue As Int = 0
   Private LCDKeyRight As Int =  0
   Private LCDKeyUp As Int = 1
   Private LCDKeyDown As Int =  2
   Private LCDKeyLeft As Int = 3
   Private LCDKeySelect As Int = 4
   Private LCDKeyNone As Int =  5
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
   Log("AppStart v20180206")
   'Init LCD Keypad Pin A0
   LCDKeyPadPin.Initialize(LCDKeyPadPin.A0, LCDKeyPadPin.MODE_INPUT_PULLUP)
   'Init LCD - RegisterSelect (RS) D8, ReadWrite (RW, not used = set to 255), Enable (E) D9, 4 Data Pins D4,D5,D6,D7
   LCD.Initialize(8, 255, 9, Array As Byte(4, 5, 6, 7))
   'Columns, Rows
   LCD.Begin(16,2)
   'LCD set back light on (default off)
   LCD.DisplayOn = True
   LCD.SetCursor(0,0)
   LCD.Write("HowTo LCD Keypad")
   LCD.SetCursor(0,1)
   LCD.Write("20180206")
   'Add the looper which handles the LCD Keypad buttons pressed
   AddLooper("Looper1")
End Sub

Private Sub Looper1
   LCDKeyPressed = KeyPadHandler
   Select LCDKeyPressed
      Case LCDKeyUp
         LCD.Clear
         LCD.SetCursor(0,0)
         LCD.Write(LCDAnalogValue)
         LCD.SetCursor(0,1)
         LCD.Write("UP")
         Exit
      Case LCDKeyDown
         LCD.Clear
         LCD.SetCursor(0,0)
         LCD.Write(LCDAnalogValue)
         LCD.SetCursor(0,1)
         LCD.Write("DOWN")
         Exit
      Case LCDKeyLeft
         LCD.Clear
         LCD.SetCursor(0,0)
         LCD.Write(LCDAnalogValue)
         LCD.SetCursor(0,1)
         LCD.Write("LEFT")
         Exit
      Case LCDKeyRight
         LCD.Clear
         LCD.SetCursor(0,0)
         LCD.Write(LCDAnalogValue)
         LCD.SetCursor(0,1)
         LCD.Write("RIGHT")
         Exit
      Case LCDKeySelect
         LCD.Clear
         LCD.SetCursor(0,0)
         LCD.Write(LCDAnalogValue)
         LCD.SetCursor(0,1)
         LCD.Write("SELECT")
         Exit
   End Select
End Sub

Private Sub KeyPadHandler As Int
   LCDAnalogValue = LCDKeyPadPin.AnalogRead
   If LCDAnalogValue > 1000 Then Return LCDKeyNone
   If LCDAnalogValue < 50 Then Return LCDKeyRight
   If LCDAnalogValue < 195 Then Return LCDKeyUp
   If LCDAnalogValue < 380 Then Return LCDKeyDown
   If LCDAnalogValue < 555 Then Return LCDKeyLeft
   If LCDAnalogValue < 790 Then Return LCDKeySelect
   Return LCDKeyNone
End Sub


ESP8266 WeMOS D1 R1 (retired) Test
Tested the LCD Keypad Shield with a ESP8266 WeMOS D1 R1 (retired). Settings used:

Code: Select all

'Pin mapping D8=GPIO0, D9=GPIO2, D4=GPIO4,D5=GPIO14,D6=GPIO12,D7=GPIO13) resulting in 0,255,2,4,14,12,13
LCD.Initialize(0,255,2, Array As Byte(4, 14, 12, 13))
...
Private Sub KeyPadHandler As Int
   Delay(100)
   LCDAnalogValue = LCDKeyPadPin.AnalogRead
   If LCDAnalogValue > 1023 Then Return LCDKeyNone
   If LCDAnalogValue < 60 Then Return LCDKeyRight
   If LCDAnalogValue < 250 Then Return LCDKeyUp
   If LCDAnalogValue < 500 Then Return LCDKeyDown
   If LCDAnalogValue < 790 Then Return LCDKeyLeft
   If LCDAnalogValue < 1023 Then Return LCDKeySelect
   Return LCDKeyNone
End Sub


Download
No download as code is shared previous

Software Versions
B4R v2.20

ToDo
  • Experiment is completed.

Version
20180206

Return to “Arduino”

Who is online

Users browsing this forum: No registered users and 1 guest