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
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?