2

I have a RPi3B with the RPi 7inch screen attached. I am running Jessie, but will re-image it to stretch soon. I would like to turn the screen on or off via Python. For example, when I ask what is the time I want the screen to wake up as I display a certain webpage on the screen that has time and date. Then after a certain amount of time I want it to go blank again.

How can I control the screen display Python or via command line?

I have found posts that explain how to stop the screen timeout, but none that explain how to toggle back and forth.

Lee Melbourne
  • 219
  • 1
  • 3
  • 15

3 Answers3

3

Command line:

OFF:

sudo -E sh -c 'echo 1 > /sys/class/backlight/rpi_backlight/bl_power'

ON:

sudo -E sh -c 'echo 0 > /sys/class/backlight/rpi_backlight/bl_power'

TIP: I define the above as backlight_off and backlight_on in ~/.bash_aliases. Be sure to save that file elsewhere, since you'll lose it if you re-image.

Python:

  • First save the above as backlight_off.sh and backlight_on.sh (with proper #!/bin/sh shebang header), maybe saved to /usr/local/bin/ if you want it available in your $PATH
  • ... then you can use Python subprocess() to invoke these new scripts. Use the "command syntax" to run the script files, and the script's own shell should take care of things.
  • If you are doing this in a web application (and therefore rightly concerned about security), then avoid use of variables around these calls (hardcode the Python subprocess calls), and get the code peer-reviewed.

If you read Subprocess documentation, it may contain suggestions to not use it to run shell scripts. That's fine in most cases, but not in this one.

FYI - "-c" is a life-saver

The "-c" option is often used to "wrap" commands into a user context, like sudo. You'll see this in some cron files, or used heavily by the Docker community.

More reading (and doing this in "pure" Python):

There are downsides to this, and you may need to run your Python script as sudo (not a good idea, generally) or make configuration changes to your Pi. You'll learn a bit going down this rabbit hole..

FYI this also worked:

pi@pi3:~ $ echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power
1

That works because the redirect is executed as sudo, but there is the unintended side-effect of 1 output text.

Scott Prive
  • 231
  • 2
  • 4
1

Although not Python these is commands I use to turn the screen off and on.

turn screen OFF

echo 1 > /sys/class/backlight/rpi_backlight/bl_power

turn screen ON

echo 0 > /sys/class/backlight/rpi_backlight/bl_power

Note: I did have to run these via sudo/root

rob
  • 2,803
  • 2
  • 21
  • 31
  • Thanks. I have tried that, but I get "-bash: /sys/class/backlight/rpi_backlight/bl_power: Permission denied" even when using sudo. I dont quite understand why sudo doesnt override the security – Lee Melbourne Jun 07 '18 at 10:23
  • how bizarre, what permissions does it report ls -l /sys/class/backlight/rpi_backlight/ ? – rob Jun 07 '18 at 12:20
  • -r--r--r-- 1 root root 4096 Jun 10 21:11 actual_brightness -rw-r--r-- 1 root root 4096 Jun 10 21:11 bl_power -rw-r--r-- 1 root root 4096 Jun 10 21:11 brightness lrwxrwxrwx 1 root root 0 Jun 10 21:11 device -> ../../../rpi_backlight -r--r--r-- 1 root root 4096 Jun 10 21:11 max_brightness drwxr-xr-x 2 root root 0 Jun 10 21:11 power lrwxrwxrwx 1 root root 0 Jun 10 21:11 subsystem -> ../../../../../class/backlight -r--r--r-- 1 root root 4096 Jun 10 21:11 type -rw-r--r-- 1 root root 4096 Jun 10 21:11 uevent – Lee Melbourne Jun 10 '18 at 11:14
  • I ran "sudo chmod 777 /sys/class/backlight/rpi_backlight/bl_power" and that then allowed me to run the commands you mentioned. However they didnt seem to affect the screen. – Lee Melbourne Jun 10 '18 at 11:26
  • I dont think this works with Stretch. As it doesnt seem to help with my Pi. I would be interested to know if someone else has tested this. – Lee Melbourne Jun 17 '18 at 06:26
  • 1
    @LeeMelbourne Ensure you've disabled console blanking: https://github.com/notro/fbtft/wiki/Boot-console#console-blanking – Aaron F Jul 10 '18 at 12:22
  • 1
    @LeeMelbourne Most likely you're using sudo incorrectly. sudo works on commands, not on redirections, so sudo command > file runs the command elevated, but accesses the files with your initial permissions. – Dmitry Grigoryev May 28 '19 at 11:19
  • Is this supposed to turn off the screen or to turn it dark? I have a 3rd party screen and had to replace rpi_backlight with 10-0045 (the rest looks the same). However, the display only gets a bit darker, not turned off, when running the commands... – Manuel Popp Oct 16 '22 at 09:37
1

ok, so the permissions error reported in the comments is not pi specific, it's unix/linux specific.

sudo echo 1 > /sys/class/backlight/rpi_backlight/bl_power

will give you -bash: /sys/class/backlight/rpi_backlight/bl_power: Permission denied because the sudo is only applying to the echo statement, not the redirection of output to the /sys/ file.

One option is to sudo bash followed by echo 1 > /sys/class/backlight/rpi_backlight/bl_power.

Alternatively, sudo sh -c "echo 1 > /sys/class/backlight/rpi_backlight/bl_power". That takes everything in double quotes and runs it in a shell that has had it's privileges elevated.

mrda
  • 11
  • 1