netromaster
Posts: 33
Joined: Thu Jul 02, 2015 11:49 pm

Toggle 1602 lcd backlight on/off

Tue Jan 30, 2018 2:34 pm

I have a working library for my 1602 char lcd connected to an i2c adapter.

In the python code i see that there is a defined constant to turn off/on the lcd.

Code: Select all

 
LCD_BACKLIGHT  = 0x08  # On
#LCD_BACKLIGHT = 0x00  # Off
 

But when i initiate the command to turn lcd off for 5 seconds it doesn't seem to work.

Code: Select all

 
lcd_string(">Checking remote grid",LCD_LINE_1)
lcd_string("> ....ONLINE..",LCD_LINE_2)
LCD_BACKLIGHT = 0x00
time.sleep(5)
LCD_BACKLIGHT = 0x08
 


What am i doing wrong? Here's the code in :

Code: Select all

 
 
import smbus
import time


# Define some device parameters
I2C_ADDR  = 0x3F 
LCD_WIDTH = 16   # Maximum characters per line

# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

LCD_BACKLIGHT  = 0x08  # On
#LCD_BACKLIGHT = 0x00  # Off

ENABLE = 0b00000100 # Enable bit

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

#Open I2C interface

bus = smbus.SMBus(1) # Rev 2 Pi uses 1

def lcd_init():
# Initialise display
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off 
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)

def lcd_byte(bits, mode):
# Send byte to data pins
# bits = the data
# mode = 1 for data
#        0 for command

bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT

# High bits
bus.write_byte(I2C_ADDR, bits_high)
lcd_toggle_enable(bits_high)

# Low bits
bus.write_byte(I2C_ADDR, bits_low)
lcd_toggle_enable(bits_low)

def lcd_toggle_enable(bits):
# Toggle enable
time.sleep(E_DELAY)
bus.write_byte(I2C_ADDR, (bits | ENABLE))
time.sleep(E_PULSE)
bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
time.sleep(E_DELAY)

def lcd_string(message,line):
# Send string to display

message = message.ljust(LCD_WIDTH," ")

lcd_byte(line, LCD_CMD)

for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)

def main():
# Main program block

# Initialise display
lcd_init()

while True:

# Send some test
lcd_string("Initializing.....",LCD_LINE_1)
lcd_string("  ",LCD_LINE_2)

time.sleep(5)

# Send some more text
lcd_string(">Checking remote grid",LCD_LINE_1)
lcd_string("> ....ONLINE..",LCD_LINE_2)

time.sleep(10)

# Send some more text
lcd_string("Status: Active",LCD_LINE_1)
lcd_string("Grid Secure",LCD_LINE_2)

time.sleep(200000000000)    


if __name__ == '__main__':

try:
main()
except KeyboardInterrupt:
pass
finally:
lcd_byte(0x01, LCD_CMD)
lcd_string("Shutting down..",LCD_LINE_1)
GPIO.cleanup()
 
Last edited by netromaster on Tue Jan 30, 2018 4:41 pm, edited 2 times in total.

pcmanbob
Posts: 7276
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Toggle 1602 lcd backlight on/off

Tue Jan 30, 2018 3:54 pm

So first are you sure your i2c adapter actually supports this function ?

all the i2c adapters I could find only seem to have a physical jumper to enable or disable the backlight.

secondly please when posting code use code tags

Add [code] at the top

code goes here

and [/code] at the bottom.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

netromaster
Posts: 33
Joined: Thu Jul 02, 2015 11:49 pm

Re: Toggle 1602 lcd backlight on/off

Tue Jan 30, 2018 4:37 pm

Yes I'm sure my i2c supports it.

pcmanbob
Posts: 7276
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Toggle 1602 lcd backlight on/off

Tue Jan 30, 2018 6:05 pm

Can you post a link to the i2c adapter you are using.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported


pcmanbob
Posts: 7276
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Toggle 1602 lcd backlight on/off

Tue Jan 30, 2018 6:17 pm

Can't see any indication that it supports back light control, in fact it has the same physical jumper to turn off the back light as all the other examples I found.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

netromaster
Posts: 33
Joined: Thu Jul 02, 2015 11:49 pm

Re: Toggle 1602 lcd backlight on/off

Tue Jan 30, 2018 6:23 pm

You mean to say where there is a physical jumper the backlight can't be toggled with code?

User avatar
DougieLawson
Posts: 36570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Toggle 1602 lcd backlight on/off

Tue Jan 30, 2018 6:31 pm

netromaster wrote:
Tue Jan 30, 2018 6:11 pm
here's the link http://www.famosastudio.com/i2c-lcd-backpack
You can toggle the backlight. The pins are
RS == P0
RW == P1
EN == P2
BL == P3
D4 == P4
D5 == P5
D6 == P6
D7 == P7

I think Richard Hull has support in his library at: https://github.com/rm-hull/luma.lcd but I've not checked (both of my PCF8574s are wired to Wemoses at the moment).

I've got some code at: https://github.com/DougieLawson/Raspber ... nified_LCD
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

netromaster
Posts: 33
Joined: Thu Jul 02, 2015 11:49 pm

Re: Toggle 1602 lcd backlight on/off

Tue Jan 30, 2018 6:48 pm

DougieLawson wrote:
Tue Jan 30, 2018 6:31 pm
netromaster wrote:
Tue Jan 30, 2018 6:11 pm
here's the link http://www.famosastudio.com/i2c-lcd-backpack
You can toggle the backlight. The pins are
RS == P0
RW == P1
EN == P2
BL == P3
D4 == P4
D5 == P5
D6 == P6
D7 == P7

I think Richard Hull has support in his library at: https://github.com/rm-hull/luma.lcd but I've not checked (both of my PCF8574s are wired to Wemoses at the moment).

I've got some code at: https://github.com/DougieLawson/Raspber ... nified_LCD
I took a look at both repos and I can confidently say I didn't understand a thing looking at the code.
Is there no way to get it working with my code?

pcmanbob
Posts: 7276
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Toggle 1602 lcd backlight on/off

Tue Jan 30, 2018 7:14 pm

netromaster wrote:
Tue Jan 30, 2018 6:23 pm
You mean to say where there is a physical jumper the backlight can't be toggled with code?
Were this jumper existed on other adapters they stated that back light was on with jumper in place and off with jumper removed.

You could just remove the jumper and see if you can control the back light with you code then, you have nothing to loose trying it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
DougieLawson
Posts: 36570
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Toggle 1602 lcd backlight on/off

Tue Jan 30, 2018 7:38 pm

There's some python code here: http://www.recantha.co.uk/blog/?p=4849
And a forum thread here: viewtopic.php?f=32&t=34261&p=378524
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

netromaster
Posts: 33
Joined: Thu Jul 02, 2015 11:49 pm

Re: Toggle 1602 lcd backlight on/off

Wed Jan 31, 2018 5:58 am

I got it working now. Thanks everyone!
Now on to the next.

lnxusr
Posts: 1
Joined: Sun Aug 18, 2019 4:15 am

Re: Toggle 1602 lcd backlight on/off

Sun Aug 18, 2019 4:24 am

netromaster wrote:
Wed Jan 31, 2018 5:58 am
I got it working now. Thanks everyone!
Now on to the next.
I know it's been a while, but if you'd taken the time to tell us what you did, I may not have to continue looking for a solution.

Return to “Python”