Adjusting the light intensity with lcd.backlight () function

Continuing the discussion from LCD backlight control:

Hi, could we adjust the light intensity with lcd.backlight() function ?
Can I use lcd.backlight(50) to reduce the light intensity to 50% ?
Thanks !

AFAIK it just an on/off feature. To adjust the brightness programmatically you would need to power the LED using PWM.

To set the dim level, I simply added a resistor. When powered on, backlight on, the display operated at full brightness, but there was no off position. This setup has been working reliably for several years without any issues.

The resistor was connected across the collector and emitter leads of the transistor, corresponding to the cathode (K) and ground of the LCD LED. At the time, the easiest solution was to solder the resistor directly to the LCD.

Indeed, a suitable fixed resistor or pot would be the simplest option.

1 Like

Hi @viet5d.

The answer to your question depends on which library you are using. There are several different LCD libraries. Some of those might provide the capability to adjust light intensity. For example, if you read the other forum topic you linked:

So please tell us which library you are using.

If you installed it using the Arduino Library Manager (Sketch > Include Library > Manage Libraries... in Arduino IDE or Libraries > Library Manager in "Arduino Cloud Editor") then say so and state the full name of the library.

If you downloaded it from a website, then please post a link to that website.

@ptillisch,
You left off the rest of my comment about dimming support.
An important part being the last paragraph of my post

If you really want/need backlight dimming, you may want to look at the the hd44780 library which supports backlight control, including dimming, using the LCD API 1.0 api function setBacklight(dimlevel).

@viet5d,
The simple answer to your question is, as far as I've seen and am aware, no.
Because what you are asking for is an extension/modification of the backlight() function which is not part of the API implemented by the various libraries.
The LiquidCrystal API which is what most hd44780 libraries implement, does not support any kind of backlight control.
LCD API 1.0 defined some additional functions including how to support dimming. dimming is done using the setBacklight(dimlevel) function.
Some libraries implement some of LCD API 1.0 like the backlight control functions. (LiquidCrystal library does not)

There is also the issue of, does the h/w support dimming.
For example, the common PCF8574 based i2c backpacks do support backlight control but do not support dimming. They only support turning the backlight on or off so dimming is not possible with that hardware.
One, relatively easy way to get backlight control would be use the hd44780 library and the hd44780_pinIO i/o class which is used to control the hd44780 LCD directly with Arduino pins.
You can initialize the library with a constructor to tell it which arduino pin is used for backlight control and the active level to turn on the backlight.
Then as long as the pin provided for backlight control supports PWM, you can use the setBacklight(dimlevel) function to control its brightness.

The hd44780_pinIO i/o class supports the LiquidCrytal API as well as LCD API 1.0
This will allow sketch code written for the LiquidCrystal library to work with this i/o class with only changing the header files and constructor.
If you add the pin definition for backlight control to the constructor, then you can control the backlight including dimming, assuming you use a pin that supports PWM

See the hd4480 library documentation for more details.
Here is a link to the hd44780 library wiki:
https://github.com/duinoWitchery/hd44780/wiki

Here is the direct link to the hd44780_pinIO i/o class wiki:
https://github.com/duinoWitchery/hd44780/wiki/ioClass:-hd44780_pinIO
See the hd44780_pinIO examples for more details about how inialize the constructor and how to use use the library, including dimming.

If you have a UNO type LCD keypad hield, the examples included in the hd44780_pinIO class will likely "just work" as the pins used in the examples are for the most common pinout used on LCD keypad shields.

--- bill

Agree. Thanks for replying.

Hi ---bill,
I use the LiquidCrystal library for LCD_I2C 16x2.

It seems I will use a pot to dimmer the backlight for this LCD... (as post #4 advised)

Thanks for relying,
V5D

The LiquidCrystal library is used to control hd44780 LCD using Arduino pins to directly control the LCD pins.

That LCD has what appears to be an on board PCF8574 "backpack".
The LCD pins are control through a PCF8574 i2c i/o expander chip.
That is a totally different way of controlling the pins so it needs a different library.
That device has the h/w to allow s/w control of the backlight power.
i.e. a LCD library could turn the backlight on or off, but not dim it.

Also to remove any potential confusion, when I stated using the hd44780 library hd44780_pinIO i/o class and a pwm pin to control the back light, I did not mean to imply that the Arduino pin would hook up to the LCD and power the backlight directly. The Arduino pin would be controlling a backlight circuit. A sample backlight control circuit is show on the hd44780_pinIO wiki page.

--- bill

When you have a free PWM pin, you can use that pin to drive the backlight.
If you need to combine the LiquidCrystal_I2C with an "external" PWM pin, you could create your own class, inherit from the legacy class and add the needed functions to control the PWM pin.

something like that:

/*
  https://forum.arduino.cc/t/adjusting-the-light-intensity-with-lcd-backlight-function/1333947

  by noiasca
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

//create your own backlight enabled I2C class:
class MyLCD : public LiquidCrystal_I2C {
    const uint8_t blPin; //   UNO: 3, 5, 6, 9, 10, 11
  public :
    MyLCD (uint8_t lcd_Addr, uint8_t lcd_cols, uint8_t lcd_rows, uint8_t blPin) : LiquidCrystal_I2C(lcd_Addr, lcd_cols, lcd_rows), blPin(blPin) {}

    void init() {
      LiquidCrystal_I2C::init();
      pinMode(blPin, OUTPUT);
    }

    void backlight() {
      digitalWrite(blPin, HIGH);  // assumption HIGH active (on most PCF8574 a NPN transistor drives the LED cathode)
    }

    void noBacklight() {
      digitalWrite(blPin, LOW);
    }

    void setBacklight(byte val)  {
      analogWrite(blPin, val);
    }
};

//LiquidCrystal_I2C lcd(0x3F, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display

MyLCD lcd(0x3F, 20, 4, 9);  // address, columns, rows, PWM enabled backlight pin

constexpr uint16_t interval = 1500;

void setup() {
  Serial.begin(115200);
  lcd.init();                      // initialize the lcd
  lcd.backlight();
  lcd.setCursor(3, 0);
  lcd.print(F("Hello, world!"));
  lcd.setCursor(2, 1);
  lcd.print(F("backlight"));
}

void loop() {
  Serial.println(F("switch backlight off"));
  lcd.noBacklight();
  delay(interval);

  Serial.println(F("switch backlight on"));
  lcd.backlight();
  delay(interval);

  Serial.println(F("set backlight to 0"));
  lcd.setBacklight(0);
  delay(interval);

  Serial.println(F("set backlight to 64"));
  lcd.setBacklight(64);
  delay(interval);

  Serial.println(F("set backlight to 128"));
  lcd.setBacklight(128);
  delay(interval);

  Serial.println(F("set backlight to 192"));
  lcd.setBacklight(192);
  delay(interval);

  Serial.println(F("set backlight to 255"));
  lcd.setBacklight(255);
  delay(interval);

  Serial.println(F("set backlight to 0"));
  lcd.setBacklight(0);
  delay(interval);
}

You can disconnect the jumper and connect pin 1 to the PWM pin.
See also Dimming 16×2 and 20×4 LCD displays – Garry's blog

1 Like

Thanks for replying !
V5D

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.