Python Script to Control Brightness

The forum area for anything related to the MakerPlane Sunlight Readable Displays.

Moderator: John Nicol

Post Reply
User avatar
John Nicol
Posts: 611
Joined: 23 Nov 2011, 12:17
Contact:

Python Script to Control Brightness

Post by John Nicol »

If you would like to control brightness of the screen using a Python script, user Steve M has put a script together to control brightness using touch screen buttons. Here is his email to me reproduced for you:

=======

Hey John,
I could not use the PWM brightness commands on your website in Ubuntu, it seems the wiringPi lib on which the command line gpio commands are based has been deprecated. I was however able to control it using the python RPi.GPIO library. Other users might find that useful. Simple program below

Brightness.py:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm=GPIO.PWM(18, 100)
pwm.start(0)

for dc in range(0,110,10):
        print dc
        pwm.ChangeDutyCycle(100-dc)
        time.sleep(1)

pwm.stop()
GPIO.cleanup()
--
-----
John Nicol
Founder, MakerPlane
zavracky
Posts: 8
Joined: 20 Dec 2020, 23:02

Re: Python Script to Control Brightness

Post by zavracky »

All,

I have been working with the display and my python software for several weeks. What I learned about Python GPIO library is that the PWM is not hardware, even when using the only PWM on the processor. Try this to verify: instead of pwm=GPIO.PWM(18, 100) try testing pwm=GPIO.PWM(17, 100) with your scope. I found that both 18 and 17 work the same. Hmmm. I believe that suggests that they are using software instead of hardware. But what makes this bad is that with my code and a bunch of interrupts, this software PWM doesn't work well. The result is a display that does not have constant brightness.

To fix this, I import the OS library:

import os

Then I use the OS commaneds:

# Global Variables

#Set up Brigntness Controls
brightnessLevels = [1,2,3,4,5,7,10,15,20,30,50,70,100] # control the percentage of on time for the PWM
numBrightLevels = 13 # to add or remove levels, this number and the number below must be changed
brightness = 13 # this variable keeps track of the brightness setting, eg: brightnessLevels[13] or 100%

#the set up below insures we are using a hardware pwm, not software as may be happening with python (web references discovered)
os.system("gpio -g mode 18 pwm") #set pin 12 (gpio 18) for pulse width modulation
os.system("gpio pwmc 1000") # set the frequency for 1000Hz
os.system("gpio -g pwm 18 0") # set up for maximum brightness

#Routine to change brightness
def setDisplayBrightness(brightness):
global brightnessLevels
#pwm.ChangeDutyCycle(100-brightnessLevels[brightness-1])
s = "gpio -g pwm 18 " + str(1024 - (brightnessLevels[brightness-1])*10.24) # convert percentage brightness to 10 bit binary number
os.system(s)
return()

This actually uses the built in PWM in the background - does not experience delays from your software.

Good Luck,

Paul
stevem
Posts: 1
Joined: 09 Oct 2020, 09:48

Re: Python Script to Control Brightness

Post by stevem »

Hi Paul,

I tried this on a Raspberry Pi 3B running Ubuntu, unfortunately it doesn't work for me. I guess my kernel does not support wiringPi. I get the following message:

- expecting BCM2708 or BCM2709.
If this is a genuine Raspberry Pi then please report this
to projects@drogon.net. If this is not a Raspberry Pi then you
are on your own as wiringPi is designed to support the
Raspberry Pi ONLY.

Further investigation shows the wiringPi library has been deprecated :(
http://wiringpi.com/wiringpi-deprecated/

Steve
zavracky
Posts: 8
Joined: 20 Dec 2020, 23:02

Re: Python Script to Control Brightness

Post by zavracky »

Steve,

So sorry for not responding sooner. My bad.

I have never tried to use Ubuntu and have no experience. I am sorry this doesn't work for you, but can say that it does work soooo much better than GPIO on my pi with Raspbian. It is true that the software I have written uses timers and interrupts which may effect the smooth operation of Pi's GPIO software PWM??? I would be curious to know if you are observing any flicker when you decrease the brightness.

Best,

Paul
Post Reply