Changing PWM period

When I have a pin configured as PWM_OUT, I can easily change the duty doing pin.write(newDuty);

However, I don’t see a way of changing the period unless I call configure again, but configure doesn’t seem to do it properly when doing it very fast.

To test my assumption (although I’m not changing the period in this tests):

pin.configure(PWM_OUT, period, 0); for (local j = 0; j < 10; j += 1) { pin.write(j * 0.1); imp.sleep(0.00001); }
…I get a very nice PWM with incrementing duty cycle. But if I do

for (local j = 0; j < 10; j += 1) { pin.configure(PWM_OUT, period, j * 0.1); imp.sleep(0.00001); }
…the result is totally different, although I’m supposedly doing the same.

I assume configure does much more than changing period and duty cycle, which is expected, but it will be very nice to be able to change period without having to call configure. Am I missing something?

I’m trying to drive a speaker with this, and being able to change period fast is essential to do this :slight_smile:

Thank you!

Probably depends on your period. Reconfiguring almost certainly (I’ve not checked) resets the PWM block and starts again; writing the period updates the duty cycle on the next cycle.

Without knowing your period I can’t tell what the effect might be given your sleep value.

Yeah, that might explain the clicks I’m getting. I’m using periods of 1/440 or 1/480 for example (for musical notes), but I’m not changing the duty cycle. Even with sleeps of 0.25, I still hear the clicks when changing the period.

Is there any way of knowing when the pulse is going to start, or to change that period without affecting the current pulse?

I’ll take that as a ‘no’.

What are the chances of adding something to change the frequency like what we have to change the duty?

It’s possible, I’ll file a feature request.

Awesome! Let me know if there is a place where I can watch that feature request (not sure if you have a public JIRA or similar).

Thank you!

We don’t have a public jira I’m afraid, but it’s something that has been discussed. For now it’s just me typing on the forums :wink: