"auto-brightness" aka ambient light sensor in Linux? Details?

Hi all, I installed Arch (Endeavour) on my Framework with KDE and so far everything is running pretty smoothly.
One of the few details I have not figured out is the ambient light sensor. I have seen texts about other Linux Distros that seem to imply that automatic brightness might work there out of the box but nowhere could I find any details about how the sensor or automatic brightness adjustment can be used. I also could not find any system setting that refers to this function.

Is anyone here able to shed some light on that ambient sensor topic? :slight_smile: Where would I look for technical information enabling me to use it?

1 Like

Don’t tell the other Arch people that you’re referring to Endeavour as “Arch”… :stuck_out_tongue:

(I’m fine with it, one of my machine uses Endeavour, but you know how the joke goes…) Anyway:

I’ll assume you’ve started here: Framework Laptop - ArchWiki

I found things like this on the AUR, but given the age of it I wouldn’t suggest using it, but might give you a place to start: AUR (en) - autolight

I found other mentions of Gnome-related packages here: Lenovo ThinkPad Yoga 370 - ArchWiki

My main suggestion might be to figure out which light sensor the device is using, and then start googling from there.

Edit: here’s a KDE-related bash script that might give you some ideas for what you need, if it still works: https://store.kde.org/p/1259771/

(Unfortunately I don’t have KDE on any of my systems, I’m mostly a tiling VM kind of person, and have never bothered with ambient sensors so best I can do is offer these hints on starting points. But given this, that last bash-based solution seems like something up my alley for when I throw some BSD and BSPWM onto the Framework I’m waiting for. :smiley: )

I don’t think that one is KDE specific, it is Intel specific. It does seem to mostly work.
It assigns a brightness variable from 1,000 to 100,000 to the brightness file.
*9,600 to 96,000

cat /sys/class/backlight/intel_backlight/brightness

By reading the ambient light sensor.

cat /sys/bus/iio/devices/iio:device0/in_illuminance_raw

The ambient light sensor on the Framework seems to match 1:1 with the raw illuminance values, though the raw illuminance can detect values over 100% brightness.

A glitch in the script causes it to go full brightness in dark rooms, and when fixing the glitch it looks like the code was attempting to compensate for the monitor as a light source.

Edit:

This will need to be run as ‘sudo’. It works on Kubuntu 21.10.

#!/bin/bash
max=$(cat /sys/class/backlight/intel_backlight/max_brightness)
sensitivity=$((max/10))
min=$((max/10))
delay=6
while [ 1 ]
do
updated=1
while [ $updated -gt 0 ]
do
updated=0
backlight=$(cat /sys/class/backlight/intel_backlight/brightness)
sensor=$(cat /sys/bus/iio/devices/iio:device0/in_illuminance_raw)
target=$sensor
if [ $backlight -gt $sensor ]
then
if [ $(($backlight - $sensor)) -gt $sensitivity ]
then
updated=1
fi
fi
if [ $backlight -lt $sensor ]
then
if [ $(($backlight - $sensor)) -lt $sensitivity ]
then
updated=1
fi
fi
if [ $target -gt $max ]
then
target=$max
fi
if [ $target -lt 1 ]
then
target=$min
fi
if [ $updated -gt 0 ]
then
echo “Starting brightness: $backlight”
echo “Ambient light: $sensor”
echo “Adjusted brightness: $target”
echo “-------------------------------”
echo “Sensitivity: $sensitivity”
echo “Min: $min Max: $max”
echo
echo $target > /sys/class/backlight/intel_backlight/brightness
fi
done
sleep $delay
done

4 Likes

Thanks a lot, this was exactly the sort of information I was looking for. Great! :smiley:

1 Like

Is there a way to disable this auto brightness perhaps?

Edit, immediately after that I found it: How to Turn Off Automatic Brightness on Ubuntu Linux

On that itsfoss link, what is running that page. Each time I open that page my browser locks after using an additional 1GB of RAM for that page.

1 Like

Hi. The ambient light sensor works by default on Fedora, but on Arch does not. Has anyone had a success with making the auto brightness regulation work in Arch? What type/model of ambient light sensor is used in Framework laptop by the way? I found nothing on ArchWiki about that, tried Autolight from AUR which also doesn’t work. Please advise.
Update. Fonund the solution that worked for me by running the bash script from that guy:

Upd2: The script launches but works kind of weird, seems not regulating the brightness in dependence with the ambient light.

Ok, finally made it work. Replaced the original AmbientLightFix_intel code with the provided by Monster_user above using vim. Thanks for that! Then ran sudo AmbientLightFix_RunME.sh and the screen brightness auto regulation has started working as a service even after reboot.

Hi there,
Here is a smooth integration to kubuntu 21.10.
I preferred to use systemd to integrate the script to the system and give it root privileges.

Create the script file:

sudo nano /etc/pm/power.d/10_framework.automatic-brightness

Paste there the code from Monster_user above.

Make it executable

sudo chmod 744 10_framework.automatic-brightness

Create the systemd service creation

sudo nano /lib/systemd/system/framework-automatic-brightness.service

Paste this in:

[Unit]
Description=Automatic brightness adjustment based on ambient sensor for Framework laptops (tested on Kubuntu 21.10)
After=systemd-user-sessions.service systemd-logind.service

[Service]
# User=spark
# Type=simple
# PIDFile=/run/my-service.pid
ExecStart=/etc/pm/power.d/10_framework.automatic-brightness start
Restart=on-failure
RestartSec=30
StartLimitInterval=350
StartLimitBurst=10

[Install]
WantedBy=multi-user.target

Enable the service

sudo systemctl enable framework-automatic-brightness.service

1 Like

@Monster_user, do you know if there is a way to slow down the backlight transition rate?
Is there a command in the library to have a rate of change that is less abrupt?
Cheers

Hey, my previous laptop is a ThinkPad Yoga 370!

The ambient light sensor in that thinkpad and also the framework does work out of the box indeed on Gnome at least. Even back in 2017. I disabled it as quickly as I could because it’s annoying. Shadow of your head moves across the sensor? Brightness does down and up again. And not as smoothly as you might like.

peter@raamwerk ~> monitor-sensor 
    Waiting for iio-sensor-proxy to appear
+++ iio-sensor-proxy appeared
=== No accelerometer
=== Has ambient light sensor (value: 80,069000, unit: lux)
=== No proximity sensor
    Light changed: 80,016000 (lux)
    Light changed: 76,273000 (lux)
    Light changed: 80,123000 (lux)
    Light changed: 80,069000 (lux)
    Light changed: 79,807000 (lux)
    Light changed: 77,801000 (lux)
    Light changed: 80,278000 (lux)
    Light changed: 79,910000 (lux)

I assume monitor-sensor is a part of iio-sensor-proxy (which is not a gnome package and doesn’t have any gnome dependencies).

The wiki does say:

To make this devices work with GNOME you need to install iio-sensor-proxy package.

Which implies Gnome might be the only DE that supports ambient light sensors trough iio maybe? That line of text might also be outdated. If other desktops support it, then I expect that to just work if iio-sensor-proxy is installed. And even if your desktop doesn’t support it you could install it anyway so you can at least verify the sensor works with monitor-sensor.