CONTROLLING BRIGHTNESS FOR SAINSMART I2C 20x4 LCD DISPLAY WITH A PHOTORESISTOR

Please help,

I am new to arduino.

I am trying to control the brightness of the above LCD with a photoresistor.

I am using the <hd44780.h> by Bill Perry for my sketch GitHub - duinoWitchery/hd44780: Extensible hd44780 LCD library

using lcd.setBacklight() only completely sets the backlight on and off i.e(0 turns off the backlight ,1,2,3..... turns it on completely).

Do you have a circuit of how you've wired up the photoresistor. Would help a lot

Backpacks that use PCF8574 or MCP23008 i2c i/o expanders do not support backlight dimming.
This is a h/w limitation of the backpack.
The backlight circuit is a simple transistor that can be switched on or off using an i/o pin on the expander chip.
The hd44780 library tries do its best to support dimming but is limited by the underlying h/w.
In the case of these backpacks, since the backpack can only do on or off, the library has to make a choice as to what to do when a sketch asks for dimming when dimming is not supported.

hd44780 will dim when it can, but when the h/w does not support dimming, it will turn the backlight on whenever the backlight is not fully off.
i.e. the library dims as much as it can, so if there is no dimming support, there will be no dimming and you end up with full on until the backlight is told to be off.

Essentially the backpack supports two levels. The backpacks come wired to allow on/off.
You could make a h/w modification to the backpack to have two levels of brightness but no off.
But dimming simply cannot be done with the existing h/w on these types of backpacks.


It is technically possible to do backlight dimming when using an i2c backpack but it would require h/w modifications to the backpack as well as using an additional Arduino pin and require and additional wire connected to the backpack.
And then you would need to use analogWrite() to control the Arduino pin controlling the modified backpack h/w rather than being able to use the library setBacklight() function.

--- bill

Most of the low cost backpacks have a 2 pin backlight enable link. This connects Vcc (5V) to the LED anode. If this link is removed then it is possible to wire a PNP transistor to those pins and take the base of the transistor via say a 1K reisitor to an Arduino PWM pin. Then the LED can be commanded on via the I2C and the PWM pin used to control the brightness. (High PWM values will be dim).