I am using Pi Zero W with latest Raspberry Pi OS.
I have a backlight of an LCD that I am trying to control via PWM.
If I use RPi.GPIO I get flicker at anything other that 100% (or 0%) duty cycle. As I understand, this is probably due to the timing of the software PWM being affected by other processes.
If I use pigpio, I get good dimming but as soon as I turn the backlight off and back on, it breaks the dimming and I only get full-on or off. Only a reboot fixes this.
The backlight goes off even if I set the DC to 1
(/255) not only to zero , which is also strange and in either case, as mentioned, it breaks the dimming until reboot.
The backlight also automatically comes on as I have it in an overlay for the screen, here is the extract from the dts file. If I comment out that fragment, then I get no dimming at all, just on/off from the start. Also, default-off
doesn't change anything.
fragment@5 {
target-path = "/";
__overlay__ {
rpi_backlight: rpi_backlight {
compatible = "gpio-backlight";
gpios = <&gpio 18 0>;
default-on;
};
};
};
Ideally, I would remove that fragment and only control the backlight from Python and ideally without a daemon consuming resources.
How can I fix pigpio so that I can turn the backlight off and then get it dimming again (ideally without DTS fragment above or with it defaulting to off)?
pigpio daemon is going to continually use resources just for a backlight, is there a fix for RPi.GPIO so that I can use the hardware PWM (since I am using GPIO 18 which has that capability, as I understand) - also ideally without
fragment@5
Edit: This is the python script that dims the backlight up and down.
$ sudo pigpiod
$ python3
import pigpio
import time
gpio = pigpio.pi()
gpio.set_mode(18, pigpio.OUTPUT)
#gpio.set_mode(18, pigpio.ALT5) - behaves the same
def dim_up():
for x in range(2,200):
gpio.set_PWM_dutycycle(18, x)
print('level =', x)
time.sleep(0.01)
def dim_down():
for x in range(200,1,-1):
gpio.set_PWM_dutycycle(18, x)
print('level =', x)
time.sleep(0.01)
def cycle_dim():
while True:
dim_up()
dim_down()
cycle_dim()
This works fine and dims up and down smoothly until you change either range
to include 1
or 0
in the range. Then, the first time before it hits those numbers, it works fine, then after those numbers it is either full on or full off, no range in between.