Skip to content

PWM: Changing the PWM frequency affects the duty cycle on some ports. #7909

Closed

Description

@robert-hh

PWM: with some frequency changes the duty cycle is affected as well.
MicroPython v1.17-84-gba940250a on 2021-10-17; ESP32 module with ESP32
Examples:
Code:

from machine import PWM, Pin
p=PWM(Pin(4), 100000)

Result: No output, until duty cycle is set, e.g. to 512 = 50%.
Then, starting at 100k:
p.freq(10000)
Duty cycle changes to 256
After setting duty cycle again to 512:
p.freq(100000)
Result: output is off, a call to duty() reports 1024, which b.t.w. cannot be set manually.
I expect the duty cycle to stay the same or at least as close as possible at the set value, when the frequency is changed, since it is ratio, not related to physical units like frequency or time. Contrary to that, I expect the on-time of the duty-cycle to be frequency-invariant if it was set with duty_ns().

Edit: The RP2040 and STM32 ports show a similar behavior. ESP8266 seems ok. I did not test NRF ports.

Activity

  1. changed the title [-]ESP32: Changing the frequency affects the duty cycle.[/-] [+]ESP32: Changing the PWM frequency affects the duty cycle.[/+] on Oct 17, 2021
  2. robert-hh commented on Oct 18, 2021

    @robert-hh
    ContributorAuthor

    @IhorNehrutsa I faintly recall that you contributed the PWM module. May I ask you to take care of this unexpected behavior. It could be sufficient to memorize the duty cycle setting and set if again after a frequency change.

  3. IhorNehrutsa commented on Oct 18, 2021

    @IhorNehrutsa
    Contributor

    Under construction at
    esp32/machine_pwm: Add PWM duty_u16() and duty_ns(). #7907

  4. IhorNehrutsa commented on Oct 19, 2021

    @IhorNehrutsa
    Contributor

    Please rename
    ESP32: Changing the PWM frequency affects the duty cycle.
    to
    PWM: Changing the PWM frequency affects the duty cycle.

    This issue about all hardware PWM's

  5. robert-hh commented on Oct 19, 2021

    @robert-hh
    ContributorAuthor

    Yeah, it seems that not only the ESP32 port shows that behavior. I tested the RP2040, pyboard, ESP8266 and W600. The RP2040 and pyboard fail too. The ESP8266 and W600 are fine. The mimxrt port as well. While the fix for the rp2040 seems easy, it seems more complicated for the pyboard.

  6. changed the title [-]ESP32: Changing the PWM frequency affects the duty cycle.[/-] [+]PWM: Changing the PWM frequency affects the duty cycle on some ports.[/+] on Oct 19, 2021
  7. IhorNehrutsa commented on Oct 19, 2021

    @IhorNehrutsa
    Contributor

    The Latest commits of #7907 fix this issue for ESP.
    Please report bugs to #7907

  8. added a commit that references this issue on Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions