Turn on lcd backlight by button, Arduino

Posted: December 29, 2012 in Arduino
Tags: , ,

This is post about Arduino and how to turn on the backlight of the lcd display by pressing a button and how to make that the display backlight will turn off with delay.

Hardware that I used
  • Arduino Uno
  • 16×2 LCD display HD44780
  • Pushbutton
  • 10k ohm resistor
  • 4k7 ohm potentiometer

Image was made using Fritzing.

Arduino circuit lcd button
 Make sure that your display backlight does not draw more current then arduino pin can handle! For Arduino Uno that is 20mA max. In my case, backlight draw 15mA. If current is more then 20mA use a current limiting resistor (backlight will be less bright) or use a transistor and external power supply.


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(){
  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.

  // Set the push-button pin as an input.
  // Set the lcd display backlight anode pin as an 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);
    // Button not pressed.
    //Serial.println("Button not pressed - LOW");

      currentLcdLightOnTime = millis() - lcdLightOn_StartMillis;
      if(currentLcdLightOnTime > LCD_LIGHT_ON_TIME){
        isLcdLightOn = false;
        digitalWrite(LCD_LIGHT_PIN, LOW);

  Serial.print("Lcd light on time: ");
  1. Bryce says:

    So I’ve put together everything and written out the code but I keep getting and error stating “Invalid suffix “_” on integer constant what should I do to fix this

    • TheUzo007 says:

      Looks like there’s a problem with constants. I’m guessing that with LCD_LIGHT_ON_TIME. I read somewhere that integer constants default to int so may be the number 60000 is to big for signed int. Try replacing the line #define LCD_LIGHT_ON_TIME 60000 with unsigned int LCD_LIGHT_ON_TIME = 60000; or change number 60000 to number lower then 32000.
      If that doesn’t work try replacing all the constants with variables, and see what you get.

      • Bryce says:

        I’ve tried everything and I still can’t seem to figure out what the problem is.

      • TheUzo007 says:

        I do’t know what else to do. Try deleting “line by line” to see where/what the problem is. Or write the script again gradually, not just copy it all at once, but line by line and test it every time you add something new to the script.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s