Controlling 16x2 LCD contrast pin by software (i.e. PWM pin) instead of hardware

*Background*

I have been trying to control the contrast pin using a PWM output from an Uno board, rather than from a physical pot. I found that when controlling V0 from the PWM this caused random characters to appear of the LCD after varying amounts of time, while controlling V0 from the physical pot caused it to be stable. After some hair pulling, I found the answer helpfully at: http://forum.arduino.cc/index.php?topic=91529.0

So to make the the LCD work properly, I stopped controlling the V0 contrast pin from the Uno board. Note that I still controlled the backlight from the Uno board, using a digital output pin, and this worked fine. More here if interested: http://makerself.wordpress.com/2014/12/08/getting-a-16x2-lcd-screen-to-work-and-developing-a-lcd-contrast-tester/

*My question is this*

What is it about the PWM output that causes this issue? And any thoughts on how to solve this? I would be very keen to have LCD contrast be adjustable by software, rather than hardware.

I am thinking of two potential solutions to 'clean' the PWM output, but am not sure if these will work as I am not sure what it is about the PWM output that causes these issue.

Option 1>>: put in a capacitor between the PWM output and ground, as maybe the PWM output has some spikes that need to moderated that are causing the issue.

Option 2>>: have the V0 pin (which controls the contrast) connected to a supply that is controlled by a transistor. This way, the PWM pin on the Uno can control the transistor gate (if MOSFET) or base (if BJT), which would adjust the voltage on the V0 pin. This is because I suspect that it is the variable nature of the PWM which causes the issue vs. the continuous current from the pot.

My questions are: 1) Any advice on how to do this (control V0 by software)? 2) Which of these options would be the better approach (or another approach that I have not thought of)? 3) If the transistor is the way to go, which of a MOSFET or BJT would be the best for ensuring a continuous current at the V0 pin?

Thanks!

Nice idea! :) I think the best solution for this is to replace analog potentiometer with a digital potentiometer.

AD5220 , DS1804 , MCP4131 or other...

Silvius is right. A digital pot would be best in this situation.

I have been trying to control the contrast pin using a PWM output from an Uno board, rather than from a physical pot.

I'm curious about why you want to be able to vary this setting. The contrast is normally a set and forget type of adjustment.

Note that I still controlled the backlight from the Uno board, using a digital output pin, and this worked fine.

That is to be expected since it is no more complex than controlling any other LED with a digital I/O pin.

More here if interested: . . .

The perpetrator author may know how to use Wordpress but he doesn't seem to know a whole lot about electronics in general or LCD modules in particular.

Don

I’m curious about why you want to be able to vary this setting. The contrast is normally a set and forget type of adjustment.

I’ll try to answer what I find interesting idea. :slight_smile: Not necessarily for these small displays (16x2 or 20x4 which we use in our small/hobby projects.) but for a final product (commercially available) sometimes is more practical to have this option. This setting may be lost in time. (dust, vibrations, etc.)

For example, this product (jpeg attachment) has almost same option (contrast setting by user, analog pot anyway).

However, these types of displays are pretty outdated now.

Thanks Silvius and Good Inventor!

A digital pot seems to be a good idea, and would probably solve this issue. I looked at the datasheets for the ones suggested, and it looks like the AD5220 and DS1804 use a basic up/down increment protocol, while the MCP4131 uses SPI. Much appreciate the suggested ICs, as it is very helpful to find relevant parts. I will order some digital pots and try to figure this out, and greatly appreciate you helping me find this direction, thanks! ;D

As for Don/Floresta, The reason is two fold:

First, I want to have the contrast (as well as the backlight) be able to respond to external inputs automatically (e.g. amount of light in the room, as read off a photo-resistor) or just because of software decisions (e.g. want more or less contrast depending on what the output happens to be, without needing to turn a knob).

Second, is that I am intellectually interested. I am actually interested in why the PWM does not work for this, and when I discovered the issue I wanted to find out why it didn't work, as well as finding a solution. Obviously I am still learning about electronics - which is why I am asking the question here and did not already have the answer - and I figured that would go without saying given the context. I have read all the info on how to use this forum: searching first, how to post, etc. At least I am not "It doesn't work, pls help!!!" and I tried to give a well structured question with only the relevant info in the background and a link for more info if someone was interested. So I feel far from being a 'perpetrator'.

If you can let me know WHY the PWM does not work in this case based on your extensive knowledge, I would actually appreciate it! Thanks!

It would seem you still do not comprehend just what “PWM” is.

What it is not, is an analog voltage, which is what you need in your curious persistence to make a pre-set control, adjustable. It is in fact, a voltage rapidly swinging between the supply limits.

The “Vo” terminal of the LCD is one end of a resistor chain which defines a “ladder” of voltages used to multiplex the display. “Multiplexing” in this case, means that the voltages to the various segments are rapidly switched between the steps on the ladder in such a way that those segments which are required to show, are essentially subject to the full AC voltage while those experiencing a lesser voltage do not show.

This multiplexing is performed at a certain frequency generated inside the “HD44780-equivalent” chip. If you start chopping the voltage to the ladder, this will interact with the multiplexing frequency in a curious way so that most of the time (as you are necessarily making the PWM low most of the time since the voltage you want is close to zero) it will show relatively normally, but if (when) the two frequencies happen to destructively interfere, you will lose random parts of the display.

The proper way to solve this is to simply use a trimpot - set once and forgotten. But if you insist on making it variable (bizarre though this is, as you require the contrast to be optimum both in dim and bright illumination), then you need to filter the PWM with a resistor and capacitor. The problem with that is that the resistor value must be (significantly) less than 1k as this is the maximum value which will permit useful contrast when connected (permanently) to ground, so you need a larger capacitor, 10µF or more.

There is a common - dare I say it, rampant - misunderstanding that the “correct” way to control the contrast is with a 10k potentiometer. This is merely an accident of the development process that such a potentiometer was placed in the “typical applications” of the original datasheet. In fact, the internal ladder totals 11k to Vcc, so a variable resistor of up to 1k is quite adequate and more to the point, substitution by trial of standard values (100, 150, 180, 220, 270, 330 etc.) would work perfectly well.

The only real need to “vary” the contrast voltage is in fact, if Vcc itself is not regulated, so the solution is to ensure it is regulated. If temperature compensation is required, use a thermistor.

So I feel far from being a 'perpetrator'.

Sorry about that. I didn't realize that you were the author of that website and that the reference to "More here if interested:" meant more information about your problem.

I thought that you were referring readers of this forum to some authoritative information about how to get an LCD to work which is why I pointed out that the information was somewhat flawed.

Don

Paul__B,

Thanks for that great explanation! I know that PWM is varying between high and low at a fast frequency, and it is the width of the pulse that gives the average voltage. I just wasn’t sure how that was interpreted by the LCD, and your explanation was great and enlightened me. Many thanks!

Just so I am clear, I would do the following to filter the PWM signal:

  • Put a resister that is <1k between the V0 on the LCD and the PWM pin that I have chosen to vary. E.g. a 100 ohm resistor (I can experiment with some different values).

  • Put a >10µF capacitor between ground and the wire going from the PWM pin to V0 on the LCD. Does it matter if the capacitor is before or after the resistor? I suspect not, but am not sure - sorry if this seems obvious to you, just seeking clarity.

The other suggested solution (many thanks to Silvius and Good Inventor!) was to use a digital pot. What do you think of that solution? The digital pot seems pretty good, but does require more pins, so if can get it to work with just one pin that would be the ideal solution.

As for the 10k pot thing, completely agree! I experimented with other pot values and switched it out for a 1k pot when I build my LCD circuit as the 1k I had was easier to operate than the 10k pot I had on hand.


Floresta,
No worries! Yes, next time I will clarify that more! I blog about it really to myself, as writing it down helps me to understand what I am doing more and allows me to go back and see my thought process later. And it makes it easier to share when I run into problems :slight_smile:


All re: why do this,
I am actually really surprised that people don’t want to do this more (control the contrast with software, rather than hardware). Call me crazy for wanting to, but I just see this as something that has utility. It is especially the case if I want to put the hardware in a project box with just the LCD showing with maybe some buttons. Being able to have the contrast adjusted (say by two buttons dedicated to this, or by a menu) could help if it changes slightly over time or someone just wants to adjust it, but doesn’t want to open up the box and turn a pot. My parents would want this for any project I produce for them to use!

The “low pass filter” always consists of a resistor feeding a capacitor. If the capacitor were first, it would draw far too much current from the PWM pin - it would be equivalent to the pin feeding a short circuit.

Since dynamic adjustment of the contrast is never necessary, it would be a superb waste of resources and effort to use a digital potentiometer.

The point I was making is that a potentiometer is unnecessary- a 1k variable resistor between Vo and ground is all that is needed. It is common practice to connect the wiper of a pot to one end to form a variable resistor, should the wiper be defective and lose contact, it defaults to the maximum value rather than open circuit.

If you are “actually really surprised that people don’t want to do this more” it is because everyone else comprehends that once the contrast control is set for maximum contrast, no further adjustment is needed. This should not be confused with a grey scale or colour display - it is not - segments either indicate or they do not, there are only two shades to be distinguished.

Ambient light is irrelevant in regard to contrast, if you want to vary the brightness of the LED as a"user control", that is another matter. The only reason for adjusting contrast might be temperature which as I pointed out, could be done with a thermistor.

Paul__B,

Great, thanks! Much appreciated, and I will go off and test this.

As for your point on the adjustment of contrast, I take it! I am learning this, so that is helpful. A quick follow up tho: while it might not need to be dynamically adjusted on a LCD specific basis, what about between LCDs? if I had a different module (even if it was the same make/model) and I swapped it out for one that I already had in the circuit, might I need to adjust the contrast slightly?

Many thanks again for your explanations!

... if I had a different module (even if it was the same make/model) and I swapped it out for one that I already had in the circuit, might I need to adjust the contrast slightly?

Possibly. You would do it while you still had your tools out to close up the enclosure after replacing the LCD.

Don

MakerSelf: if I had a different module (even if it was the same make/model) and I swapped it out for one that I already had in the circuit, might I need to adjust the contrast slightly?

Indeed you might. That is why designs do use a trimpot - but it is generally not a user adjustment.

Note also that it is important to have the supply voltage properly regulated.

HITACHI -Application Note [AN-031]
Contrast Control for STN LCD Modules

Contrast, as well as viewing angle and brightness, is one of the most important optical parameters of a display. The contrast has an immediate effect on readability and with it the usability of the display. However, contrast depends on several parameters such as viewing angle and brightness (both, ambient and display brightness).

Furthermore everybody has a different perception of contrast due to the individual characteristics of the human eye. Because of these reasons the contrast of a display should be adjustable either automatically or manually to adapt to ambient changes in brightness & temperature.

This Application Note discusses ways in which the contrast on Hitachi STN liquid crystal displays can be controlled. Please find more information about the definition of contrast, brightness and viewing angle in the Application Note AN-008

AN-031_Contrast_Control_for_STN_LCD_Modules.pdf (536 KB)

Thanks Silvius!

Much appreciate the note and the links. Adds fuel to the adjustable contrast fire!

Adds fuel to the adjustable contrast fire!

No it doesn't. It supports what we have been telling you in every way.

Did you see mention of microprocessor control of contrast in that document?

Did you see any mention of supplying PWM signals in that document?

Don

No worries. I was more referring to this:

Silvius: Because of these reasons the [u]contrast of a display should be adjustable either automatically or manually[/u] [u][/quote][/u] [u]in relation to whether you would want to adjust it, rather than have it be set it and forget it.[/u] [u]All good, learning lots here.[/u]

I suspect the article Silvius has dredged up refers to quite different displays to the "1602" and like sort we presume to discuss here. I do not however propose to waste my time chasing it up.

Particularly where this contains the nonsense statement:

Silvius: Furthermore everybody has a different perception of contrast due to the individual characteristics of the human eye.

That is simply rubbish, other than it might be referring to - as I previously explained - a grey scale or colour display.

That is simply rubbish, other than it might be referring to - as I previously explained - a grey scale or colour display.

That "rubbish"is from Hitachi application note. An that App Note refers specifically to the type of display that talk here, namely STN display.

Wikipedia:

http://en.wikipedia.org/wiki/Super-twisted_nematic_display

A [u]super-twisted nematic display (STN)[/u] is a type of monochrome passive-matrix liquid crystal display (LCD).

CSTN stands for color super-twist nematic, a color form of passive matrix LCD.

Hitachi HD44780 LCD controller is one of the most common dot matrix liquid crystal display (LCD) display controllers available. http://en.wikipedia.org/wiki/Hitachi_HD44780_LCD_controller

I'm not saying that all 16x2 modules must have this option.

I say it is interesting to learn / experience how to control contrast and by other methods than the classic trimpot.

This experience we can apply when we actually need it. In fact this is done in industry for decades.

Let's see what the manufacturer says about such displays (16x2 display) in their datasheet (page11): link here: https://www.crystalfontz.com/product/CFAH1602AYYHJTE#datasheets

We recommend allowing field adjustment of VO for all designs. The optimal value for VO will change with temperature, variations in VDD, and viewing angle. VO will also vary module-to-module and batch-to-batch due to normal manufacturing variations.

I*deally, adjustments to VO should be available to the end user so each user can adjust the display to the optimal contrast for their required viewing conditions.* At a minimum, your design should allow VO to be adjusted as part of your product’s final test.

Although a potentiometer is shown as a typical connection, VO can be driven by your microcontroller, either by using a DAC or a filtered PWM. Displays that require VO to be negative may need a level-shifting circuit. Please do not hesitate to contact Crystalfontz application support for design assistance on your application.

Datasheet too large for upload as pdf. I will try as zip. Still too large.