micropython icon indicating copy to clipboard operation
micropython copied to clipboard

set_analog_period changes PWM period for all PWM pins.

Open azamat-bagatov opened this issue 6 years ago • 3 comments

I'm making a synthesizer project, and for it I need to have several different PWM signlas. The problem is when you call set_analog_period() on one of the pins it changes it for all PWM pins. Here's an example of the code, that I used to track this:

` pin0.set_analog_period(2) pin0.write_analog(250) pin1.set_analog_period(3) pin1.write_analog(250) val = 2

while True: val = 5-val pin1.set_analog_period(val) pin1.write_analog(250) sleep(2000) `

The output on pin0 and pin1 in this case is identical, although I only call set_analog_period() on pin1.

If that's not the issue, and you can only change global period, I think this should be highlighted in the documents, as it's far from obvious since it's a pin's method and not microbit's. In this case, is there a way to have several pwm signals with different periods?

Thanks!

azamat-bagatov avatar Mar 16 '19 13:03 azamat-bagatov

and you can only change global period

That's correct. The PWM driver is a software driver and uses the same timer for all channels. So all channels must have the same frequency/period (but different duty). There is no way to change this without a lot of work rewriting the PWM driver.

I think this should be highlighted in the documents, as it's far from obvious since it's a pin's method and not microbit's.

Yes it would be good to add such a note to the docs.

dpgeorge avatar Mar 20 '19 02:03 dpgeorge

Documenting the default frequency/period would be good too.

kevinjwalters avatar Aug 21 '20 17:08 kevinjwalters

i am making robotic car project in that i have to control dc motor with pwm signal using microbit. so i have written code for that which i combined digital write and analog write pins which part i am attaching below. But code is working only one time after that its not working(error - microbit led is flickering). how can i resolve this error?

pins.digital_write_pin(DigitalPin.P1,0) pins.digital_write_pin(DigitalPin.P2,0) pins.digital_write_pin(DigitalPin.P8,1) pins.digital_write_pin(DigitalPin.P12,0) pins.analog_write_pin(AnalogPin.P0,511) pins.analog_set_period(AnalogPin.P0,200) pins.analog_write_pin(AnalogPin.P3,511) pins.analog_set_period(AnalogPin.P3,200)

Darshnik253 avatar Feb 02 '22 14:02 Darshnik253