PWM affected by wxPython

Hi all,
now I have a real problem.
On RPi I use my application with 8 intruments on Panel (and on Frame)

I want to use PWM output provided by:

import RPi.GPIO as GPIO

then using this code:

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
pi_pwm=GPIO.PWM(12,1000) # Hz
pi_pwm.start(50) #duty 50%

does not make a nice/clean 1kHz tone on pin 12, but some “noise”
when I do not run wxApp the sound is clean !?

Does anybody know what is going on? Why PWM is affected by wx?

Hi,
Do you have a basic demo code reproducing the problem ?
Nicolas

The problem is that even RPi4 is not powerful enough not to be affected by running wxPython GUI

Finally I managed to use real hw pwm output

Just for interest:I have loop 0.5sec for gui update, rpi4 can do it but rpi1 /one/ needs about 4 seconds, RAM is not limiting factor

From what you say, I believe you are on a bad track. GUI must run in a thread and “work” be done in another one (or another process).

Thank you for answer, but at the same time I have to confess myself I am mostly embedded so I am closer to assembler and ANSI C but not to the OOP :slight_smile:

This is my first apk in OOP (Python) and target is RaspberryPi4, I write and test code on PC Python, then just transfering file to RPi.

I think I feel what you write about threading - probably different priority of different piece of code. Like interrupts in embedded.

My code now looks like this:

class MyCode(wx.Panel):
....
....
....
     def on_timer(self):
              mycode executed repeatedly is here
              wx.CallLater(500, self.on_timer)

Now looking at my code I am horrified: Would this calling its own function make stack pointer to overflow in time? I think it is also called “RAM leaking” ? (this on_timer() is inside the only one class I have)

I had before something like this (I had to find it again, it looks really to be this):

def startTimer():
    threading.Timer(interval, startTimer).start()
    myPeriodicFunction()

But this was only running on PC and on RPi it was crashing!? So I abandoned threading.

Can you pls give me a direction what to do where to find a good explanation how can I make “stupid” loop in time intervals?

I read and tried understand what exactly CallLater does. It is written this:

CallLater maintains references to its instances while they are running. When they finish, the internal reference is deleted and the GC is free to collect naturally.

I do not completely understand this because no idea what is GC?
But it lloks like that CallLater only sets up somewhere in wxTimer some flag to call function later? So if this is the case there is no threat of RAM leaking?

“GC” refer’s to Python’s Garbage Collector.

Yes, wx.CallLater is a timer with a more convenient usage in some cases.

Thank you very much!

Hello. Sorry for my bad English. I’m new in Raspberry and python programming and trying to use wx graphical widgets in my python script and use this script to run on startup on RPi 4. the problem is that i should to activate virtenv whenever i wanna run my script and without it, i get the error that the module couldn’t be found. Do u have any suggestion?
Thanks

Create a shell script that activates the virtual env and then runs python, something like this:

source <path to the virtualenv>/bin/activate
python my_wx_app.py

Or you should be able to skip a step and explicitly use that python to run your script, like this:

<path to the virtualenv>/bin/python my_wx_app.py

P.S. It would have been better to make a new topic for this, since it’s a new question and only mildly related to this current topic.

thx very much for the solution but i’m getting the same error as i tried to run terminal script in my python code. source: not found
but the command works prefectly when i write it directly in the terminal