ESP8266 Software PWM

Software PWM for the ESP8266. Warning: This is a software PWM and therefore can have noticeable flickering. Additionally, this software PWM can’t output values higher than 80%. That’s a known limitation.

Example Usage

// Basic
auto *output = App.make_esp8266_pwm_output(D2);
// Create a brightness-only light with it:
App.make_monochromatic_light("Desk Lamp", output);

// Advanced: Setting a custom frequency globally

See Application::make_esp8266_pwm_output().

API Reference


class ESP8266PWMOutput : public output::FloatOutput, public Component

Software PWM output component for ESP8266.

Supported pins are 0-16. By default, this uses a frequency of 1000Hz, and this can only be changed globally for all software PWM pins. To change the PWM frequency, do the following:

analogWriteFreq(500); // 500Hz frequency for all pins.

Note that this is a software PWM and can have noticeable flickering because of other interrupts on the ESP8266 (like WiFi). Additionally, this PWM output can only be 80% on at max. That is a known limitation, if it’s a deal breaker, consider using the ESP32 instead - it up to 16 integrated hardware PWM channels.

Public Functions

ESP8266PWMOutput(const GPIOOutputPin &pin)

Construct the Software PWM output.

void set_frequency(float frequency)
void setup()

Initialize pin.

void dump_config()
float get_setup_priority() const

HARDWARE setup_priority.

void HOT output::ESP8266PWMOutput::write_state(float state)

Override FloatOutput’s write_state for analogWrite.

Protected Attributes

GPIOOutputPin pin_
float frequency_ = {1000.0}