How to control the Brightness of a LCDKeypad?

Component used:

  • Arduino UNO R3 Plus
  • 16x2 LCD Display & Keypad Shield

In this project I’ll show you how to control the brightness of a 16x2 LCD Display wild using only an Arduino UNO R3 Plus and a 16x2 LCD Display & Keypad Shield.

Here is my code:

#include <LiquidCrystal.h>
#include <LCDKeypad.h>

LCDKeypad lcd;

//byte//
byte b[8] = { //goes with lcd.createChar(1,b)
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};
////////

int BL = 120; //BackLight LED_LCD initial positon 50% on 120/240 of 255
int MDim = 3; //Min-Max Dim
int Dim = 3;

void setup() {
  lcd.begin(16,2);
  lcd.print("-  Brightness  +"); 
  lcd.createChar(1,b);
    
}

void loop() {

  btnRead();
  analogWrite(10, BL); //BackLight of the LCD
  lcd.setCursor(0,1);
  lcd.print("                "); 

  
int u = BL/16; //BL/16 => 240/16 = 15
int v = 1;  //goes with lcd.createChar(1,b)
      lcd.setCursor(u,1);
      lcd.write(v);
   
    
    
}
void btnRead() {
    if (BL > 0) {
      lcd.display();
    }
    if (BL == 0) {
      lcd.noDisplay();
    }
  int button = lcd.button();

  switch (button) {
    
// UP KEY //
    
    case KEYPAD_UP:
    if (BL >= 240) { //set BL Min to Max
      MDim = 0;
    }
    if (BL <= 240) {
      MDim = Dim;
    }
      BL = BL + MDim;
      break;
      
// DOWN KEY //
    
    case KEYPAD_DOWN:
    if (BL == 0) { //set BL Max to Min
      MDim = 0;
    }
    if (BL > 0){
      MDim = Dim;
    }
      BL = BL - MDim;
      break;

  }
delay(30);
}