This is my attempt to do LED fading with my RPI using software PWM:
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
# set up GPIO pin for output
GPIOPin = 7
GPIO.setup(GPIOPin, GPIO.OUT)
# initialize PWM variable
RPWM = GPIO.PWM(GPIOPin, 100)
# start LED fading
RPWM.start(0)
try:
while 1:
for dc in range(0, 101, 5):
RPWM.ChangeDutyCycle(dc)
time.sleep(0.1)
for dc in range(100, -1, -5):
RPWM.ChangeDutyCycle(dc)
time.sleep(0.1)
except KeyboardInterrupt:
pass
RPWM.stop()
GPIO.cleanup()
But I would like to do logarithmic fading so make the fading process appear linear to the human eye.
So I found something like y = pow(2, log2(b) * (x+1) / a) - 1
with a
as the number of steps and b
as the resolution of the pwm.
But I think that is not the correct thing to use.