# Display backlight

<abstract>
This article explain how set the brightness level of the LCD backlight
</abstract>

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 

<abstract>
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
</abstract>

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


@include='adv_cmpanel' 



	
