Class Pwm

extends Object
A Pulse-Width Modulation (PWM) instance, for managing multiple PwmChannel.
The PWM instance controls a timer configured at the given frequency. All channels within the PWM instance are using the same timer. Each channel can in turn provide an individual duty factor.

Class summary


constructor --frequency/int --max-frequency/int=
Constructs the PWM generator with the given frequency and max-frequency.

Constructors

constructor --frequency/int --max-frequency/int=frequency
Constructs the PWM generator with the given frequency and max-frequency.
The resolution of the PWM is dependent on the max frequency. The higher it is the less resolution there is.
The frequency of the PWM must lie within a certain factor of the max frequency. For example, given a max-frequency of 10KHz, the lowest frequency that is accepted is 20Hz.
The max-frequency is limited to 40MHz.
The lowest acceptable frequency is 1Hz.
Advanced
On the ESP32, the duty resolution is computed as follows:

uint32 bits = msb(max_frequency << 1);
uint32 resolution_bits = kMaxFrequencyBits - bits;
duty_resolution = (ledc_timer_bit_t)resolution_bits,
This provides the highest duty resolution for the given max frequency.

Methods

close -> none
Closes the instance and all channels associated with it.

The frequency of this PWM.

frequency= value/int -> none
Sets the frequency of the PWM.
The value parameter must be lower than the max frequency that was used during construction of this instance.
It may not be too far below the max frequency, either.

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

start pin/any --duty-factor/num=0.0 -> PwmChannel
Starts a new PwmChannel on the provided pin. The channel is started, with the given duty-factor.
The pin is a GPIO number. The channel reserves the pin and releases it again when the channel is closed.
The default duty-factor of 0.0 means the pin stays low until otherwise configured.
See PwmChannel.set-duty-factor for more information on duty factors.
Passing a Pin is deprecated; provide the integer GPIO number instead. The Pin form will be removed in a future release.

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.