I have 16x2 LCD display with a I2C backpack. This is the library I am using https://drive.google.com/drive/folders/16_UqfwFm4VKWcdeJfDmHRV2Hj_IoaTai?usp=sharing
So as given in the code below, by changing variable back
to a 0 or 1, I can turn on and off the backlight. However if I change the values of back
to 100, or 50 the backlight does not get affected, how do I make this happen without the need of an external resistor/potentiometer and but with code.
Note that on my backpack, there is no connection between pins A and K to keep the backlight on but if I connect the two, then the backlight is turned off.
If you don't care of the above here's it summarized-
How do I control Backlight of LCD with I2C backpack through code?
Code-
#include "LiquidCrystal_PCF8574.h"
#define LCD_ADDRESS 0x27
#define LCD_ROWS 2
#define LCD_COLUMNS 16
#define SCROLL_DELAY 150
int back = 1; // 1-on, 0-off
LiquidCrystal_PCF8574 lcdI2C;
void setup(){
lcdI2C.begin(LCD_COLUMNS, LCD_ROWS, LCD_ADDRESS, back);
}
void loop(){
delay(1000);
back = 0;
lcdI2C.begin(LCD_COLUMNS, LCD_ROWS, LCD_ADDRESS, back);
delay(1000);
back = 1;
lcdI2C.begin(LCD_COLUMNS, LCD_ROWS, LCD_ADDRESS, back);
}
1 Answer 1
You don't. The PCF8574 is an IO expander. It doesn't have any provision for PWM. It's either on, or off.
-
Ok, kinda disappointing, but good to knowCoder9390– Coder93902021年06月20日 14:08:19 +00:00Commented Jun 20, 2021 at 14:08
-
Can I just use analog write and connect that pin to Pin A on the lcd?Coder9390– Coder93902021年06月20日 14:14:47 +00:00Commented Jun 20, 2021 at 14:14
-
1@Coder9390, if you want to do that, you should probably interpose a transistor. The backlight current is typically more than you'd want going through an Arduino I/O pin.timemage– timemage2021年06月20日 14:20:40 +00:00Commented Jun 20, 2021 at 14:20
-
....and then use PWM on the transistor rightCoder9390– Coder93902021年06月20日 14:32:53 +00:00Commented Jun 20, 2021 at 14:32
-
Note that on my backpack, there is no connection between pins A and K to keep the backlight on but if I connect the two, then the backlight is turned off.
-- Sounds like you're short circuiting the back light there.Majenko– Majenko2021年06月20日 14:34:27 +00:00Commented Jun 20, 2021 at 14:34