0

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)

10
  • I don't see any executable code
    – Milliways
    May 17 at 12:39
  • it's because it is part of a larger code that consists of around 300 lines. hence i uploaded only this section and its required libraries as this is most likely where the problem is May 17 at 12:42
  • If you know what the problem is why are you asking?
    – Milliways
    May 17 at 12:43
  • 1
    What is this LCD display? How do you know the backlight is i2c controlled? What does its spec say?
    – CoderMike
    May 17 at 13:19
  • 3
    Any explanatory information should be edited in to your question. I am not going to read comments to understand your question.
    – joan
    May 17 at 19:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.