CM Panel technical documentation Buy

Display backlight

This article explain how set the brightness level of the LCD backlight

GPIO 22 is used to control the LCD backlight brigthness

Install the pigpio daemon and python library by typing:

sudo apt update
sudo apt install pigpio python3-pigpio

Start the pigpiod daemon:

sudo systemctl enable pigpiod
sudo systemctl start pigpiod

Try id it works using this Python code:

#!/usr/bin/python

import sys
import pigpio

# Check if exist an arg from command line
if len(sys.argv) != 2:
    print("Usage: python brightness.py <percentage>")
    sys.exit(1) 

# Get the percentage
percentage = int(sys.argv[1])

# Check the value range (0-100)
if percentage < 0 or percentage > 100:
    print("Percentage must be between 0 and 100")
    sys.exit(1)

pi = pigpio.pi()

if not pi.connected:
    print("Connection error with pigpiod")
    exit()

gpio_pin = 22
frequency = 800  # Hz

# Convert the percentage in the duty cycle value
# 0% -> duty 255
# 100% -> duty cycle = 0

dutycycle = 255 - int(255 * (percentage / 100.0))

print("gpio=%d, frequency=%d Hz, percentage=%d%%, dutycycle=%d" % (gpio_pin, frequency, percentage, dutycycle))

pi.set_mode(gpio_pin, pigpio.OUTPUT)
pi.set_PWM_frequency(gpio_pin, frequency)
pi.set_PWM_dutycycle(gpio_pin, dutycycle)

and run it:

$ chmod +x brightness.py
$ ./brightness.py 0
$ ./brightness.py 50
$ ./brightness.py 100

Automatic brightness adjustment

Let's see how to automatically dim the screen when it is not in use and how to increase the brightness every time the screen is touched

Install the python3-evdev libraries:

sudo apt update
sudo apt install python3-evdev

Save this code in /home/pi/auto-brightness.py:

#!/usr/bin/python

import sys
import pigpio
import time
import threading
from evdev import InputDevice, ecodes

timer = 0
lock = threading.Lock()

pi = pigpio.pi()
if not pi.connected:
    print("pigpiod daemon not found")
    exit()

def backlight(percentage):
    gpio_pin = 22
    frequency = 800  # Hz

    # Check the value range (0-100)
    if percentage < 0 or percentage > 100:
        return

    # Convert the percentage into a duty cycle value
    # 0% -> duty cycle 255
    # 100% -> duty cycle = 0
    dutycycle = 255 - int(255 * (percentage / 100.0))

    pi.set_mode(gpio_pin, pigpio.OUTPUT)
    pi.set_PWM_frequency(gpio_pin, frequency)
    pi.set_PWM_dutycycle(gpio_pin, dutycycle)

def handle_events(device):
    global timer
    for event in device.read_loop():
        if event.type == ecodes.EV_ABS:
            backlight(100)
            with lock:
                timer = 0

backlight(100)

device = InputDevice('/dev/input/event0')

# Create a thread that will execute the handle_events function
event_thread = threading.Thread(target=handle_events, args=(device,))
event_thread.start()

try:
    while True:
        with lock:
            timer += 1
            #print(timer)

        if timer == 60:
            backlight(30)

        threading.Event().wait(1)
except KeyboardInterrupt:
    print("Interrupted by the user")

Edit this file:

sudo nano /etc/systemd/system/auto-brightness.service

and insert this definition:

[Unit]
Description=Auto-Brightness
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/auto-brightness.py
User=pi
WorkingDirectory=/home/pi/
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target       

Enable and start the daemon by typing:

sudo systemctl enable auto-brightness
sudo systemctl start auto-brightness

Products related

CM-Panel-Basic

Features Index Buy

All-in-one 7 inch touch WiFi terminal powered by Raspberry Pi CM4S
  • 7 inch TFT display 800x480
  • Capacitive touch
  • MIPI Camera connector
  • WiFi @ 2.4 GHz
CM-Panel-POE

Features Index Buy

All-in-one 7 inch touch POE terminal powered by Raspberry Pi CM4S
  • 7 inch TFT display 800x480 pixel
  • Capacitive touch
  • Embedded micro UPS for safe shutdown
  • Power Over Ethernet @ 10/100 Mbit
  • Hi-resolution audio up to 384KHz@32bit
  • Real Time Clock with backup battery
  • 3 USB Host port
  • 1 RS485/422/RS232 port
  • 1 Relay
  • MIPI Camera connector
  • WiFi @ 2.4 GHz (optional)
CM-Home

Features Index Buy

Home Automation board powered by Raspberry Pi CM4S lite module
  • Power-in @ 12 to 24VDC
  • Double opto-isolated RS-485 ports
  • 2 relays
  • 2 USB 2.0 host port
  • Ethernet port at 10/100Mbit
  • MIPI Camera connector
  • Stereo audio out on 3.5 mm jack

Home page CM Panel technical documentation Buy