PWM: Pulse-Width Modulation

The driver interface is defined in the ESP8266 SDK.

Build variables

ENABLE_CUSTOM_PWM
undefined
use the Espressif PWM driver
1 (default)
Use the New PWM driver, a drop-in replacement for the version provided in the Espressif SDK.

API Documentation

void pwm_init(uint32 period, uint32 *duty, uint32 pwm_channel_num, uint32 (*pin_info_list)[3])

Initialize PWM function, including GPIO selection, period and duty cycle.

Example:

    uint32 io_info[][3] = {
        {PWM_0_OUT_IO_MUX, PWM_0_OUT_IO_FUNC, PWM_0_OUT_IO_NUM},
        {PWM_1_OUT_IO_MUX, PWM_1_OUT_IO_FUNC, PWM_1_OUT_IO_NUM},
        {PWM_2_OUT_IO_MUX, PWM_2_OUT_IO_FUNC, PWM_2_OUT_IO_NUM}
};

pwm_init(light_param.pwm_period, light_param.pwm_duty, 3, io_info);
Note
This API can be called only once.
Parameters
  • period: PWM period
  • duty: duty cycle of each output
  • pwm_channel_num: PWM channel number
  • pin_info_list: Array containing an entry for each channel giving

void pwm_start(void)

Starts PWM.

This function needs to be called after PWM configuration is changed.

void pwm_set_duty(uint32 duty, uint8 channel)

Sets duty cycle of a PWM output.

Set the time that high-level signal will last. The range of duty depends on PWM period. Its maximum value of which can be Period * 1000 / 45.

Parameters
  • duty: The time that high-level single will last, duty cycle will be (duty*45)/(period*1000)
  • channel: PWM channel, which depends on how many PWM channels are used

For example, for 1-KHz PWM, the duty range is 0 ~ 22222.

uint32 pwm_get_duty(uint8 channel)

Get duty cycle of PWM output.

Duty cycle will be (duty*45) / (period*1000).

Parameters
  • channel: PWM channel, which depends on how many PWM channels are used
Return Value
  • uint32: Duty cycle of PWM output

void pwm_set_period(uint32 period)

Set PWM period.

Parameters
  • period: PWM period in us. For example, 1-KHz PWM period = 1000us.

uint32 pwm_get_period(void)

Get PWM period.

Return Value
  • uint32: Return PWM period in us.

uint32 get_pwm_version(void)

Get version information of PWM.

Return Value
  • uint32: PWM version