How to control lcd backlight

Ok, I have been trying so hard to figure this out, looking at a bunch of posts, but I couldn't find a solution that works. I am trying to turn off the LCD backlight after 30 seconds. Is there a pretty straight forward solution.

You have not provided any information about the LCD h/w you are using nor the library being used to control it.
There are many different types of LCDs out there and if/how they support backlight control can very substantially.

Controlling the backlight depends on the LCD hardware being used and library being used to control it.
Some LCD h/w includes support to control the backlight and some don't.
Some LCD libraries come with support for the LCD backlight h/w control, should it be available, and some don't.
You have to have h/w that supports backlight control to control the backlight.
If you have a library that knows how to use it the backlight h/w you can use library functions to control it.

If the library doesn't come with backlight support, you can still do it by adding you own code in the sketch, but only if the h/w supports backlight control.
In some cases you may have to add a small bit of h/w circuity to control the backlight.

--- bill

The lcd I am using is LCD1602 and I am using the liquid crystal library, I am thinking of using the extensible hd44780 lcd library but I don’t know how to use it.

mrcoder427:
The lcd I am using is LCD1602 and I am using the liquid crystal library, I am thinking of using the extensible hd44780 lcd library but I don't know how to use it.

This is not enough information we can't see your LCD h/w or your wiring.
You need to provide us more information about the h/w and how things are wired up and which library you are using.

Things at the h/w and s/w level must be very exact so you need to specify EXACTLY what you have and how you have it wired up.
i.e. there is no such library as "liquid crystal". There is a LIquidCrystal library though - that library does not support backlight control even if the h/w does.

Think of it this way. You need to describe things in enough detail (and/or show photos) such that a person could replicate exactly what you have on your end.

With respect to the hd44780 library.
It is easy to install and use.
The library comes with multiple i/o classes and which you use will depend on the LCD hardware you have.
If you using pin control to control the LCD, you would use the hd44780_pinIO i/o class.
There are many examples that show how to use it, but basically it the same as using the LiquidCrystal library other than the header files you include and your lcd object declaration, but the constructor parameters are the same as LiquidCrystal.

The hd44780 library includes backlight control, if the h/w supports it.

If you have a LCD keypad shield, the examples will support controlling the backlight.
If you have wired up your own LCD, then you will have add a bit if circuitry to control the backlight. Basically it is a transistor and resistor.

--- bill

bperrybap:
This is not enough information we can't see your LCD h/w or your wiring.
You need to provide us more information about the h/w and how things are wired up and which library you are using.

Things at the h/w and s/w level must be very exact so you need to specify EXACTLY what you have and how you have it wired up.
i.e. there is no such library as "liquid crystal". There is a LIquidCrystal library though - that library does not support backlight control even if the h/w does.

Think of it this way. You need to describe things in enough detail (and/or show photos) such that a person could replicate exactly what you have on your end.

With respect to the hd44780 library.
It is easy to install and use.
The library comes with multiple i/o classes and which you use will depend on the LCD hardware you have.
If you using pin control to control the LCD, you would use the hd44780_pinIO i/o class.
There are many examples that show how to use it, but basically it the same as using the LiquidCrystal library other than the header files you include and your lcd object declaration, but the constructor parameters are the same as LiquidCrystal.

The hd44780 library includes backlight control, if the h/w supports it.

If you have a LCD keypad shield, the examples will support controlling the backlight.
If you have wired up your own LCD, then you will have add a bit if circuitry to control the backlight. Basically it is a transistor and resistor.

--- bill

I made a circuit design:
https://www.circuito.io/app?components=512,11021,341099

The hd44780 library is available in the Library Manager. Go to Library Manager (in the IDE menus, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.

The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.

In the examples, there is a diagnostic sketch that will help us to help you if you still have trouble with the display. Run the diagnostic sketch and post the results.

Here is a simlified “Hello World” sketch (with backlight control). Aside from the libraries to include, the constructor and the begin function, all else is the same as the LiquidCrystal libraries.

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip


const int LCD_COLS = 16;
const int LCD_ROWS = 2;

void setup()
{
   lcd.begin(LCD_COLS, LCD_ROWS);
   lcd.print("Hello, World!");
   lcd.noBacklight();
   delay(5000); 
   lcd.backlight(); 
   lcd.setCursor(0,0);
   lcd.print("millis()        ");  // use blank spaces to overwrite old data
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if(millis() - timer >= interval)
   {
      timer = millis();
      lcd.setCursor(0,1);
      lcd.print(millis());
   }
}

How to use the millis() function for timing.
Several things at a time.
Beginner’s guide to millis().
Blink without delay().

groundFungus:
The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.

His drawing showed direct pin control not an i2c backpack.

To control that you would need the hd44780_pinIO i/o class as well as adding a transistor to the circuit to be able to control the backlight.

Here are two sample backlight circuits. One with a NPN transistor and one with an FET.
I’ll add this to the Wiki and the “HelloWorld” example for the hd44780_pinIO class in the next release of hd44780.

(LCD BL anode)----(VCC)

(LCD BL cathode)---------------+
                               |
                               C
                               |
(BL input)--[ Resistor ]---B-|> (NPN)
                 1k            |
                               E
                               |
                             (GND)

NOTE: The Base resistor is needed because the NPN is current fed.  For lower
      power draw, try a 10k resistor.


N-CH Mosfet version: (less power draw and lower part count)
---------------------------------------------------------------------------

(LCD BL anode)----(VCC)

(LCD BL cathode)---------------+
                               |
                               D
                               |
(BL input)----------------G-|-< (2N7000 FET)
                               |
                               S
                               |
                             (GND)

— bill

I added the sample backlight circuits to the hd44780_pinIO wiki page:

Sorry, my mistake.

But how will I control it, I don't really know so much about arduino

OK, now let's get serious!

We presume you have a common "1602" display.

The white backlight LED is connected to pins 15 and 16 with a resistor "R8" in series. Verify that this resistor is on your module and is marked "101", which is 100 Ohms.

This being the case, it limits the backlight current to 25 mA, which is within the capability of being provided from an Arduino (UNO/ Nano/ Pro Mini, Mega 2560) pin, so you can connect it directly to the pin. Connect pin 15 of the display to an Arduino pin of your choice, and pin 16 to ground as you have it shown already. You then control the backlight via that pin in your code entirely separately to whatever library you use to display information. :grinning: You just set it as an OUTPUT and write it HIGH or LOW.

A couple of points about the design you have copied. Noting that R8 is in fact 100 Ohms as it is on the vast majority of 1602 displays sold, the 220 Ohm resistor shown in series with the backlight is unnecessary. It reduces the backlight current to 8 mA - if that is bright enough or if you consider it too bright without the resistor, then by all means include the resistor is series with pin 15.

An absurd mistake has been present in most of the designs published, copied mindlessly from previous ones, where the potentiometer value is 10k and one end is connected to 5 V. It should not be connected to 5 V, that end should be left unconnected or given that it is a 10k pot, you can actually connect both ends to ground to make setting the contrast even easier.

There is a lesson here - not every design you see is correct, even if "everyone" uses it! :astonished:

mrcoder427:
But how will I control it, I don't really know so much about arduino

You wire up the backlight circuitry I showed, then you use the hd44780 library with the hd44780_pinIO class.
You fill in the constructor as shown in the examples, to include the 2 additional backlight control parameters
(The arduino pin you wired to the transistor, and the backlight active level, HIGH in this case)

Then you can call lcd.backlight() to turn the backlight on and lcd.noBacklight() to turn it off and lcd.setBacklight(dimlevel)
(where dimlevel is 0-255) to set a dim level if you are using an arduino pin that supports pwm.

NOTE:
If your backlight draws less than the output rating of your Arduino pin. (around 30ma for an Arduino Uno), then you can wire the Arduino pin directly to the LCD anode pin without the backlight circuit.

Newer LCDs tend to draw less current as low as 2ma on some 16x2 displays.
Old 16x2 LCDs and espcially larger ones like 20x4 can draw 40+ ma or even as high as 200-300ma for really old 20x4 ones.

--- bill

OK, given that I have not actually used the HD44780 library, I will add that you can do what Bill describes to what I explained - you connect the backlight to a pin and use that pin in the descriptor so that you use the library functions to control the backlight.

The point is that when you have the display directly connected to the Arduino, you can simply write to the pin without recourse to the library function - including using analogWrite if it is a PWM enabled pin.

If however, you were using an I2C “backpack”, then you must use the library to control the backlight as it is controlled by the port expander chip.

So pretty much Put the 5v pin into a digital pin?

NOTE:
If your backlight draws less than the output rating of your Arduino pin. (around 30ma for an Arduino Uno), then you can wire the Arduino pin directly to the LCD anode pin without the backlight circuit.

Newer LCDs tend to draw less current as low as 2ma on some 16x2 displays.
Old 16x2 LCDs and espcially larger ones like 20x4 can draw 40+ ma or even as high as 200-300ma for really old 20x4 ones.

Most 16x2 and 20x4 from Ebay probably draw a lot of current for the backlight.
Measure it first. You can only use direct GPIO pin if the current is < 30mA.

Otherwise use a transistor to switch the backlight.

Note that the Black text on yellow-green LCD is visible in sunlight. Does not need a backlight at all.
If you want it visible at night, it can use a modest backlight current.

David

Do you know any way to test it out? I bought my Arduino as a kit from amazon, but it doesn't say how many amps it is. This is the kit: https://amzn.to/2KIgIF6

Pretty simple.

Backlight is on pins 15, 16. Connect 16 to GND. Connect your DMM between 5V and pin 15. The backlight is lit. The DMM will report the mA.

If you do not own a DMM, say so.

David.

david_prentice:
Pretty simple.

Backlight is on pins 15, 16. Connect 16 to GND. Connect your DMM between 5V and pin 15. The backlight is lit. The DMM will report the mA.

If you do not own a DMM, say so.

David.

The multimeter said 2 milliamps which are I am pretty sure is too low

If the backlight was the correct brightness and the DMM said 2mA, you have got one of Bill's new 16x2s.

Connect pin #15 directly to a GPIO pin on your Uno and control the backlight nicely (and safely).

David.

Thank you very much I will try that and if I have a problem I will let you know