Coder Social home page Coder Social logo

Backlight control about lcd HOT 5 CLOSED

the-raspberry-pi-guy avatar the-raspberry-pi-guy commented on September 27, 2024
Backlight control

from lcd.

Comments (5)

Tomtom0201 avatar Tomtom0201 commented on September 27, 2024 1

BACKLIGHT CONTROL 😉✌️


STEP 1 - Adjustment
edit lcddriver.py

  • Add custom code to end of file
  • Mind code indent
   # CUSTOM CODE
   # define backlight on/off ( on = lcd_backlight(1); off = lcd_backlight(0) )
   def lcd_backlight(self, state): # for state, 1 = on, 0 = off
      if state == 1:
         self.lcd_device.write_cmd(LCD_BACKLIGHT)
      elif state == 0:
         self.lcd_device.write_cmd(LCD_NOBACKLIGHT)

STEP 2 - Use backlight control in your code (.py)
for example create new demo file demo_lcd_backlight.py

  • Turn ON backlight >>> display.lcd_backlight(1)
  • Turn OFF backlight >>> display.lcd_backlight(0)
# Simple string program. Writes and updates strings.
# Demo program for the I2C 16x2 Display from Ryanteck.uk
# Created by Matthew Timmons-Brown for The Raspberry Pi Guy YouTube channel
# Backlight: Enhanced by TOMDENKT - backlight control (on/off)
# Backlight: Caution - adjustments mandatory: add custom code to end of lcddriver.py file
# Backlight: Only copy custom code lines 8-14 into lcddriver.py (drop lines with quotation marks, mind code indent)
"""
   # CUSTOM CODE
   # define backlight on/off ( on = lcd_backlight(1); off = lcd_backlight(0) )
   def lcd_backlight(self, state): # for state, 1 = on, 0 = off
      if state == 1:
         self.lcd_device.write_cmd(LCD_BACKLIGHT)
      elif state == 0:
         self.lcd_device.write_cmd(LCD_NOBACKLIGHT)
"""


# Import necessary libraries for communication and display use
import lcddriver
import time

# Load the driver and set it to "display"
# If you use something from the driver library use the "display." prefix first
display = lcddriver.lcd()

# Main body of code
try:
    while True:
        # Remember that your sentences can only be 16 characters long!
        print("Writing to display and toggle backlight...")
        display.lcd_backlight(1)                          # Make sure backlight is on / turn on
        time.sleep(0.5)                                   # Waiting for backlight toggle
        display.lcd_backlight(0)                          # Turn backlight off
        time.sleep(0.5)                                   # Waiting for turning backlight on again
        display.lcd_backlight(1)                          # Turn backlight on again
        time.sleep(1)                                     # Waiting for text
        display.lcd_display_string("Greetings Human!", 1) # Write line of text to first line of display
        display.lcd_display_string("Demo Backlight", 2)   # Write line of text to second line of display
        time.sleep(2)                                     # Waiting for backlight toggle
        display.lcd_backlight(0)                          # Turn backlight off
        time.sleep(0.5)                                   # Waiting for turning backlight on again
        display.lcd_backlight(1)                          # Turn backlight on again
        time.sleep(0.5)                                   # Waiting for turning backlight off again
        display.lcd_backlight(0)                          # Turn backlight off
        time.sleep(0.5)                                   # Waiting for turning backlight on again
        display.lcd_backlight(1)                          # Turn backlight on again
        time.sleep(2)                                     # Give time for the message to be read
        display.lcd_display_string("I am a display!", 1)  # Refresh the first line of display with a different message
        time.sleep(2)                                     # Give time for the message to be read
        display.lcd_clear()                               # Clear the display of any data
        time.sleep(1)                                     # Give time for the message to be read
        display.lcd_backlight(0)                          # Turn backlight off
        time.sleep(1.5)                                   # Waiting until restart

except KeyboardInterrupt: # If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup
    print("Cleaning up!")
    display.lcd_clear()
    display.lcd_backlight(1) # Make sure backlight is on / turn on by leaving

from lcd.

the-raspberry-pi-guy avatar the-raspberry-pi-guy commented on September 27, 2024

Please see #9 and #11

from lcd.

the-raspberry-pi-guy avatar the-raspberry-pi-guy commented on September 27, 2024

Hey @Tomtom0201 - nice stuff getting this to work (but only on some LCD hardware, is that right? What is the deal with this?)

If you've added functionality, the appropriate place to do this is in a pull request - that way I can approve your changes and they become part of the main repo too, rather than just a side note in a buried (closed) issue. Would you be able to rework this into some additional examples to include in the official repo and then submit a PR?

from lcd.

Tomtom0201 avatar Tomtom0201 commented on September 27, 2024

Hi Matt,

I followed your YouTube instructions for adding a 16x2 LCD display to my Pi Zero and was searching for backlight control, too - so I came here. Due to the lack of any code examples I invented a solution by myself ;-)

Regarding your questions:
1) "but only on some LCD hardware, is that right?"
I used a kind of "standard" LCD 1602 display (HD44780) with I2C interface (I2C port expander PCF8574), followed your instructions and afterwards just added lcd_backlight()-function to lcddriver.py. Definitions for LCD_BACKLIGHT resp. LCD_NOBACKLIGHT state were already in place in lcddriver.py. So from this point: I think it will work for other displays with HD44780 chip/controller too, but I cannot completely confirm functionality under certain conditions. That's why there is this precaution "may depend on your LCD hardware".

2) To be honest: I'm quite "new" to github development process flow ;-) But I will do my best to open a pull request to work amendments into it, so backlight control can become part of official repository.

Regards
Tom

from lcd.

the-raspberry-pi-guy avatar the-raspberry-pi-guy commented on September 27, 2024

Closing as functionality integrated by Tomtom0201

from lcd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.