Jakob Staudinger
Posts: 13
Joined: Tue Nov 26, 2013 3:20 pm

Frquency for GPIO.PWM without flickering

Mon Jun 23, 2014 2:45 pm

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?

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Frquency for GPIO.PWM without flickering

Mon Jun 23, 2014 2:52 pm

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).

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: Frquency for GPIO.PWM without flickering

Mon Jun 23, 2014 3:32 pm

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) .

Jakob Staudinger
Posts: 13
Joined: Tue Nov 26, 2013 3:20 pm

Re: Frquency for GPIO.PWM without flickering

Mon Jun 23, 2014 3:34 pm

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.

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Frquency for GPIO.PWM without flickering

Mon Jun 23, 2014 3:38 pm

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.

Jakob Staudinger
Posts: 13
Joined: Tue Nov 26, 2013 3:20 pm

Re: Frquency for GPIO.PWM without flickering

Mon Jun 23, 2014 3:39 pm

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.

Jakob Staudinger
Posts: 13
Joined: Tue Nov 26, 2013 3:20 pm

Re: Frquency for GPIO.PWM without flickering

Mon Jun 23, 2014 3:59 pm

Remove....
Last edited by Jakob Staudinger on Mon Jun 23, 2014 4:32 pm, edited 1 time in total.

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Frquency for GPIO.PWM without flickering

Mon Jun 23, 2014 4:03 pm

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

Jakob Staudinger
Posts: 13
Joined: Tue Nov 26, 2013 3:20 pm

Re: Frquency for GPIO.PWM without flickering

Mon Jun 23, 2014 4:41 pm

Thanks, that finally works.

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Frquency for GPIO.PWM without flickering

Mon Jun 23, 2014 4:46 pm

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.

Return to “Python”