The new Raspberry display works nice, but the screen is too bright. At night in the kitchen it enlightens the entire room - unnecessarily. Since I’m using the original Raspberry Pi 7" touch display, the brightness can be controlled inĀ /sys/class/backlight/rpi_backlight/brightness
. This “file” can hold a value from 0
(display off) to 255
(full bright).
My first idea was to use a light sensor. But the display itself does not have such a sensor, and if I connect one to the Pi, I somehow need to connect the sensor in a way that it can “see” the light in the room. Which means the sensor is clearly visible next to the display.
The next idea is a cron job, which dims the display, depending on the time of the day. A bit brighter over the day, and almost dark in the night. Cron job works fine, but let’s make this an exercise in writing more code by using systemd timers. Because why not making it more complicated …
A “cron job” in systemd needs two files. One is the timer:
[Timer]
OnCalendar=*-*-* 06..22:10:00
OnBootSec=60
Persistent=false
Unit=brightness-up.service
[Install]
WantedBy=timers.target
This file goes into /etc/systemd/system/
and the filename needs to end in .timer
. In my case the filename is brightness-up.timer
. Here I run the timer every hour from 6:10
to 22:10
. Why at :10
, and not at :00
? Because there is another cron job which reboots the Pi at 4:00.
The OnBootSec=60
also runs this timer one minute after the Pi started. However I don’t want systemd to trigger this timer when one occasion was missed - hence the Persistant=false
setting.
Last but not least, the Unit=
specifies which service should be executed once the timer fires. Which brings me to the second file:
[Unit]
Description=Increase display brightness
[Service]
Type=oneshot
ExecStart=/bin/sh -c '/bin/echo 50 > /sys/class/backlight/rpi_backlight/brightness'
TimeoutStopSec=30
KillMode=none
RemainAfterExit=no
User=root
Group=root
This goes into theĀ filename brightness-up.service
, and the interesting part is ExecStart
. That is the code which will set the brightness for the display. This needs a shell (/bin/sh
), because systemd itself will not do the redirect for the echo
command.
And because things can’t be more easy with systemd, I need another set of timer/service combination for dimming the display down. brightness-down.timer
:
[Timer]
OnCalendar=*-*-* 23,00..05:10:00
Persistent=false
Unit=brightness-up.service
[Install]
WantedBy=timers.target
And brightness-down.service
:
[Unit]
Description=Decrease display brightness
[Service]
Type=oneshot
ExecStart=/bin/sh -c '/bin/echo 15 > /sys/class/backlight/rpi_backlight/brightness'
TimeoutStopSec=30
KillMode=none
RemainAfterExit=no
User=root
Group=root
Now, everything I do with this Raspberry is automated, and I install the Pi using Ansible. Therefore I added the following code to my Playbook:
|
|
The first Task uploads all 4 files to /etc/systemd/system/
, and the block only runs when anything changed. In such a case the systemd daemon is reloaded, and the two services are re-registered.