Is there a way to adjust the brightness of the official touchscreen display? I couldn't find a definite answer. I'm running the latest release of Raspbian Jessie.
3 Answers
The driver for the screen provides an interface through /sys/
. To turn the screen on you can use the command:
echo 0 > /sys/class/backlight/rpi_backlight/bl_power
and to turn it off:
echo 1 > /sys/class/backlight/rpi_backlight/bl_power
the brightness can be adjusted using:
echo n > /sys/class/backlight/rpi_backlight/brightness
where n
is some value between 0 and 255.
-
1Just small typo... rpi-backlight should be rpi_backlight for the brightness May 25, 2016 at 20:21
-
2Note that the 'brightness' setting only works on v1.1 LCD hardware. on v1.0, the control is binary, < 128 are "off", >= 128 are "on" see raspberrypi.stackexchange.com/questions/49822/… Jun 15, 2016 at 14:17
-
4If you get "Permission denied" errors, try with
sudo bash -c "echo 0 > /sys/class/backlight/rpi_backlight/bl_power"
(from raspberrypi.org/forums/viewtopic.php?p=848197#p848197) Apr 6, 2018 at 9:54 -
What if the folder
rpi_backlight
or the filebl_power
don't exist? It's the case with openplotter so none of this works Nov 1, 2018 at 14:11
I've made a Python package for this: github.com/linusg/rpi-backlight. Now you don't need to implement this yourself anymore.
(GIF is outdated because API was changed quite a bit in v2, sorry... Below example is correct 🙂)
Works basically like the above, example:
>>> from rpi_backlight import Backlight
>>>
>>> backlight = Backlight()
>>> backlight.brightness
100
>>> backlight.brightness = 50
>>> backlight.brightness
50
>>>
>>> with backlight.fade(duration=1):
... backlight.brightness = 0
...
>>> backlight.fade_duration = 0.5
>>> # subsequent `backlight.brightness = x` will fade 500ms
>>>
>>> backlight.power
True
>>> backlight.power = False
>>> backlight.power
False
>>>
It has a GUI, a CLI and a simple Python API :)
If you like an application, you can use the code below. I'll give detailed instructions of how to use it.
- create a new file
/home/pi/brightnesschanger.py
- copy and paste the following code into that file (Explanation: this is the program for changing brightness):
import tkinter as tk
from tkinter import ttk
MAX_BRIGHTNESS = 255
CONFIG_FILE = "/sys/class/backlight/rpi_backlight/brightness"
def runApp ():
# root window
root = tk.Tk ()
root.geometry ('330x100')
root.resizable (True, True)
root.title ('Raspberry Pi 7" screen brightness')
root.columnconfigure (0, weight=1)
root.columnconfigure (1, weight=3)
# slider current value
current_value = tk.IntVar ()
def get_current_value ():
return (current_value.get ()/MAX_BRIGHTNESS*100)
def slider_changed (event):
value_percent = "{: .2f} %".format (get_current_value ())
value_label.configure (text=value_percent)
try:
with open(CONFIG_FILE, "w") as file:
value = current_value.get ()
file.write (str(value))
except:
print ("ERROR: Could not set brightness")
#probably file does not exist
# slider
slider = ttk.Scale (
root,
from_=12,
to=MAX_BRIGHTNESS,
orient='horizontal', # vertical
command=slider_changed,
variable=current_value
)
slider.pack(expand = 'yes', fill = 'x')
# current value label
# current_value_label = ttk.Label (root, text='Current Value:' )
# current_value_label.pack(expand = 'yes', fill = 'x')
# value label
value_label = ttk.Label (root, text=get_current_value ())
value_label.pack(expand = 'yes', fill = 'x')
try:
with open(CONFIG_FILE, "r") as file:
value = file.readline ()
slider.set (int (value))
except:
print ("Error: Could not read brightness")
root.mainloop ()
if (__name__ == "__main__"):
runApp ()
- Save and close the file
- Unfortunately you cannot double-click that file to run the program.
- We will now create a file on the Desktop, that you can doubleclick to start the program
- Open a text editor and paste the following content:
[Desktop Entry]
Version=1.0
Name=Brightness
Comment=Change brightness of Raspberry Pi 7 inch official screen
Exec=sudo python3 /home/pi/brightnesschanger.py
Path=/home/pi
Terminal=false
Type=Application
Categories=Utility
- Save the file under
home/pi/Brightness.Desktop
- Security notice: The application will be started with root privileges. If you use your pi only for playing around, this should be no problem. Otherwise you should figure out if this is a problem for you.