Controlling Display Backlight on Raspberry Pi


This is how to turn on and off backlight on Raspberry Pi for the official 7-inch touchscreen display and HDMI displays using command-line interface (CLI).

I checked it with Raspberry Pi 3 B+ running Raspbian Stretch (September 2017 version) + official 7″ display / GeChic 1002 10.1″ HDMI Display.

 

The official 7-inch touchscreen display [1]

OFF:

echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power

ON:

echo 0 | sudo tee /sys/class/backlight/rpi_backlight/bl_power

 

 

HDMI displays [2] [3] [4]

OFF:

vcgencmd display_power 0

ON:

vcgencmd display_power 1

 

 

Troubleshoot

You may get the error below if you are using older software.

-bash: /sys/class/backlight/rpi_backlight/brightness: No such file or directory

In that case, run rpi-update. [5]

sudo rpi-update

 

 

Summary [6]

Since I use both the official 7″ display and the HDMI display, I wrote a script which works for both displays.

Note: It’s working on my environments but I don’t think how it detects HDMI connection (i.e. line 10) is robust enough. You should test with “tvservice -s” command on your environment.

rpi-backlight:

#!/bin/bash

if [ $# -eq 0 ] || [[ $1 != "on"  &&  $1 != "off" ]]; then
  echo "Usage: rpi-backlight [on/off]"
  exit 1
fi

output=$(tvservice -s)

if [[ $output =~ "0x400000 [LCD]" ]]; then

  # Official 7" display
  if [ $1 = "on" ]; then
    echo 0 | sudo tee /sys/class/backlight/rpi_backlight/bl_power
  elif [ $1 = "off" ]; then
    echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power
  fi

else

  # HDMI display
  if [ $1 = "on" ]; then
    vcgencmd display_power 1
  elif [ $1 = "off" ]; then
    vcgencmd display_power 0
  fi

fi

 

 

References
[1] Official 7” Raspberry Pi Touch Screen FAQ

[2] Programmatically turn screen off

[3] Turning ‘tvservice’ on and off leaves screen blank

[4] RPI vcgencmd usage

[5] RPi Touchscreen – How to turn it off?

[6] Detect HDMI and Start GUI