Skip to content

Instantly share code, notes, and snippets.

@jjfalling
Last active January 8, 2016 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jjfalling/8118225 to your computer and use it in GitHub Desktop.
Save jjfalling/8118225 to your computer and use it in GitHub Desktop.
backlight control for rpi tft screen
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import os
#adjust for where your switch is connected
buttonPin = 18
sleepDelay = 1.0
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonPin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
#MAKE ALL OF THIS NATIVE
os.system("echo 252 > /sys/class/gpio/export")
os.system("echo 'out' > /sys/class/gpio/gpio252/direction")
os.system("echo '1' > /sys/class/gpio/gpio252/value")
while True:
#assuming the script to call is long enough we can ignore bouncing
if (GPIO.input(buttonPin) == 0):
#get current state of backlight
curValFile = open("/sys/class/gpio/gpio252/value", "r")
curVal = curValFile.read()
curVal = int(curVal)
curValFile.close()
#this is the script that will be called (as root)
if curVal == 0:
os.system("echo '1' > /sys/class/gpio/gpio252/value")
time.sleep(sleepDelay)
elif curVal == 1:
os.system("echo '0' > /sys/class/gpio/gpio252/value")
time.sleep(sleepDelay)
@FuzzyRoll
Copy link

Thank you! I have been trying to get that button to work but without any luck, was almost giving up and then i found this, you really made my day.

Only thing i needed to change was that on my pi i use gpio508 instead of gpio252 :)

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment