Page 1 of 1

Frquency for GPIO.PWM without flickering

Posted: Mon Jun 23, 2014 2:45 pm
by Jakob Staudinger
Using some transistors, I connected LEDs to 3 GPIOs.

I wanted to dim them using GPIO.PWM, but I get irregular flickering from time to time.

Which frequencies are suitable to avoid flickering?

Re: Frquency for GPIO.PWM without flickering

Posted: Mon Jun 23, 2014 2:52 pm
by joan
Jakob Staudinger wrote:Using some transistors, I connected LEDs to 3 GPIOs.

I wanted to dim them using GPIO.PWM, but I get irregular flickering from time to time.

Which frequencies are suitable to avoid flickering?
By GPIO.PWM do you mean the RPi.GPIO Python module?

If that's the case you will always get flickering as it uses software timed PWM.

If you don't want flickering you'll have to buy extra hardware or use one of the Python modules which use hardware timing (RPIO.GPIO or pigpio).

Re: Frquency for GPIO.PWM without flickering

Posted: Mon Jun 23, 2014 3:32 pm
by Gert van Loo
Use C-code.
I tried python for a LED matrix and had the same experience you described: flickering.
Going to C solved all my problems (At least 100x faster) .

Re: Frquency for GPIO.PWM without flickering

Posted: Mon Jun 23, 2014 3:34 pm
by Jakob Staudinger
joan wrote:RPIO.GPIO
Even more flickering.

Gert van Loo wrote:Use C-code.
I tried python for a LED matrix and had the same experience you described: flickering.
Going to C solved all my problems (At least 100x faster) .
Please share an example to get started quickly.

Re: Frquency for GPIO.PWM without flickering

Posted: Mon Jun 23, 2014 3:38 pm
by joan
Gert's C code will not be as good as using a Python module which uses hardware timing. It will still flicker for the same reason, it will use software timing.

Re: Frquency for GPIO.PWM without flickering

Posted: Mon Jun 23, 2014 3:39 pm
by Jakob Staudinger
Currently I have

Code: Select all

#!/usr/bin/env python
 
# import RPi.GPIO as GPIO, feedparser, time, sys, random, RPIO.PWM
import feedparser, time, sys, random
from RPIO import PWM

PWM.setup()
PWM.init_channel(0)
              
while True:  
  R = random.randrange(0, 255,1)
  G = random.randrange(0, 255,1)
  B = random.randrange(0, 255,1)
  PWM.add_channel_pulse(0, 17, R, 255)
  PWM.add_channel_pulse(0, 22, B, 255)
  PWM.add_channel_pulse(0, 25, G, 255)       
  time.sleep(2)

# Shutdown all PWM and DMA activity    
PWM.clear_channel_gpio(0, 17)
PWM.clear_channel_gpio(0, 22)
PWM.clear_channel_gpio(0, 25)  
PWM.cleanup()
using an RGB-LED.

And it flickers even worse than before.

Re: Frquency for GPIO.PWM without flickering

Posted: Mon Jun 23, 2014 3:59 pm
by Jakob Staudinger
Remove....

Re: Frquency for GPIO.PWM without flickering

Posted: Mon Jun 23, 2014 4:03 pm
by joan
Perhaps you have a separate problem. RPIO should work.

pigpio version.

Code: Select all

#!/usr/bin/env python

import time, sys, random

import pigpio

pi = pigpio.pi() # connect to local Pi

start_time = time.time()

while (time.time()-start_time) < 60: 
   R = random.randrange(0, 255, 1)
   G = random.randrange(0, 255, 1)
   B = random.randrange(0, 255, 1)

   pi.set_PWM_dutycycle(17, R)
   pi.set_PWM_dutycycle(22, B)
   pi.set_PWM_dutycycle(25, G)

   time.sleep(2)

pi.stop()
Video (webm). http://abyz.co.uk/rpi/misc/jakob.webm

Re: Frquency for GPIO.PWM without flickering

Posted: Mon Jun 23, 2014 4:41 pm
by Jakob Staudinger
Thanks, that finally works.

Re: Frquency for GPIO.PWM without flickering

Posted: Mon Jun 23, 2014 4:46 pm
by joan
I don't understand why RPIO doesn't appear to work. It uses the same technology.

Are you using sound at the same time? The hardware timing is done using DMA paced by the PWM or PCM "peripherals". This usage conflicts with sound modules using the same peripherals at the same time.