problem with Using PWM for the LCD backlight

I'm using digital pin 3 ( which is a PWM pin ) to set the voltage of the back light of the LCD . When I set it to 255 its working fine , but when I change it to something less than 255 for example 127 , The brightness is reduced as excepted but its displaying random characters on the LCD . I really dont know whats going on but I'm pretty sure the connections are correct since that is working when its 255 . I'm already using the BS170 transistor where the Gate is connected to pin 3 , drain is connected to the cathode pin of the ledbacklight and the source is connected to ground and the anode of the ledbacklight is connected to 5 volts

a video showing what's happening

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 2, 4, 5, 6, 7);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
      pinMode(3, OUTPUT);

 analogWrite(3,100) ; // when 255 its working fine
  // Print a message to the LCD.
  lcd.print("hello, world!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
}

The PWM signal is not analog. If you look at it with a scope you will see it is switching your backlight on and off rapidly many times a second. You need to add an RC LOW PASS FILTER on the PWM signal using a 4.7k ohm resistor and at least a 1 uF cap. That will smooth it out enough to eliminate the problem.

thanks , I dont have a capacitor at the moment . Any other suggestions ?

NO. IT IS NOT AN ANALOG SIGNAL. YOU CAN'T USE IT THE WAY YOU ARE WITHOUT FILTERING THE PWM SIGNAL. It is as simple as that. If you want to control it manually you can use the wiper of a pot to drive the transistor. I told you why it is not working and now you are asking me if there is any other option. What other option is there going to be ? I could suggest you add a DAC to your arduino and of course that would work because that is an analog signal. There are no other options. Don't do it. What part of "it won't work without a low pass filter don't you understand ?

He said backlight, not contrast!

He's driving the backlight - LED - with a transistor - perfectly correctly. Unless he is as thoroughly confused as you are. The video is useless in showing his hardware.

If applying PWM to the backlight - pins 15 and 16 - is causing errors on the LCD, it is obviously a problem with bad connections.

Sorry , my bad. You're correct. I was thinking he was trying to drive the contrast. I did get those mixed up. Sorry Nero777.
I think I forgot to take my FOCUS today...
Paul,
Why doesn't it apply to the backlight ? Wouldn't it cause it to flicker due to the pwm signal ?

raschemmel:
Why doesn't it apply to the backlight ? Wouldn't it cause it to flicker due to the pwm signal ?

I can't see any reason why, you are using POV just like any other multiplexing application, the only reason to flicker is (given a frequency above 100 Hz,) if the PWM "hiccups" for some reason (as it well might if you are doing it in software) or if there is a heterodyne with some other multiplexing function such as when applying PWM to a multiplexed LED display - you have to use frequencies an order of magnitude apart and not harmonically related.

The LCD display is of course multiplexed, but it does not rely so much on the eye's POV as the slow response of the liquid crystals - the reason why you get the "fade" persistence effect when you update it.

Ok. But it does flicker if you try to drive the contrast. I guess the POV doesn't apply to that ?

raschemmel:
Ok. But it does flicker if you try to drive the contrast. I guess the POV doesn't apply to that ?

The contrast pin is the voltage used to feed the resistor chain which is used to perform the multiplexing on the LCD segments, so applying (raw) PWM would certainly cause trouble by altering the multiplex process! OTOH, as its impedance is relatively high (10k), it would be easy to filter.

Point is, there is no need to adjust the contrast anyway - once set correctly, that's it!

Thanks for the explanation. Don't tell Don. XD

Yes Paul__B

If applying PWM to the backlight - pins 15 and 16 - is causing errors on the LCD, it is obviously a problem with bad connections.

Thats exactly what I'm doing , I tried changing all the wires that I have used but I still get the same problem and I double checked my connections
I just cant understand how its working with 255 and not working with any other value !!

does it have anything to do with the transistor im using ? (BS170)

Nero777:
does it have anything to do with the transistor im using ? (BS170)

No.

I have just looked at you code and noticed something.

After

  lcd.print(millis()/1000);

put a   delay(200); and tell us how you go. :smiley:

still same problem :frowning:

Well, that was worth a try as updating the LCD ridiculously fast is always a source of problems.

OK, failing that what I was going to say is - we absolutely need a clear, properly-focused photograph of your rig to see what curious things may be wrong. Resolution 1024 by 768 or perhaps slightly smaller, in good even lighting (daylight best) and in a position where parts are not obscured by wires.

What happens if you change the value from 255 to, say, 250, 230, 200.... ?
Does it suddenly not work as you move from 255?