Go Down

Topic: Set LCD brightness from code? (Read 61319 times) previous topic - next topic


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,


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.


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.


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);
  pinMode(LCD_Backlight, OUTPUT);
  analogWrite(LCD_Backlight, 128); // Set the brightness of the backlight

This works for me.


RandallR's method for Backlight control is a better solution.


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?



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.



Guys, thanks for your help!

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



Why not just add a push button switch and code for toggle
On/Toggle Off  control of the transistor ?


Jan 03, 2014, 06:32 am Last Edit: Jan 03, 2014, 06:39 am by Paul__B Reason: 1

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).


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.


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?


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.



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.

Go Up