Arduino Forum

Using Arduino => Displays => Topic started by: Zarnick on Aug 20, 2014, 03:08 pm

Title: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 20, 2014, 03:08 pm
Hello, I have a backlight enabled LCD that I'm using together with the PCF8574. The wiring and schematics is the same as found in this  (http://garagelab.com/profiles/blogs/tutorial-lcd-using-only-2-arduino-pins-with-pcf8574-and-i2c)tutorial from Garagelab, the only difference is the 10k potentiometer that I simply change to a resistor.
As in the tutorial, I'm using the LiquidCrystal_I2C library found here (http://playground.arduino.cc/Code/LCDi2c). I've noticed that the library has some control for the backlight, however I'm not being able to use it (it changes nothing), am I missing some wiring? Or it may be another problem?
The function I'm talking about is the setBacklight(val), it changes nothing.
I do believe that the setContrast(val) wouldn't change anything as well, since I'm connecting the contrast pin directly on the GND with a resistor, however I haven't tested it.

Thanks for the help!
Title: Re: Backlight and I2C on LCD Display
Post by: arduinomagbit on Aug 20, 2014, 09:01 pm

Hello, I have a backlight enabled LCD that I'm using together with the PCF8574. The wiring and schematics is the same as found in this  (http://garagelab.com/profiles/blogs/tutorial-lcd-using-only-2-arduino-pins-with-pcf8574-and-i2c)tutorial from Garagelab, the only difference is the 10k potentiometer that I simply change to a resistor.
As in the tutorial, I'm using the LiquidCrystal_I2C library found here (http://playground.arduino.cc/Code/LCDi2c). I've noticed that the library has some control for the backlight, however I'm not being able to use it (it changes nothing), am I missing some wiring? Or it may be another problem?
The function I'm talking about is the setBacklight(val), it changes nothing.
I do believe that the setContrast(val) wouldn't change anything as well, since I'm connecting the contrast pin directly on the GND with a resistor, however I haven't tested it.

Thanks for the help!


I believe you need another pin to control backlight. you need to define this backlighPin. You also need to use a transistor connected to the pin, and the LCD (A, K)
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 20, 2014, 09:03 pm
Something like this?
(http://bansky.net/blog_stuff/images/alphanumericLCD_I2C.png)

If yes, how do I set up the Backlight pin on the PCF8574P? Or is it the 7th pin as default?

Thanks.
Title: Re: Backlight and I2C on LCD Display
Post by: MAS3 on Aug 20, 2014, 09:04 pm
You are connecting power lines direct to the back light, following that "do it this way and it will magically work" site.
(They are actually trying to invoke magic by mentioning it on this particular page).
Well, no magic there.
You are powering the back light LEDs, so it will light to the intensity set by the resistor you are using for this goal.
There is nothing controlling the LEDs from the PCF8574.
And there is nothing controlling the contrast either.
So your findings are correct.

Most likely controlling the contrast and back light will only work with noticeable difference between the values 0 or any other value.
That means any other value will turn it on (100 %), while value 0 will turn it off.

By the way, did you know a potentiometer consists of 2 resistors in the way it is applied here to control the contrast ?
It doesn't do what you seem to think it does the way you hacked it.

I take it that the display works but you are looking for away to control back light and contrast ?
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 20, 2014, 09:07 pm
Yes, the primary issue would be the backlight, the contrast I'm happy with the resistor solution, but it would also be nice to be able to play with it.
I did found one way by connecting a PWM Arduino Pin on the contrast, but this didn't worked for the backlight.
What I found odd, was that even if I remove the +5VCC and GND pins that should be for the backlight (pins 15 and 16), the backlight is still on!
Title: Re: Backlight and I2C on LCD Display
Post by: arduinomagbit on Aug 20, 2014, 09:08 pm

Something like this?
(http://bansky.net/blog_stuff/images/alphanumericLCD_I2C.png)

If yes, how do I set up the Backlight pin on the PCF8574P? Or is it the 7th pin as default?

Thanks.


yes, you also need a resistor (1k) between pin 7 and the transistor. If you want to control the brightness, simply use PWM. make sure that pin can handle PWM.
Title: Re: Backlight and I2C on LCD Display
Post by: MAS3 on Aug 20, 2014, 09:13 pm
My datasheet seems to be incomplete.
Which pin is the PWM compatible PCF8574 pin ?
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 20, 2014, 09:14 pm
I don't believe anyone is. That's why I used the Arduino Pin for the contrast ;)
Title: Re: Backlight and I2C on LCD Display
Post by: MAS3 on Aug 20, 2014, 09:25 pm
@Zarnick:
i missed your answer to my first reply.
The Fritzing doodle shows PCF pin 7 to be in use for brightness control.
As stated before, you need the resistor in the base of the transistor, don't forget that.

If you do want to use that PCF for such functions, it should be able to do something alike, but the PWM would need to be a software solution for this, and be probably at a relatively quite low frequency.
Not a thing to use when starting with this matter i'd say.

You need to know exactly what pin the library you are using expects for brightness and contrast control.
Sometimes you can set this, also something that should be explained in the libraries documentation.
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 20, 2014, 09:41 pm
Ok, I'll try that as soon as I get home!

Thanks for the help!
Title: Re: Backlight and I2C on LCD Display
Post by: bperrybap on Aug 21, 2014, 02:16 am
One thing you can do for brightness control is to have two brightness levels but no OFF.
In you schematic, you use a resistor to ground to set the dim brightness.
When you turn on P7 of the PCF8574,
which turns on the transistor, you will get the bright backlight.

Not PWM for full range of dimming, but it can give you two levels of brightness if you
don't ever need to turn off the backlight.

--- bill
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 21, 2014, 03:30 am
Well, unfortunately, nothing I did actually worked! Even taking the pins 15 and 16 off (completely unwiring them on the protoboard) led me no were, the backlight was still on! I'm beginning to think they may be fixed on the pins 1 and 2, since I got this LCD screen already soldered, I mean, I should completely turn the backlight off if I simply got rid of the wires on pin 16 and 15 right?
Title: Re: Backlight and I2C on LCD Display
Post by: bperrybap on Aug 21, 2014, 04:07 am

Well, unfortunately, nothing I did actually worked! Even taking the pins 15 and 16 off (completely unwiring them on the protoboard) led me no were, the backlight was still on! I'm beginning to think they may be fixed on the pins 1 and 2, since I got this LCD screen already soldered, I mean, I should completely turn the backlight off if I simply got rid of the wires on pin 16 and 15 right?


If the backlight is on when you think pins 15 and 16 are not connected, then you probably are not properly identifying
the pins.

The Tutorial you linked to show pin 1 on the left (nearest to the PCB edge) and the pins increasing to the right (twards the center)
with pin 16 furthest from the edge where pin 1 is.

The schematic you showed in reply #2 is for a different pinout.
It shows pin 15 closest to the PCB edge followed by 16 then 1 and then increasing up to 14 towards the center.

From what I have seen, the pinout in the tutorial is much more common than the one you showed
in reply #2.

--- bill
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 21, 2014, 04:10 am
I'm using the pins on the tutorial I showed, were the pins 15 and 16 are the farthest away from the PCB edge. The one I linked the image here was on another web-site, I just used since it was what I was looking for....even if it didn't helped. But I always used the pins from the tutorial, otherwise I wouldn't be able to actually use the LCD.
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 21, 2014, 02:24 pm
Just to be clear, here's the actual Fritzing sketch.
(http://i.imgur.com/Y0CggIgl.png) (http://imgur.com/Y0CggIg)
Title: Re: Backlight and I2C on LCD Display
Post by: bperrybap on Aug 21, 2014, 06:03 pm
But that fritzing sketch does not match how you said things were wired.
i.e. it shows the backlight wired up.

If you disconnect pins 15 and 16 from the LCD and the backlight is still on,
then your LCD uses a pinout that is different from the way you have it wired.

How are you connecting the wires to the actual module?
Have you soldered the wires or header to the LCD?
I would be better for us to see  some actual pictures of your wiring.
Make sure that they are clear enough for us to see the wiring and the soldering
on the LCD.

Here is what I'd recommend.
- Find a datasheet for your LCD and verify the pinout
- use a proper contrast circuit (a single resistor to ground is not a proper circuit - use a pot)
- Use this library:
https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home (https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home)
(Make sure to install it properly, which means removing any other LiquidCrystal_I2C library)
I would install it in you local sketchbook/libraries directory and name it "LiquidCrystal"
that way it will override the LiquidCrystal library that comes with the IDE.
- use the full constructor to specify how you have the PCF8574 wired up.
Code: [Select]
LiquidCrystal_I2C(i2cAddr, En, Rw, Rs, d4, d5, d6, d7, backlighPin, pol);


If you have it wired up as shown in the fritzing diagram, then your constructor should be:
Code: [Select]
   LiquidCrystal_I2C(i2cAddr, 4, 5, 6, 0, 1, 2, 3, 7, POSITIVE);


Some of the examples on fm's site and included with the library,
are out of date or are for fm's i2c backpack so ignore any code
that attempts to use/configure a backlight pin. The constructor specifies all that is needed.
You will then initialized and use the LCD the same way as the standard LiquidCrystal library
but you will have backlight control using backlight() and noBacklight().

--- bill
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 21, 2014, 06:47 pm
I'll try that as soon as possible.
But why a resistor to the ground isn't a proper circuit? (really newbie question I know, but hell, I am a newbie ;))
Title: Re: Backlight and I2C on LCD Display
Post by: bperrybap on Aug 21, 2014, 08:22 pm

I'll try that as soon as possible.
But why a resistor to the ground isn't a proper circuit? (really newbie question I know, but hell, I am a newbie ;))

The Vo input signal (LCD module pin 3) is used to control the contrast of the pixels.
You feed that pin a voltage. It isn't an input current that needs to be limited like with an LED.
The lower the voltage you provide to the Vo signal, the more intense the pixels will be.
To high of a voltage and no pixels show up, to low and all the pixels will be on including the ones
that are not supposed to be on.
When using a single resistor to ground you are essentially just grounding the pin.
Some LCDs will have an acceptable contrast when the Vo signal is grounded but many won't
and will simply turn on all the pixels including the ones that should be off.
To set the voltage on the Vo signal, you can use a 2 resistor voltage divider between two voltages.
The voltage divider will create a voltage that is between the two voltages that is proportionally
between the voltages with the same ratio as the ratio of the two resistors.
You can either use 2 fixed resistors or pot.
A pot allows you to vary the two resistors of the voltage divider to control the output voltage, and
that varying voltage is what allows you to adjust the contrast on the LCD.

--- bill
Title: Re: Backlight and I2C on LCD Display
Post by: MAS3 on Aug 21, 2014, 08:24 pm

But why a resistor to the ground isn't a proper circuit? (really newbie question I know, but hell, I am a newbie ;))



By the way, did you know a potentiometer consists of 2 resistors in the way it is applied here to control the contrast ?
It doesn't do what you seem to think it does the way you hacked it.
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 21, 2014, 09:26 pm
Ok, so here are the pictures.
I've added a trimpot for the contrast (thanks for the tip ;)) and here's the circuit:
(http://i.imgur.com/TqvbIa0s.jpg) (http://imgur.com/TqvbIa0)
And some closeups:
(http://i.imgur.com/rcpG0i1s.jpg) (http://imgur.com/rcpG0i1)
(http://i.imgur.com/YPPUzbFs.jpg) (http://imgur.com/YPPUzbF)
(http://i.imgur.com/g4si2zLs.jpg) (http://imgur.com/g4si2zL)
(http://i.imgur.com/AraJ962s.jpg) (http://imgur.com/AraJ962)
This is the LCD Pins:
(http://i.imgur.com/j0xD8u4s.jpg) (http://imgur.com/j0xD8u4)

The LCD I'm using a generic GM1602K (https://www.robocore.net/upload/lojavirtual/GDM1602K.pdf).

The code I've used was this one (with the new library you guys told me about):
Code: [Select]

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(32,4,5,6,0,1,2,3,7,POSITIVE);  // Set the LCD I2C address
void setup()
{
  // Switch on the backlight
  lcd.begin(16,2);               // initialize the lcd
  lcd.clear();
  lcd.noBacklight();
  lcd.home ();                   // go home
  lcd.print("Hello, ARDUINO "); 
  lcd.setCursor ( 0, 1 );        // go to the next line
  lcd.print (" WORLD!  ");     
}

void loop()
{

}


And of course, the backlight is still on.
As for who soldered it, I got it already soldered up.
Title: Re: Backlight and I2C on LCD Display
Post by: MAS3 on Aug 21, 2014, 09:50 pm
Earlier you showed a picture identifying your transistor as a BC548.
Is that the one you actually used ?
If not, what one did you use ?
Title: Re: Backlight and I2C on LCD Display
Post by: bperrybap on Aug 21, 2014, 10:14 pm

Earlier you showed a picture identifying your transistor as a BC548.
Is that the one you actually used ?
If not, what one did you use ?

Good questions.
If it is a BC548, then it appears top me that the transistor is wired up incorrectly.
Ground should be connected to the Emitter.

Zarnick,
look at the transistor datasheet to verify the pinout and wire it up as shown in
that second diagram you provided earlier.
BTW, if you are interested, here is a nice tutorial on transistors:
https://www.youtube.com/watch?v=F-Cv7CMHoGM
(https://www.youtube.com/watch?v=F-Cv7CMHoGM)
--- bill
Title: Re: Backlight and I2C on LCD Display
Post by: MAS3 on Aug 21, 2014, 10:16 pm
That is why i asked..
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 21, 2014, 10:25 pm
I'm sorry for the mixup, what I'm actually using is a NPN2222
Title: Re: Backlight and I2C on LCD Display
Post by: Paul__B on Aug 22, 2014, 03:20 pm

- use a proper contrast circuit (a single resistor to ground is not a proper circuit - use a pot)

Actually, a resistor to ground is a perfectly proper circuit.   :D

On the LCD board is a five resistor array, 2k2 resistors totalling 11k, from Vo to Vcc defining the six multiplex voltages for the LCD.  The contrast voltage required is generally about 4.5 to 4.8V.  If you put a resistor between 330 ohms and 1k in series with this internal resistor chain, from Vo to Gnd, this will generally provide a suitable contrast voltage.  And the display will generally work (at 4.5 to 5V supply) with Vo connected directly to ground.  Whilst a 10k potentiometer is one way of defining the voltage, 90% of its travel it totally wasted (which is even funnier when it is a ten turn pot!).  A 1k variable resistor (a pot with the wiper connected to one end) makes just as much or more sense.

And - there is no sensible reason to muck about generating this voltage using PWM as there is no reason to adjust the voltage once initially calibrated.  In general, the only problem with contrast is when the supply voltage varies.

Nor is there generally any reason to set the backlight to other than two or possibly three levels.  You want it fully bright for daylight viewing, dimmer at night time to be easier on the eyes, or off (only) if you are operating from batteries or solar power.  I have been trying to think of how to set these three levels using one Arduino pin - haven't quite figured it out yet.
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 22, 2014, 03:23 pm
Thanks Paul, I want some even simpler thing with the backlight, either on or off! But I'm not being able to make it....I honestly don't know what to try anymore...
Title: Re: Backlight and I2C on LCD Display
Post by: bperrybap on Aug 22, 2014, 07:56 pm

Nor is there generally any reason to set the backlight to other than two or possibly three levels.  You want it fully bright for daylight viewing, dimmer at night time to be easier on the eyes, or off (only) if you are operating from batteries or solar power.  I have been trying to think of how to set these three levels using one Arduino pin - haven't quite figured it out yet.



With a few changes  to the library code and wiring,
you could get 4 levels of dimming  using the PCF8574 and no extra Arduino pins.
You would use two of the output pins on the PCF8574 to control the backlight vs just one.
There is no need to support setting the R/W pin for reads so if you wire the LCD R/W pin to ground
that frees up a PCF8574 pin.
Then you can use two transistors with different current limiting resistors to control the back light.
If both are off the backlight is off.
Then you have 3 combinations of the two output pins, to provide up to 3 levels of on brightness.

You could do the same using another arduino pin in addition to the existing PCF8574 backlight control pin,
but if you are going to make h/w changes to the backpack and s/w changes to the i2c library code to support the 4 levels,
might as well just use the available PCF8574 pin rather than use another Arduino pin.

--- bill
Title: Re: Backlight and I2C on LCD Display
Post by: floresta on Aug 22, 2014, 09:57 pm
Quote
On the LCD board is a five resistor array, 2k2 resistors totalling 11k, from Vo to Vcc defining the six multiplex voltages for the LCD.  The contrast voltage required is generally about 4.5 to 4.8V.  If you put a resistor between 330 ohms and 1k in series with this internal resistor chain, from Vo to Gnd, this will generally provide a suitable contrast voltage.  And the display will generally work (at 4.5 to 5V supply) with Vo connected directly to ground.  Whilst a 10k potentiometer is one way of defining the voltage, 90% of its travel it totally wasted (which is even funnier when it is a ten turn pot!).  A 1k variable resistor (a pot with the wiper connected to one end) makes just as much or more sense.


If using a resistor between pin 3 and GND 'makes just as much or more sense' then I wonder why the vast majority of data sheets that provide any information on how to feed this pin specify using a potentiometer. 

Don
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 22, 2014, 10:00 pm
Maybe because not all LCD will have a good contrast with a specific resistor, but once you found and you can use a fixed resistance, why not?
Title: Re: Backlight and I2C on LCD Display
Post by: MAS3 on Aug 22, 2014, 10:14 pm
That remark tells you still don't understand the difference between a single resistor and the potentiometer in the fashion it is used here.
If you are using a single resistor directly connected between pin 3 and GND , then it is just a pull down resistor or a current limiting resistor.
Paul__B points out that this board (i have no idea about other boards) has already some resistors, and the extra resistor adds to that.
That will create an entirely different effect.

If you are using a potentiometer which has its ends tied to the power supply and the center contact as an output, you are creating an adjustable voltage divider.
And that is completely different from a single current limiting or pull down resistor.
Even in the circuit mentioned by Paul__B the voltage divider will do a different thing than the single resistor.

As you are going to use the voltage divider more often while experimenting, you need to understand this.
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 22, 2014, 10:17 pm
I did understood both that and my mistake, that's why I'm using a trimpot now. I'll fix it for the good contrast and that should be ok. I only meant that sometimes, you can hardwire this. I mean, I can't control the voltage the user is going to attach, but if I could, then I wouldn't need the voltage divider right? Or I'm still off the track here?
Title: Re: Backlight and I2C on LCD Display
Post by: MAS3 on Aug 22, 2014, 10:26 pm
You're still off.
If you want to use a fixed value, you should build something that does the same as that potentiometer.
And as said, that means you need to make the voltage divider by using at least 2 resistors.
You cannot do the same thing with a single resistor.

If you cannot control the voltage the user is going to attach, as you stated, then you should at least specify the voltage this is to be attached.
Should that user decide to do something completely different, then the consequences are theirs.
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 22, 2014, 10:28 pm
Ok, I'll study more on Voltage Dividers then ;).
Title: Re: Backlight and I2C on LCD Display
Post by: bperrybap on Aug 22, 2014, 10:30 pm

Ok, I'll study more on Voltage Dividers then ;).

The issue isn't really about the voltage divider,
it is understanding the nature of the signal used by the Vo pin.
A voltage divider is  simply one easy way to create that signal.

--- bill
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 22, 2014, 10:47 pm
Let me see if I finally understood that then. I need to send a specific Voltage for the contrast to be correctly adjusted, the fact that if I simply put a resistor to GND there and that it works, is because the LCD in question already has this in it's board, but I really should be simply sending a new Voltage (around 5V), thus creating a complete circuit, instead of actually closing the circuit by attaching it to GND right?
And this is actually what a potentiometer does, so it would be wrong the believe that the potentiometer is a variable resistor, it is actually a variable voltage divider, is that correct?
Title: Re: Backlight and I2C on LCD Display
Post by: Paul__B on Aug 23, 2014, 01:23 am

If you want to use a fixed value, you should build something that does the same as that potentiometer.
And as said, that means you need to make the voltage divider by using at least 2 resistors.
You cannot do the same thing with a single resistor.

You are still "off"!   :smiley-eek:

There is actually no need to use two external resistors.  Since there is already an 11k resistor on the LCD board between Vo and Vcc and will be in every such LCD assembly,  you only need an external resistor - which may be variable or fixed if you have determined a suitable value - between Vo and ground.  You then have two resistors - one external and one internal - a voltage divider.

The fact that the datasheets - which are mostly copies of one another in case that is not blindingly obvious with the mistakes transferred from one to the next - show potentiometers, is simply that they are demonstrating an experimental or "demonstration" circuit, not a definitive way to make a final product.

It is OK to use a 10k potentiometer because it is simple and probably as cheap or cheaper than a 1k one; it is not OK to assert that is the only or optimal way to do it.
Title: Re: Backlight and I2C on LCD Display
Post by: Zarnick on Aug 23, 2014, 01:31 am
I see, so my previous reply would be correct?
Title: Re: Backlight and I2C on LCD Display
Post by: Paul__B on Aug 23, 2014, 01:46 am

I see, so my previous reply would be correct?

Yes.  Try a 1k potentiometer (or even a 10k, but it will only work near one end) from pin 3 to ground, that is ground one end, wiper to pin 3, when you get a good setting you could measure the value with your multimeter and select a fixed resistor value close to the measured value.
Title: Re: Backlight and I2C on LCD Display
Post by: gilshultz on Sep 09, 2017, 03:21 am
I never found the answer to the question so here goes! The LCD boards I have are using a PCF8574  which is connected to the LCD and is controlled with the I2C interface.  It also contains a jumper plug on the opposite side of the board from the I2C interface connector.  Try connecting a P-Channel BS-170 available from On-Semi or whatever you may have.  The drain and source leads connect in place of the plug.  Put a 10K resistor in series with the gate then connect it to a PWM output.  If you connect it in reverse the LED will remain on, because the internal substrate diode will conduct.  This should not damage anything assuming a 5 volt system.  Hint: use the tilde character to complement the PWM value you write, that will invert the logic which will again be inverted by the MOSFET.  If you do not 0 will be full on, 0xFF will be full off.

When the PWM output is LOW the MOSFET will turn on illuminating the backlight LED. If it does not work try reversing the Drain Source leads. I would start with an On-Semi BS170 P-Channel MOSFET or use whatever you may have.  For those that want to use a resistor just connect it in place of the plug.  The LED cathode on my parts is driven from the +5 supply and the anode is connected to the collector of a transistor on the board.  I have not had the time to actually try this but I see no reason it should not work. If the PWM source and the LCD do not have the same ground problems will ensure.  
Title: Re: Backlight and I2C on LCD Display
Post by: 6v6gt on Sep 21, 2017, 12:22 pm
I never found the answer to the question so here goes! The LCD boards I have are using a PCF8574  which is connected to the LCD and is controlled with the I2C interface.  It also contains a jumper plug on the opposite side of the board from the I2C interface connector.  Try connecting a P-Channel BS-170 available from On-Semi or whatever you may have.  The drain and source leads connect in place of the plug.  Put a 10K resistor in series with the gate then connect it to a PWM output.  If you connect it in reverse the LED will remain on, because the internal substrate diode will conduct.  This should not damage anything assuming a 5 volt system.  Hint: use the tilde character to complement the PWM value you write, that will invert the logic which will again be inverted by the MOSFET.  If you do not 0 will be full on, 0xFF will be full off.

When the PWM output is LOW the MOSFET will turn on illuminating the backlight LED. If it does not work try reversing the Drain Source leads. I would start with an On-Semi BS170 P-Channel MOSFET or use whatever you may have.  For those that want to use a resistor just connect it in place of the plug.  The LED cathode on my parts is driven from the +5 supply and the anode is connected to the collector of a transistor on the board.  I have not had the time to actually try this but I see no reason it should not work. If the PWM source and the LCD do not have the same ground problems will ensure. 
I've just noticed this.
(a) a picture (schematic) would be worth a 1000 words.
(b) whereas a P channel mosfet could be useful as a high side driver as here for powering the led backlight (since it [the led] is already indirectly grounded), the BS170 (http://logosfoundation.org/instrum_gwr/display/BS170.pdf) is not a well chosen example of such.
Title: Re: Backlight and I2C on LCD Display
Post by: david_prentice on Sep 21, 2017, 12:41 pm
I am always amazed by variable backlight   questions.

In practice,  you might want off or on.   Possibly half-bright for night time.
You can do this with a single resistor and transistor switch.

You can probably achieve a good enough half-bright with 20mA i.e. with a 100R - 150R resistor switched over the 2 pin header.

David.
Title: Re: Backlight and I2C on LCD Display
Post by: 6v6gt on Sep 21, 2017, 01:25 pm
I am always amazed by variable backlight   questions.

In practice,  you might want off or on.   Possibly half-bright for night time.
You can do this with a single resistor and transistor switch.

You can probably achieve a good enough half-bright with 20mA i.e. with a 100R - 150R resistor switched over the 2 pin header.

David.
If you have to devote 1 pin of the MCU to control the intensity of the backlight (and of course 1 pin for the ambient light sensor) anyway, then it is only a matter of little extra coding effort to handle this using little more than analogWrite() on a PWM pin instead of digitalWrite() to change the brightness. Of course, you are correct that multiple levels of intensity are not really necessary, but in some cases, full brightness is too bright (the lcd also gets warm) so you would want at least the intensities "Day" (not Full), "Night" and "Off".
To do this nicely, it is better to spend some time on the programming logic to prevent noticeable flicker when the ambient light is on the border between 2 intensity levels. Here, I have developed something I'm quite happy with which applies large changes in the ambient light level immediately (say someone turning the room light off at night) but smaller changes are averaged and applied only every 5 to 10 seconds or so.
Title: Re: Backlight and I2C on LCD Display
Post by: david_prentice on Sep 21, 2017, 01:49 pm
Some 16x2 modules are supplied with 0R series resistor mounted for the Backlight.
Other 16x2 modules have 22R series resistor mounted for the Backlight.  (or other suitable value)

The modules with 0R take a massive current.

So yes,  PWM is a good idea.   But a better idea is to mount an appropriate series resistor in the first place.

I can't help noticing your name.   You obviously must be a younger generation.   The 6V6G had a bigger glass envelope.

David.
Title: Re: Backlight and I2C on LCD Display
Post by: 6v6gt on Sep 21, 2017, 02:05 pm
. . .
I can't help noticing your name.   You obviously must be a younger generation.   The 6V6G had a bigger glass envelope.
. . .
Well, I feel there is still some life in me yet. This was a key part of my first electronics project, a 4Watt amplifier and took a huge part of my pocket money. "T" is for tiny !
Title: Re: Backlight and I2C on LCD Display
Post by: david_prentice on Sep 21, 2017, 02:30 pm
From memory,   the GT had a cylindrical envelope.
The standard Octal base vaccuum tubes had a bulbous envelope.

Some Octal tubes had a metal envelope.    I am sure that I might have owned one.
I don't know how they were internally constructed.    Presumably there was a glass-metal pinch seal round the electrode pins.

I think the 6V6 was a Beam Tetrode.    Normally used in push-pull audio output stage.

Yes,   I know that I should check with Wikipedia before typing.

David.