Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

Loops for Everything ?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Loops for Everything ?

by hitsware2 on Thu Mar 11, 2021 5:02 pm

I would like to simply set a frequency and pulsewidth .
All the PWM examples use a loop to control one and / or the other .
Is there a simpler way to output a constant PWM signal ,
without the ' while True : ' ?
Code: Select all | TOGGLE FULL SIZE
"""CircuitPython Essentials: PWM with Fixed Frequency example."""
import time
import board
import pwmio
 
# LED setup for most CircuitPython boards:
led = pwmio.PWMOut(board.D13, frequency=5000, duty_cycle=0)
# LED setup for QT Py M0:
# led = pwmio.PWMOut(board.SCK, frequency=5000, duty_cycle=0)
 
while True:
    for i in range(100):
        # PWM LED up and down
        if i < 50:
            led.duty_cycle = int(i * 2 * 65535 / 100)  # Up
        else:
            led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100)  # Down
        time.sleep(0.01)

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Re: Loops for Everything ?

by dastels on Thu Mar 11, 2021 6:51 pm

The duty cycle is changed in the loop in that example only to fade the brightness of the LED up & down over time. Once you set the frequency and/or duty cycle of a PWM it keeps going until you change it.

Dave

dastels
 
Posts: 6094
Joined: Tue Oct 20, 2015 3:22 pm

Re: Loops for Everything ?

by hitsware2 on Thu Mar 11, 2021 7:07 pm

How do I set it without a loop ?
No output with this :
Code: Select all | TOGGLE FULL SIZE
import board; import pwmio
pwmio.PWMOut(board.A2, duty_cycle=30000, frequency=1000)

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Re: Loops for Everything ?

by dastels on Thu Mar 11, 2021 8:28 pm

That looks right.

Make sure that A2 is a PWM capable pin.

Dave

dastels
 
Posts: 6094
Joined: Tue Oct 20, 2015 3:22 pm

Re: Loops for Everything ?

by hitsware2 on Thu Mar 11, 2021 9:27 pm

With this there is output on A2
Code: Select all | TOGGLE FULL SIZE
import board; import pwmio
n = pwmio.PWMOut(board.A2, duty_cycle=30000, frequency=1000)
while True:
  n.duty_cycle = 30000

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Re: Loops for Everything ?

by dastels on Thu Mar 11, 2021 9:42 pm

Ah! What if you try:
Code: Select all | TOGGLE FULL SIZE
import board
import pwmio
n = pwmio.PWMOut(board.A2, duty_cycle=30000, frequency=1000)
while True:
  pass

The PWM is most likely shut down when the program ends.

Dave

dastels
 
Posts: 6094
Joined: Tue Oct 20, 2015 3:22 pm

Re: Loops for Everything ?

by hitsware2 on Thu Mar 11, 2021 10:26 pm

[code}
import board; import pwmio
pwmio.PWMOut(board.A2, duty_cycle=30000, frequency=1000)
while True: pass
[/code]
Thank You !
I ' ll dance at Your wedding : )

hitsware2
 
Posts: 44
Joined: Fri Sep 11, 2020 9:54 pm

Please be positive and constructive with your questions and comments.