I have an LCD display that has a backlight (since when I plug it in it lights up). However, using the code below I cant get it to turn on. Any help would be greatly appreciated
import smbus
import time
# Set up I2C LCD
I2C_ADDR = 0x27 # I2C address of the LCD module
LCD_WIDTH = 16 # Maximum characters per line
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
ENABLE = 0b00000100 # Enable bit
BACKLIGHT_ON = 0x08 # Backlight on command
BACKLIGHT_OFF = 0x00 # Backlight off command
# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
def lcd_init():
# Initialize display
lcd_byte(0x33, LCD_CMD)
lcd_byte(0x32, LCD_CMD)
lcd_byte(0x06, LCD_CMD)
lcd_byte(0x0C, LCD_CMD)
lcd_byte(0x28, LCD_CMD)
lcd_byte(0x01, LCD_CMD)
time.sleep(E_DELAY)
# turn on backlight
bus.write_byte(I2C_ADDR,BACKLIGHT_ON)
def lcd_byte(bits, mode):
# Send byte to data pins
# bits = data
# mode = 1 for data, 0 for command
bits_high = mode | (bits & 0xF0) | ENABLE
bits_low = mode | ((bits << 4) & 0xF0) | ENABLE
# 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)