LCD Dislplay 16x2 I2C adapter button to turn on off backlight

Hi

I’ve made a temp and humity project with an lcd display 16x2. My plan was to toggle the backlight via a button, so that the light will stay on for a few seconds. I’ve not had time to figure that out, so I’m waiting for the I2C adapter to arrive before I play aroun.

I’ve tried looking online but can’t see much regarding the I2C adapter with the use of a button. But my question is, can I not just use a button/switch between the backlight jumper? Saves using code and by using a switch. Obviously I won’t have a timer on then.

Cheers

Yes you could do that. While a software solution can be more flexible, sometimes it is simpler and easier to go with a h/w solution.

In terms of how to control it in s/w you need a library that supports backlight control. The hd44780 library has backlight() and noBacklight() functions to turn the backlight on/off.
It is then a matter of writing the code to detect the button push, turning on the backlight, and have a state timer in s/w that can determine when the elapsed time has expired, and turning off the backlight.

--- bill

exiledyorkie:
But my question is, can I not just use a button/switch between the backlight jumper? Saves using code and by using a switch. Obviously I won’t have a timer on then.

Of course you can - a switch which you can turn on or off, or a button to light the backlight as long as you push it.

The question though is - why? :astonished:

Why do you want to turn the backlight off anyway? To save power? - Irrelevant if it is mains powered, you could not even detect the difference and it would be a couple of cents a year on your power bill. If it is battery powered then you would clearly want a button, not a switch.

To save the LED? Perhaps, but they are pretty long-lived and some other component is just as likely to fail first. So unless you use a button, it is just as likely to be left turned on.

Will it glare? That would be a valid reason. In which case, switch or button makes no difference, does it?

So what will you save by not doing it is software? Components? No, because it is either a switch or a button. All the other hardware is already in place in the backpack You can wire a button from a pin to ground and use INPUT_PULLUP, so no extra component either way.

Save a port pin? Perhaps. Are you desperate for port pins?

Save code? That is virtually farcical. Code is what makes the world go round - the computer world that is. :grinning:

Yes, you might be close to "full" in code space but I very much doubt it. :sunglasses:

bperrybap:
Yes you could do that. While a software solution can be more flexible, sometimes it is simpler and easier to go with a h/w solution.

In terms of how to control it in s/w you need a library that supports backlight control. The hd44780 library has backlight() and noBacklight() functions to turn the backlight on/off.
It is then a matter of writing the code to detect the button push, turning on the backlight, and have a state timer in s/w that can determine when the elapsed time has expired, and turning off the backlight.

--- bill

Thank you for the tips. I'm not sure if I'm using that library but I will check. I think I'll need to use another library for the I2C adapter which is on its way, so will have a play around. Thank you for the commands.

Paul__B:
Of course you can - a switch which you can turn on or off, or a button to light the backlight as long as you push it.

The question though is - why? :astonished:

Why do you want to turn the backlight off anyway? To save power? - Irrelevant if it is mains powered, you could not even detect the difference and it would be a couple of cents a year on your power bill. If it is battery powered then you would clearly want a button, not a switch.

To save the LED? Perhaps, but they are pretty long-lived and some other component is just as likely to fail first. So unless you use a button, it is just as likely to be left turned on.

Will it glare? That would be a valid reason. In which case, switch or button makes no difference, does it?

So what will you save by not doing it is software? Components? No, because it is either a switch or a button. All the other hardware is already in place in the backpack You can wire a button from a pin to ground and use INPUT_PULLUP, so no extra component either way.

Save a port pin? Perhaps. Are you desperate for port pins?

Save code? That is virtually farcical. Code is what makes the world go round - the computer world that is. :grinning:

Yes, you might be close to "full" in code space but I very much doubt it. :sunglasses:

:slight_smile: thank you for the in depth response.

Well, the reason for turning the backlight off is due to it being battery powered, so I'm just thinking of ways to reduce the consumption. It will be handy to toggle the backlight due to the lighting. Just playing really.

As for the code, I'm a major noob and it's not coming naturally haha. Oh and it's definitely not using all the memory.

cheers

Paul__B:
The question though is - why? :astonished:

Time

Depending on the skills and knowledge of the person, it may be much quicker and easier to get a simple switch up and working.

exiledyorkie,
you may want to take a look at jacks button library: GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses to handle the button input

and for the timeout, you could use jacks timer library: GitHub - JChristensen/Timer: A fork of Simon Monk's Arduino Timer library

It could be something as simple as when a button is pressed, you turn the backlight on, then use the after() method to call a function after the desired amount of time that simply turns off the backlight.

For a couple of bucks, it might be cool if you used something like a rcwl-0516 microwave/radar sensor instead of a button to sense when you walked up to the device. The display backlight would just light up when you walked up to it.

--- bill

exiledyorkie:
Thank you for the tips. I'm not sure if I'm using that library but I will check. I think I'll need to use another library for the I2C adapter which is on its way, so will have a play around. Thank you for the commands.

The hd44780 library includes several different i/o class for different interfaces.
You can use it for both direct pin control, hd44780_pinIO, as well as i2c control using a PCF8574 i/o expander based backpack, hd44780_I2Cexp.
The API is the same regardless of which h/w interface you have.

The only difference when using different h/w is you use a different i/o class and the lcd object constructor definition will be different.
The sketch code to drive the lcd is the same.

--- bill

exiledyorkie:
Well, the reason for turning the backlight off is due to it being battery powered, so I'm just thinking of ways to reduce the consumption. It will be handy to toggle the backlight due to the lighting. Just playing really.

OK, battery power. So you may be considering putting the Arduino to sleep as well to substantially reduce current draw.

If so, and battery current draw is critical, then there is a modification you want to make to the I2C backpack if at all possible. There was a major mistake early on in the application of the HD44780 LCD driver chip where a designer copied something from a test circuit in the chip documentation where a potentiometer was used to set the contrast voltage. Once made, this blunder became ingrained in almost all following designs and "instructional" literature, since few designers actually understood the magical workings of a multi-level multiplexed LCD - or cared to spend the time to do so.

So what? The contrast-setting potentiometer should not be connected to Vcc - 5 V. If you can cut this connection on the backpack without interrupting any other connection on the same trick, you will save 500 µA which it useful as the contrast ladder itself already draws about 500 µA and the HD44780 chip (or clone) itself about 400 µA. This is much less than the backlight of course, but is significant when battery powered (unless perhaps you are talking about a car battery). This correction will also make the contrast setting easier.

If you using battery power and have the extra pins, then don't use the i2c interface with a backpack. Direct pin control will use less power than using an i2c backpack.

bperrybap:
Time

Depending on the skills and knowledge of the person, it may be much quicker and easier to get a simple switch up and working.

exiledyorkie,
you may want to take a look at jacks button library: GitHub - JChristensen/JC_Button: Arduino library to debounce button switches, detect presses, releases, and long presses to handle the button input

and for the timeout, you could use jacks timer library: GitHub - JChristensen/Timer: A fork of Simon Monk's Arduino Timer library

It could be something as simple as when a button is pressed, you turn the backlight on, then use the after() method to call a function after the desired amount of time that simply turns off the backlight.

For a couple of bucks, it might be cool if you used something like a rcwl-0516 microwave/radar sensor instead of a button to sense when you walked up to the device. The display backlight would just light up when you walked up to it.

--- bill

bperrybap:
The hd44780 library includes several different i/o class for different interfaces.
You can use it for both direct pin control, hd44780_pinIO, as well as i2c control using a PCF8574 i/o expander based backpack, hd44780_I2Cexp.
The API is the same regardless of which h/w interface you have.

The only difference when using different h/w is you use a different i/o class and the lcd object constructor definition will be different.
The sketch code to drive the lcd is the same.

--- bill

Hi Bill
Thank once again, those links look very interesting. I'd have never come across those. I have a screen without the I2C adapter, so I may have a go with that first, see if I can get it working.
The sensor sounds like a great idea. Not sure if I'll use it for this but its something to paly around with.

Paul__B:
OK, battery power. So you may be considering putting the Arduino to sleep as well to substantially reduce current draw.

If so, and battery current draw is critical, then there is a modification you want to make to the I2C backpack if at all possible. There was a major mistake early on in the application of the HD44780 LCD driver chip where a designer copied something from a test circuit in the chip documentation where a potentiometer was used to set the contrast voltage. Once made, this blunder became ingrained in almost all following designs and "instructional" literature, since few designers actually understood the magical workings of a multi-level multiplexed LCD - or cared to spend the time to do so.

So what? The contrast-setting potentiometer should not be connected to Vcc - 5 V. If you can cut this connection on the backpack without interrupting any other connection on the same trick, you will save 500 µA which it useful as the contrast ladder itself already draws about 500 µA and the HD44780 chip (or clone) itself about 400 µA. This is much less than the backlight of course, but is significant when battery powered (unless perhaps you are talking about a car battery). This correction will also make the contrast setting easier.

bperrybap:
If you using battery power and have the extra pins, then don't use the i2c interface with a backpack. Direct pin control will use less power than using an i2c backpack.

I do plan to play around with sleep mode. I've read the Nick Gammon article but just not had chance to play around yet. Just in the process of finishing uni. So I've done a bunch of reading/video watching.

I thought about running from mains but I'd rather use batteries so will play around before it is built. I will only use it during the evenings, so I wouldn't have thought it'd consume so much power. I plan to have the temp sensor elsewhere and use rf24 to transmit data. Although I'm yet to receive those too.

Great advice about the pot on the board, I had not heard of that problem but it's definitely something to consider. Maybe I'll use the display alone. Or just order an OLED :slight_smile:

Thanks again guys. Amazing info there!

Update

Although I have not received the I2C adapter as of yet, I had a dabble with my current lcd display.

Came across this code which I must have overlooked prior, due to looking at so many pages/vids. Thought it may help some one who is looking for the same solution.

https://theuzo007.wordpress.com/2012/12/29/turn-on-lcd-backlight-by-button-arduino/

Will just have to adapt once the I2C adapter arrives.

Thanks again

exiledyorkie,
You need to be careful if you decide to wire up the backlight to be controlled directly by an arduino pin as shown in the wordpress article. Depending on the current needed for the backlight, it could damage the AVR.

Before doing that, you should measure the current draw of the backlight. If it exceeds 40ma you need a transistor.
IMO, I'd use a transistor if it were above about 20-30 ma. If power consumption really is a concern then use an FET.

Given you are expecting a i2c backpack soon and it will have a transistor to drive the backlight,
I'd just wait on that if you don't have a meter to measure the current.

--- bill

Also, just noticed in that wordpress article, you don't need the 10k pullup resistor. Just use INPUT_PULLUP instead of INPUT mode for the pin.

--- bill

A transistor! Okay, will have a look into that. I’m sure I came across a tutorial using one.
Will have to did through the many windows open on my phone.
I do have a multimeter, so will measure that.
Only done a few tests, so hopfully it’ll be okay if it is over. Will check that shortly.

bperrybap:
Also, just noticed in that wordpress article, you don't need the 10k pullup resistor. Just use INPUT_PULLUP instead of INPUT mode for the pin.

--- bill

Ah I was not aware of that! I will have a look into that and how it works.

How would I measure the current drawn? Just a tad confused.
In series with either the anode/cathode (pin 15/16 of lcd display)
Or in series from the pin driving the lcd?

exiledyorkie:
How would I measure the current drawn? Just a tad confused.
In series with either the anode/cathode (pin 15/16 of lcd display)
Or in series from the pin driving the lcd?

You won't need or use the Arduino to measure the current.
Yes, wire up the backlight to power and ground with the meter in series on one side,
Use a meter to measure the current between either the anode and 5v or the cathode and ground.
Either side can be used.
Just make sure you have the correct polarity for the meter probes for the side you chose to measure.

If you get less than 40ma you can safely use the Arduino pin to power the backlight directly.
--- bill

bperrybap:
You won't need or use the Arduino to measure the current.
Yes, wire up the backlight to power and ground with the meter in series on one side,
Use a meter to measure the current between either the anode and 5v or the cathode and ground.
Either side can be used.
Just make sure you have the correct polarity for the meter probes for the side you chose to measure.

If you get less than 40ma you can safely use the Arduino pin to power the backlight directly.
--- bill

Glad you’re here to bail me out bill :slight_smile:

Measured the current and its 19.5mA. So all is good I guess.

Will have a play around with a transistor, just for experience as I’ve never used one. Got a NPN in my starter kit.

Thanks once again.

The simplest way is to check the resistor R8 on the display - you will find it is a "101" or 100 Ohm. This limits the current to something in the order of 20 mA which is safe for an Arduino pin. I have measured 25 mA.

Note that putting the multimeter in series with the device may reduce the actual current measured when you do so, however an Arduino output driver has an internal resistance of about 40 Ohm so this also limits the current in practice.

Paul__B:
The simplest way is to check the resistor R8 on the display - you will find it is a "101" or 100 Ohm. This limits the current to something in the order of 20 mA which is safe for an Arduino pin. I have measured 25 mA.

Note that putting the multimeter in series with the device may reduce the actual current measured when you do so, however an Arduino output driver has an internal resistance of about 40 Ohm so this also limits the current in practice.

I removed the screen in order to measure across R8. This appears to be 150mA, which is not what I was hoping for.
Measuring in series from +5V to the Anode is 20.5mA, which is a little more than when the screen was in the bread board.

So I guess the arduino is supplying 20.5mA, rather than the 150mA?

bperrybap:
Also, just noticed in that wordpress article, you don't need the 10k pullup resistor. Just use INPUT_PULLUP instead of INPUT mode for the pin.

--- bill

I used the PULLUP code. But if I remove the actual resistor from the breadboard, the backlight remains on. I changed the format of the wires to the switch but nothing.

I’m assuming something in the code will have to be the opposite but nothing I have changed works so far. I’m pretty much using the same code for the button on that other webpage I posted. Any ideas?

I’ll have another play at a later date.

exiledyorkie:
I removed the screen in order to measure across R8. This appears to be 150mA, which is not what I was hoping for.

If you put the probes measuring current on each side of the resistor you are effectively shorting it out.
You are then measuring the current if the resistor was 0 ohms - which is not good as it likely way over drives the LEDs in the backlight which could damage it.

--- bill