Turn on lcd backlight by button, Arduino

/*
 
LiquidCrystalDisplay_Button
 
Turn on the backlight of a display for one minute by pressing the button.
 
Created 28 Dec 2012
by TheUzo007.wordpress.com
 
*/
 
#include <LiquidCrystal.h>
 
// Set pins.
#define BUTTON_PIN A5    // The number of the push-button pin.
#define LCD_LIGHT_PIN A4 // The number of the pin where anode of the display backlight is.
 
#define LCD_LIGHT_ON_TIME 60000 // How long (in milliseconds) should lcd light stay on?
 
unsigned int currentLcdLightOnTime = 0;
// For calculating the lcd light on time.
unsigned long lcdLightOn_StartMillis;
 
boolean isLcdLightOn;
 
// For checking push-button state.
int buttonState = 0;
 
// Initialize the LiquidCrystal library with the numbers of the interface pins.
// LiquidCrystal(rs, e, d4, d5, d6, d7)
LiquidCrystal lcd(8, 9, 5, 6, 7, 3);
 
void setup(){
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  // Set the lcd number of columns and rows.
  lcd.begin(16, 2);
 
  // Print the message to the lcd.
  lcd.setCursor(0, 0); // First row.
  lcd.print("Test lcd light.");
  lcd.setCursor(3, 1); // Second row.
  lcd.print("TheUzo007");
  lcd.display();
 
  // Set the push-button pin as an input.
  pinMode(BUTTON_PIN, INPUT);
  // Set the lcd display backlight anode pin as an output.
  pinMode(LCD_LIGHT_PIN, OUTPUT);
  // Set the lcd display backlight anode pin to low - lcd light off.
  digitalWrite(LCD_LIGHT_PIN, LOW);
  isLcdLightOn = false;
}
 
void loop(){
  // Check the state of the push-button.
  buttonState = digitalRead(BUTTON_PIN);
 
  if (buttonState == HIGH){
    // Button pressed.
    Serial.println("Button pressed - HIGH");
 
    lcdLightOn_StartMillis = millis();
    currentLcdLightOnTime = 0;
    isLcdLightOn = true;
    digitalWrite(LCD_LIGHT_PIN, HIGH);
  }
  else{
    // Button not pressed.
    //Serial.println("Button not pressed - LOW");
 
    if(isLcdLightOn){
      currentLcdLightOnTime = millis() - lcdLightOn_StartMillis;
      if(currentLcdLightOnTime > LCD_LIGHT_ON_TIME){
        isLcdLightOn = false;
        digitalWrite(LCD_LIGHT_PIN, LOW);
      }
    }
  }
 lcd.setCursor(0,0);
 lcd.print("Lcd light on time: ");
 lcd.print(currentLcdLightOnTime);
}

hi
i have problems with my code...
if i push button 1 time screen open for 0.1 sec, if bush button continuously lcd backlight opened but nothing view on it and countdown timer start and freeze....

any help please?

The current limit resistor for the LCD backlight is missing in the Fritz. That will cause problems.

groundfungus:
The current limit resistor for the LCD backlight is missing in the Fritz. That will cause problems.

It's better to remove the resistor?

hackertom:
It's better to remove the resistor?

I have seen some LCD displays that have the series current-limiting resistor on the LCD breakout, but most do not. Some breakout boards provide an On/Off transistor so the LEDs are just logic signals from the Arduino. But the most likely scenario is your board has no current limiting resistor and you must add one or the LED current will be too great and the LED will surely live a short life. This thread discusses your options.

Ray

Isn't there an lcd.backlight function in those liquidcrystal libraries that can just be set true/false?

INTP:
Isn't there an lcd.backlight function in those liquidcrystal libraries that can just be set true/false?

Often, yes, but I seem to remember that you have to tell the object the digital pin you are using. This implies that a transistor/FET is controlling the LED or that the digital output is directly controlling the current limiting resistor. Problem is, Op has not verified if there is a limiting resistor.

Ray

On the back of all of my LCDs I see a resistor inline from "A" coming straight from the backlight LED lead, where A and K are the pins for it. I think it's safe to assume all of them are the same in that regard.

INTP:
On the back of all of my LCDs I see a resistor inline from "A" coming straight from the backlight LED lead, where A and K are the pins for it. I think it's safe to assume all of them are the same in that regard.

No so fast, I have some in the old LCD box in the lab that do NOT have a resistor ... somewhere there is an Atmega328P-PU with an open output gate :-* Gone, but not forgotten.

Fine, let me rephrase....

I think it's safe to assume all of them that OP and any of us who are under 60 years old will have are the same in that regard.

:slight_smile:

INTP:
Fine, let me rephrase....

I think it's safe to assume all of them that OP and any of us who are under 60 years old will have are the same in that regard.

:slight_smile:

Works for me ... I'm not the least bit touchy about the over 60 inference since when I was 20 I never thought I would make it to 40.
:slight_smile:

Ray

What is the value of the resistor?

groundfungus:
What is the value of the resistor?

A single Arduino pin can sink/source 40 mA MAX but LEDs usually run about 20mA as a rule and about 3.5Vd for a White LED

(5 - 3.5) / 0.02 == 1.5/0.02 = 75 Ohm. That may well be too bright, so the final value may require a bit of experimentation. Critical users may wish to use a variable resistor for fine adjustment.
Note: there is a Vd for the CMOS output gate, but I neglected to include it in the above equation.

Ray