Arduino Forum

Using Arduino => Displays => Topic started by: joostvanpoppel on Dec 31, 2013, 07:59 pm

Title: Set LCD brightness from code?
Post by: joostvanpoppel on Dec 31, 2013, 07:59 pm
So, about two weeks ago I received the Arduino starter kit. Love IT!

Now I came up to the project that displays info on the LCD screen.
For a fututre project I got in mind, I found a setup without using a pot-meter.
(Can be found here; http://www.fibidi.com/arduino-lcd-16x2-characters/) 

Now I'm wondering: is it possible to:
- Shut the LCD off/on from code?
- Set the brightness of the LCD backlight from code?

Any help is greatly appreciated!

thx in advance,
Joost
Title: Re: Set LCD brightness from code?
Post by: Paul__B on Dec 31, 2013, 09:25 pm

Now I'm wondering: is it possible to:
- Shut the LCD off/on from code?
- Set the brightness of the LCD backlight from code?


Firstly, you do not really want to do without the potentiometer on the contrast function, as it allows you to optimise the contrast.  What you could do of course, is to use a 10k potentiometer temporarily to determine best contrast, then measure where it is set with a multimeter (it will be set quite close to ground) and replace it with two resistors - 10k to Vcc and what will be something of the order of 500 ohms to 1k, to ground.

Note that the LCD contrast voltage is quite sensitive and the contrast will change if the supply voltage varies significantly from 5V.

You can tell the LCD to blank using code of course, and it consumes very little current anyway, so this will not save power.  The LED is entirely separate.  If you wish to alter its apparent brightness, including to shut it off, you need a transistor to switch it (NPN transistor with emitter to ground, collector to pin 16 on the display, pin 15 to VCC and a 2k2 resistor from a PWM pin on the Arduino to the base of the transistor) under control from an Arduino PWM pin.
Title: Re: Set LCD brightness from code?
Post by: raschemmel on Dec 31, 2013, 10:10 pm
You can  use analog output PWM but you need to provide isolation from the PWM outputs using an RC filter (4.7k/0.1uF)
to convert the PWM to a smooth DC value. The PWM value for the contrast pin (3) will be very low (20 -100) (out of 1023)
whereas the brightness pin (15) would be high (>700) .  Also, you will need a much larger cap for the RC filter for pin-15,
maybe 1 to 10uF.  I've used this method for the contrast but never brightness (pin-15).  You can use a pot connected to
an analog input to adjust contrast and brightness by using Serial.print statements to the serial monitor of the value from
the PWM output by reading the analog input as you adjust the pot connected to it and write that value to output for
the pin you are setting.  When the contrast or brightness looks correct, write down the value sent to the serial port
and hard code that value.  You can also add a small signal diode (1N4148/1N914) between the PWM outputs and the
RC filters with the cathode connected to the resistor input for the filter.
Title: Re: Set LCD brightness from code?
Post by: RandallR on Dec 31, 2013, 11:15 pm

Now I'm wondering: is it possible to:
- Shut the LCD off/on from code?
- Set the brightness of the LCD backlight from code?


I use a pot for the contrast and a set the brightness of the backlight with a PWM pin, resistor and transistor.
The following is pretty standard code when I am using an LCD module.
Code: [Select]

// The LCD is usually interfaced via 16 pins which are labelled as shown below:
                         //Connections to Arduino
                         //  LCD                                                 Connection
                         //  1. GND - Ground                                     GND
                         //  2. VDD - 3 - 5V                                     5V
                         //  3. VO  - Contrast                                   (Tap off a 5K - 10K pot across VCC and Ground)
#define LCD_RS         8 //  4. RS  - Register Select - 0=Command / 1=Character  Arduino Pin as defined
                         //  5. RW  - Read/Write - 0=Write or 1=Read             GND
#define LCD_ENABLE     9 //  6. E   - Enable - Enable data transmit              Arduino Pin as defined
                         //  7. DB0 - Data Bit 0                                 N/A
                         //  8. DB1 - Data Bit 1                                 N/A
                         //  9. DB2 - Data Bit 2                                 N/A
                         // 10. DB3 - Data Bit 3                                 N/A
#define LCD_DB4        4 // 11. DB4 - Data Bit 4 - used in 4 bit operation       Arduino Pin as defined
#define LCD_DB5        5 // 12. DB5 - Data Bit 5 - used in 4 bit operation       Arduino Pin as defined
#define LCD_DB6        6 // 13. DB6 - Data Bit 6 - used in 4 bit operation       Arduino Pin as defined
#define LCD_DB7        7 // 14. DB7 - Data Bit 7 - used in 4 bit operation       Arduino Pin as defined
#define LCD_Backlight 10 // 15. BL1 - Backlight +                                Emitter of 2N3904, Collector to VCC, Base to D10 via 10K resistor
                         // 16. BL2 - Backlight -                                GND

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

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(LCD_RS, LCD_ENABLE, LCD_DB4, LCD_DB5, LCD_DB6, LCD_DB7);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  lcd.clear();
  pinMode(LCD_Backlight, OUTPUT);
  analogWrite(LCD_Backlight, 128); // Set the brightness of the backlight
}


This works for me.
Title: Re: Set LCD brightness from code?
Post by: raschemmel on Dec 31, 2013, 11:44 pm
RandallR's method for Backlight control is a better solution.
Title: Re: Set LCD brightness from code?
Post by: joostvanpoppel on Jan 01, 2014, 03:45 pm
Thx for your replies!

On the forums I also found this option (please note, total electronics noob here):
- Connect LCD-pin 15 to Arduino-pin 13
- Use this code:
int backLight = 13;
pinMode(backLight, HIGH);
By using digitalWrite(backLight, HIGH) or digitalWrite(backLight, LOW) it's possible to turn the backlight off/on.

Someone noticed this is "bad" to do... So my follow-up question: should or shouldnt i do it this way?

thx,
Joost
Title: Re: Set LCD brightness from code?
Post by: floresta on Jan 01, 2014, 04:21 pm
Quote
Someone noticed this is "bad" to do... So my follow-up question: should or shouldnt i do it this way?

It depends on how much current your backlight needs.  You shouldn't source (or sink) much more than 20 mA through any I/O pin and pin 13 is already supplying some current to the on-board LED.  Any other free I/O pin would be a slightly better choice but you really should use a transistor switch driven by an I/O pin.

Don
Title: Re: Set LCD brightness from code?
Post by: raschemmel on Jan 01, 2014, 07:14 pm
I concur.
Title: Re: Set LCD brightness from code?
Post by: joostvanpoppel on Jan 01, 2014, 08:48 pm
Guys, thanks for your help!

Now i'm wondering: are there any LCD's around that have "built-in" on/off functionality for backlight?

greetings,
Joost
Title: Re: Set LCD brightness from code?
Post by: raschemmel on Jan 01, 2014, 09:04 pm
Why not just add a push button switch and code for toggle
On/Toggle Off  control of the transistor ?
Title: Re: Set LCD brightness from code?
Post by: Paul__B on Jan 03, 2014, 06:32 am

Now I'm wondering: are there any LCD's around that have "built-in" on/off functionality for backlight?


They all do.  That's the whole point - that is why it has pins 15 and 16 separate from the others, so that you can wire them to a control circuit - and you can switch either the cathode or the anode, as both are separate from the other pins.

It's your business how you do this, generally with a transistor.  You can use a FET of course, but it has to be a "logic level" FET which will switch at three volts or less, the advantage being that it does not require a series resistor (2k2) in series with the base, though if controlling from an Arduino pin, it may be appropriate to have a (100k) pull-down to switch it off until the Arduino (or anything else). is initialised.

And the "I2C backpacks" which use fewer wires to drive the LCD generally include such functionality - using a transistor of course.

And: Actual "dimming" functionality is rarely needed; if anything you may wish two alternate brightness settings, one for daylight and one for night - in which case you could use a circuit in which you have the (NPN) transistor fed with a2k2 resistor from the Arduino pin and a higher value resistor such as 220k from that pin to Vcc.  Setting the Arduino pin to input will cause the LED to be dim, setting it high will make it bright and setting it low will turn the LED off.  (A fourth, mostly bright, level may be achievable by setting it to INPUT_PULLUP).
Title: Re: Set LCD brightness from code?
Post by: manek100 on Jan 03, 2014, 01:08 pm
You can connect backlight oin to some of arduino's pins directly. My 16x2 blue backlight display consume about 15mA on backlight pin so arduino is ok with this current.
Title: Re: Set LCD brightness from code?
Post by: ArduinJoe on Jan 05, 2014, 02:13 am
Reading this thread with interest. I'm currently controlling the brightness on the backlight of my 4x20 LCD directly from a PWM pin.

It's been working fine for several days but on reading this I'm now worried I'm going to fry something unless I use a transistor. What would happen if the LCD drew too much current?
Title: Re: Set LCD brightness from code?
Post by: floresta on Jan 05, 2014, 02:25 am
Quote
What would happen if the LCD drew too much current?

It's not the LCD that is the problem, it is the LED backlight. 

If you draw too much current you run the risk of damaging the I/O circuitry in the microprocessor.  The I/O pin may still appear to 'work' but it may no longer meet specifications.

Don
Title: Re: Set LCD brightness from code?
Post by: raschemmel on Jan 05, 2014, 04:45 am
If you're going to go to the trouble to control your backlight  with SW then you might as well go the
rest of the way and add the ambient control code.
Here is an example.( SEE ATTACHED FILE)

It maps the LDR sensor values (with 10k series resistor to ground) to the PWM  output values, varying
the pin-15 backlight voltage from about 1.2V to 2.8V . I had to add a 4.7k resistor pullup to +5V and a 1uF decoupling cap
from pin-15 to ground to eliminate eradic display behavior when a sudden, large change in lighting from bright to dark..
The more compensation added to pin15, the smaller the range of brightness in the backlight so basically the "counter-
measures reduced the primary objective which was noticible range of brightness.
When you turn the lights down the display gets brighter.
It's not flawless but it seems to work.
Title: Re: Set LCD brightness from code?
Post by: Paul__B on Jan 05, 2014, 04:46 am

It's been working fine for several days but on reading this I'm now worried I'm going to fry something unless I use a transistor. What would happen if the LCD drew too much current?


So, get your multimeter and connect just the LED - pins 15 and 16 - to 5V with the meter in series set on 10 amp scale (200 mA may not work) to 5V and find out!
Title: Re: Set LCD brightness from code?
Post by: ArduinJoe on Jan 05, 2014, 03:24 pm
@raschemmel

Thanks for the code - that's almost exactly what I'm doing already; reading the value from a photo resistor, dividing by four, and controlling the brightness of the back light on a scale of 0 to 255.

I haven't compensated for sudden changes in ambient light levels, which does seem like a good idea. But think I might tackle that with code rather than electronics (you lost me somewhere between 'resistor pullup' and 'decoupling cap'!). I'm doing a similar thing with two temperature sensors on the same project - sampling a value from each every second then displaying an average of those ten values. Of course that means it takes ten seconds after boot up to give a correct reading, but the readings seem quite accurate and stable thereafter.

@Paul__B

I just put my multimeter in series with the power to the LCD backlight and it's reading 8ma at full brightness, 4ma at half. So I guess if 20ma is the upper limit I'll be totally fine without adding a transistor. I have a 330ohm resistor protecting the backlight - sounds like I could drop that value a little and get a bit more brightness?
Title: Re: Set LCD brightness from code?
Post by: Paul__B on Jan 06, 2014, 12:08 pm

I just put my multimeter in series with the power to the LCD backlight and it's reading 8ma at full brightness, 4ma at half. So I guess if 20ma is the upper limit I'll be totally fine without adding a transistor. I have a 330ohm resistor protecting the backlight - sounds like I could drop that value a little and get a bit more brightness?


I am somewhat troubled by your reference to "half brightness".  If you are gong to test this, you need to do it without your control circuit (or connection to Arduino pin), because even the meter itself introduces extra resistance into the circuit and you want to know the maximum that might be drawn.  For the same reason you need to try it on the 10 amp meter setting (in addition to the 200 mA) just to see that the meter resistance is not giving you an erroneous estimate.

Most displays already have a current limiting resistor, so you need to inspect yours to see what it is.  On a 2016 it is generally "R8" but I do not have a 2004 here.  Once you verify that there is such a resistor, generally 100 ohms ("101" marking), you make the test without any other current limit and make your conclusions from there.  A 330 ohm resistor will by definition limit the current  to less than 15 mA and you certainly can use that method (down to 120 ohm) to make it safe to control directly from an Arduino pin, but you are clearly sacrificing brightness by doing so.
Title: Re: Set LCD brightness from code?
Post by: ArduinJoe on Jan 06, 2014, 12:24 pm
Thanks, Paul - this is all very useful learning.

So if I understand you correctly, to get a true sense of max. current drawn by the backlight I should connect it directly to 5v without my 330ohm resistor and set my meter to measure a 10A range?