setting LCD contrast with pwm?

I know that the datasheet-way to adjust LCD contrast is to hook a voltage divider up to the contrast pin. However I was wondering if I could just use a spare analog pin, and then I could use analogWrite() to adjust the contrast with software. I'm thinking that maybe the 500Hz PWM might mess something up.

I would test it myself, but I have to pull my device apart to get to the LCD screen, and I don't want to do it twice, so I'm wondering if anyone has successfully done this.

http://fritzing.org/media/fritzing-repo/projects/l/lcd-with-backlight-control/images/LCD%20con%20control%20del%20contraste%20proto.png

That one looks like they used a pwm pin.

I hope that project has worked. If the LCD is very sluggish (typical STN displays are) then you may get a readable result. I would strongly recommend a 10K trimpot. All the liquid crystals need is a small static voltage to align. If you can’t see anything with PWM, try a low-pass filter to get a more constant voltage.

As far I my test goes, here’s the code, pretty straight forward.

#include <LiquidCrystal.h>

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

void setup(){
// set up the LCD’s number of rows and columns:
lcd.begin(20, 4);
pinMode(10,OUTPUT);
// initialize the serial communications:
Serial.begin(9600);
delay(100);
// clear the screen
lcd.clear();
delay(3);
lcd.print(“Testing LCD…”);
}

void loop()
{
for (int i=100; i<130;i+=1)
{
delay(1500);
analogWrite(10, i);
lcd.setCursor(0,1);
lcd.print(i);
}
for (int i=130; i>100;i-=1)
{
delay(1500);
lcd.setCursor(0,1);
lcd.print(i);
}
}

I ramp up and down my PWM (no low-pass filter). I can see that only the first few numbers near 100 are good and as voltage gets higher the ripples get more and more obvious until it’s back to the start of the next loop run. Weired. I also tried to ramp it from 85 to 120, no the way back to 85, the display rippled all the way down to 86. Then loop is re-run at 85 and the display abruptly becomes good and stable. Even when the displayed characters are ok with PWM, the background slightly ripples. I’ll definitely, use a trimpot, no ripples.

Well I don't want ripples. Part of my issue is that I have very little space, so I don't even have room for a normal size trim-pot.

Mine is only 6.5mm*6.5mm*4.5mm, you're saying that's even too big?

http://www.sparkfun.com/commerce/product_info.php?products_id=9806

This one is taller due to the knob but still it's small. Now I'm curious, picture if you're willing :)

I do that with my lcd, i use an analog value of 60. Though at wider viewing angles you can notice flickering. But it is better than nothing if you don’t have a 10k trimpot.

I agree but I'm not sure about the impact on the display's life. Anyway as you said, it's a temp solution without a trimpot.