I am using a new Raspberry Pi Touch Display 2 with 5 inch screen connected to a Raspberry Pi 3B.

I want to be able to control the brightness and the way to do this is to write a brightness value to a file.

To check the device I looked here:

ls -l /sys/class/backlight

To check the brightness and max_brightness, I looked here:

cat /sys/class/backlight/10-0045/brightness
cat /sys/class/backlight/10-0045/max_brightness

We can see that the max_brightness is 31 so this used a 5 bit scale and 50% brightness would be value 16.

I want to be able to set the brightness conveniently and the following bash script does exactly that:

nano bright.sh
#!/bin/bash
# Set Raspberry Pi 5" Touch 2 Display brightness (0–100 % → 0–31 raw)

DEV="/sys/class/backlight/10-0045"
MAX=$(<"$DEV/max_brightness") # should be 31
PCT=$1

# check input
if [ -z "$PCT" ]; then
echo "Usage: $0 <brightness 0-100>"
exit 1
fi

if ! [[ "$PCT" =~ ^[0-9]+$ ]]; then
echo "Error: argument must be an integer 0–100"
exit 1
fi

if [ "$PCT" -lt 0 ] || [ "$PCT" -gt 100 ]; then
echo "Error: brightness must be 0–100"
exit 1
fi

# scale percentage to raw value
VAL=$(( PCT * MAX / 100 ))

echo "Setting brightness to $PCT% (raw $VAL/$MAX)..."
echo "$VAL" | sudo tee "$DEV/brightness" >/dev/null

Save the file.

Make it executable

chmod +x bright.sh

Run it with:

bash bright.sh 30

This will set the display to a brightness of 30.