# LCD contrast switcher for 8051

I'm trying to come up with a good circuit from common parts that will allow me to set predefined contrast levels to the LCD from the microcontroller. I've seen an example on the internet but its for the PIC micro and it uses pulse width modulation.

I cannot afford to use pulse width modulation since I need to use my CPU cycles in the micro elsewhere.

So I thought of using a simple switch setup where depending on the value of the port pins, the contrast will be low, medium or high.

The maximum voltage that will enter the LCD as well as the micro is 5VDC. Eventually I will be running this circuit on batteries.

It is important that the display can produce clear text (not faint grey) when the battery level is very low (about 2-3V)

Question is, if this circuit setup is perfect, I would assume I could use 2N3905 for PNP transistors, but what about the resistors? Do I use some inverted voltage divider equation of some sorts?

I remembered in the past when I was playing with LCD's that when I hooked a resistor from the contrast to ground without initializing the LCD I see a bar of black squares (normal), but when I instead connected the resistor from contrast to VCC, I saw nothing. So that's why I asked if I needed some sort of inverted voltage divider equation to calculate the optimal resistor values.

Say the voltage level each battery produces is the following:

4V, 3.5V, 3V.

and in each of those cases, I want the display to be of maximum darkness without melting anything inside the display.

How do I choose the optimal resistor values?

And if my transistor setup is incorrect, how would I be able to improve that?

• Could a digital potentiometer work? They have up/down interfaces if you can't spare pins for bit-banged SPI. It does depend on how much current flows out of Vo however as digipots can't handle much current. – user185972 Sep 19 '18 at 17:09
• Are you sure your micro and display are going to run on 2 to 3V? The contrast is probably the least of your problems. – Finbarr Sep 19 '18 at 17:18
• The contrast voltage is temperature and viewing-angle sensitive. For most micros PWM uses ZERO CPU cycles once it is set up. It's a hardware peripheral that, once initialized, just runs autonomously. – Spehro Pefhany Sep 19 '18 at 17:44

• PWM would work fine. And it shouldn't require a lot of CPU cycles, either. For example, if you have a timer interrupt that happens every millisecond, you just need to add one line to its ISR: output = ((++counter & 0x0F) < setting); where output is your output pin and counter and setting are 8-bit variables. This gives you 16 distinct settings with a refresh rate of 62.5 Hz. – Dave Tweed Sep 19 '18 at 19:28