You can use your Slider Trinkey as a monitor brightness controller! You can control the brightness of your monitor on any computer running Windows.

This example requires you to run Python code on your computer, and CircuitPython code on your Slider Trinkey.

Python Library Installation

To use this example, you need to install two Python libraries on your computer: screen brightness control and pyserial.

Run the following command in your terminal:

pip install screen-brightness-control pyserial

Depending on your setup, you may need to use pip3 in place of pip!

CircuitPython Code

Save the following as code.py on your CIRCUITPY drive.

Click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.

Your CIRCUITPY drive should now look similar to the following image:

CIRCUITPY
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import time
import board
from analogio import AnalogIn
import adafruit_simplemath

analog_in = AnalogIn(board.POTENTIOMETER)


def read_pot(samples, min_val, max_val):
    sum_samples = 0
    for _ in range(samples):
        sum_samples += analog_in.value
    sum_samples /= samples  # ok take the average

    return adafruit_simplemath.map_range(sum_samples, 100, 65535, min_val, max_val)


while True:
    print("Slider:", round(read_pot(10, 0, 100)))
    time.sleep(0.1)

Python Code

Save the following to your computer wherever is convenient for you.

# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
Slider Trinkey Monitor Brightness Demo for Windows
(Requires Hue and Monitor Brightness CircuitPython example to be running on the Slider Trinkey)
"""
import sys
import screen_brightness_control as sbc
import serial
from serial.tools import list_ports

slider_trinkey_port = None
ports = list_ports.comports(include_links=False)
for p in ports:
    if p.pid is not None:
        print("Port:", p.device, "-", hex(p.pid), end="\t")
        if p.pid == 0x8102:
            slider_trinkey_port = p
            print("Found Slider Trinkey!")
            trinkey = serial.Serial(p.device)
            break
else:
    print("Did not find Slider Trinkey port :(")
    sys.exit()

curr_brightness = sbc.get_brightness()

while True:
    x = trinkey.readline().decode('utf-8')
    if not x.startswith("Slider: "):
        continue

    val = int(float(x.split(": ")[1]))

    if val != curr_brightness:
        print("Setting brightness to:", val)
        sbc.set_brightness(val)
        curr_brightness = sbc.get_brightness()

Usage

Once you have the CircuitPython code running on the Slider Trinkey, you'll want to run the Python script from the same computer you have the Slider Trinkey plugged into with the following command in your terminal from within the same directory as the Python script:

python Monitor_Brightness_Python_code.py

Depending on your computer setup, you may need to substitute python3 for python. If you want to run the command from a different directory, include the path to the file with the filename.

Once it's running, move the Slider Trinkey to control the brightness of your monitor.

That's all there is to controlling the brightness of your monitor using Python, CircuitPython and Slider Trinkey!

This guide was first published on Jun 16, 2021. It was last updated on Mar 29, 2024.

This page (Monitor Brightness) was last updated on Mar 29, 2024.

Text editor powered by tinymce.