0

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.

user3142695
  • 15,844
  • 47
  • 176
  • 332

1 Answers1

0

main problem is you only have 100 fading steps - thats not even 8bit (256 steps).

As I'm a beginner I tried this:

i=[0,1,2,3,5,7,10,15,22,31,44,63,90,100] //14 pseudo logarithmic steps
x=0; //index of steps 


try:
while 1:

for dc in range(0, 13, 1): // 14 steps


        p.ChangeDutyCycle(i[x])
        x=x+1;
        time.sleep(0.1)

I made 16bit fading in C before. There is an article about that issue but its in German: LED PWM fading