← index #5737Issue #8249
Off-topic · high · value 3.072
QUERY · ISSUE

ESP32 Confusing behaviour of PWM after soft reset

openby peterhinchopened 2020-03-08updated 2023-09-17
port-esp32

Consider:

from machine import Pin, PWM
pin = Pin(23, Pin.OUT)
pwm = PWM(pin, freq=38000, duty=338)

This works as expected. If you issue ctrl-d the waveform on the pin continues unchanged. If you paste the above code again, you cause a low output with

pwm.duty(0)

Subsequently changing the duty value to a value > 0 has no effect. This is surprising.

There is a simple workround of issuing pwm.deinit() at the start. The current state could be confusing to newcomers. It should either be documented or changed so that a soft reset de-initialises any PWM instance.

CANDIDATE · ISSUE

RP2: PWM duty cycle does not remain constant when frequency is changed

closedby AlasdairAtKitronikopened 2022-02-02updated 2022-02-02
port-rp2

The expected behaviour is that if a duty cycle value is set, this should remain constant when the frequency is varied.
In the example code (used to drive a buzzer), the duty cycle starts at 50%, but as the frrequency is increased, the duty cycle increases up to 100%. This means that the buzzer stops sounding correctly as there is no longer any PWM occurring.

import machine

PWMOutput = machine.PWM(machine.Pin(0))

PWMOutput.duty_u16(32767)
freq = 500

while True:
    for freq in range (120, 5000, 50):
        PWMOutput.freq(freq)
    
    for freq in range(5000, 120, -50):
        PWMOutput.freq(freq)

To get round this, the cycle needs to be set ever time the frequency is set. Although this works, it does not seem the right way to be doing it.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied