How can i adjust LCD brightness (VIA I2C interface) from code?

i've been reading a few posts how you can use the LED pin on the LCD on a pwm pin on the nano, tried that, however that doesn't seem to work

#define BRIGHTNESS_PIN      5
pinMode ( BRIGHTNESS_PIN, OUTPUT );
analogWrite(BRIGHTNESS_PIN, 255);

Hello
Post your hardware setup
to see how we can help.

iirc that backlight draws too much current for a not-amplified IO pin. You will be needing a transistor to get the job done

if understand correclty that pin leads to a transistor

Yes but if you did what I think you did, you have connected pin 5 to one of those 2 pins left of that board. That bypasses the onboard transistor.

If you want to dim that backlight using arduino's PWM pins, you will need to an extra resistor yourself.

You may also cut the trace between that base resistor and the PCF chip. Than you can solder a wire to that resistor to PWM the onboard transistor. This should allow you to dim the backlight.

Regards,

Bas

yea, i allready figured i need to disconnect that from pin 16, is there a way to avoid cutting the trace
where do i place the extra resistor?

the 2 pins on left, the lower one leads to vcc , and the upper one leads to pin 15
when they shortened the LCD fully lit, if i use a potentionmeter i can control the brightness.
if i could digitally "emulate" that potentionmeter via arduino connected to those pins that would have been perfect.

so i just connected the pwm pin to the upper pin of that jumper which is connected to pin 15, and that seems to control the LED, but i read somewhere it may damage the pwm pin

You can remove (by soldering) the base resistor and solder it back vertically on the pad which leads to the base, than you can solder a wire to the floating end of the resistor.

This allows you to undo the damage if needed.

That on board potentiometer does not control the brightness but handles the contrast. You typically configure that one time in it's lifespan.

You can add a potentiometer of your own to one of the analog pins and control the brightness via software. You can also do something fancies and use an LDR so the LCD adepts to the environment lighting :smiley:

This is like I said. That backlight draws more current than a pin can supply. In long term it may damager the pin. That is why you should use the onboard or an external resistor

great :confused: i hate ordering single stuff from ali express and wait for ages, wish there was some known transistor kit or commonly used transistor i could find ..

You can still do that solder solution and put that resistor next to the transistor on it's side. All you need is a pincet, your soldering iron and 2 minutes of your time.

Also get a resistor kit, opamp kit, optocoupler kit...

:wink:

It simply takes time (order of few years) to accumulate a decent stash of components. I have AC optocouplers, DC optocouplers both in SMD and THT. Various resistor values in bulk amounts, smd resistor book with the more exotic values. 5V mosfets, various transistors, attiny 45 chips, arduino nano's, various recitifiers, opamps, comparators, capacitors, all sort of connectors, screw terminals, alie buck converters, dupont cables. shift registers, uln2803/2003, led strips, leds, wemos d1 minis, various DIP IC sockets, various potentiometers.. to name just a few :stuck_out_tongue:

And now I am building my SMD assortment. Will it ever end? idk

not sure i understand the solution, you mean have a resistor connect between that transistor onboard and the pin?
i also was looking for articles about commons transistors for arduino and found this.

next to that onboard transistor there is that smd resistor of which one of the two leads/pads goes to the base of that transistor. That resistor you must solder loose from the board. Solder it back on the board only you solder it vertically so that one lead goes to the transistor and the other lead points to the sky. It must not be connected to the PCF chip anymore.

Than you can solder a wire from pin 5 to the floating lead of the resistor. It allows you to control the transistor directly with pwm

EDIT:
Added picture to explain better

1 Like

holy crap, turns out i actually bought a transistor kit way back (as well as capacitor kit)
however this one only has pn2222 but the differences between that and pn2n2222 seem sutble

You should be able to control the backlight without altering the backpack using a PNP transistor like bc327 or probably one of those in your kit.

The 2 pins on the backpack that you removed the jumper from are connected to Vcc and the backlight anode. Connect the PNP emitter pin to Vcc and the collector pin to the anode pin. You will need a resistor between the PNP base and the Arduino PWM pin.

yea, seems to be working although not as bright as i thought it would be, (used 2.2k resistor)

That's a strange way to wire up a transistor! Looks very fragile. Why didn't you solder it on your Protoboard?

Regarding the brightness, did you notice anything unexpected when you control the brightness with analogWrite()?

Have you tried connecting the transistor base, with the resistor, direct to 5V and 0V instead of the PWM pin? How does the brightness look then? What voltage drop do you measure across the collector-emitter of the PNP?

You could try lowering the 2K2 to 1K or 680R but I don't think it will make much difference.

Do you mean analogWrite() or were you refering to potentiometer control?

Reminder, the PNP transistor works the other way around when it comes to on/off.
Pin High -> backlight Off, pin Low -> backlight On

You can ofcourse easily fix this behaviour in code with something like
analogWrite( 255 - brightness ) ; // brightness is 0-255

Regards,

Bas

Hi,
I'm not sure if this is the schematic of the I2C board.

Tom... :smiley: :+1: :coffee: :australia:

Yes, a typo, thanks for spotting that, I've corrected it.

And, yes, I know the effect of the PNP on the use of analogWrite() but I wondered if @emaayan had figured that out or if it might be the cause of the lower than expected brightness.