ESP32 minimal LEDC PWM configuration example on PlatformIO/Arduino using ESP-IDF LEDC API
This example configures the LEDC PWM timer at 10 kHz with 12 bit resolution, outputting a 50% duty cycle PWM on IO14
. This code uses the ESP-IDF API directly in order to configure the PWM
#include <Arduino.h>
#include <driver/ledc.h>
void setup() {
Serial.begin(115200);
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_HIGH_SPEED_MODE,
.duty_resolution = LEDC_TIMER_12_BIT,
.timer_num = LEDC_TIMER_0,
.freq_hz = 10000,
.clk_cfg = LEDC_AUTO_CLK
};
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
ledc_channel_config_t ledc_channel = {
.gpio_num = GPIO_NUM_14,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER_0,
.duty = 2048, // Set duty to 50%
.hpoint = 0
};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
}
void loop() {
}