#include <stdio.h>
#include "driver/ledc.h"
#include "esp_err.h"
// ------------------------------------------------------------------------
// ISSUE: In low speed mode only, the PWM frequency at the output is 1/10th
// of the defined frequency when set to 979 Hz or higher at 10-bit resolution.
// Note: 1/978 = 1023 (10-bit)
#define LEDC_FREQUENCY (979) // Frequency in Hertz
#define LEDC_MODE LEDC_LOW_SPEED_MODE
//#define LEDC_MODE LEDC_HIGH_SPEED_MODE
// -----------------------------------------------------------------------
#define LEDC_TIMER LEDC_TIMER_0
#define LEDC_OUTPUT_IO (19) // Define the output GPIO
#define LEDC_CHANNEL LEDC_CHANNEL_0
#define LEDC_DUTY (511) // Set duty to 50%
static void example_ledc_init(void)
{
// Prepare and then apply the LEDC PWM timer configuration
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = LEDC_TIMER_10_BIT,
.timer_num = LEDC_TIMER,
.freq_hz = LEDC_FREQUENCY,
.clk_cfg = LEDC_AUTO_CLK
};
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
// Prepare and then apply the LEDC PWM channel configuration
ledc_channel_config_t ledc_channel = {
.gpio_num = LEDC_OUTPUT_IO,
.speed_mode = LEDC_MODE,
.channel = LEDC_CHANNEL,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER,
.duty = 0, // Set duty to 0%
.hpoint = 0,
.flags = {
.output_invert = 0
}
};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
// Set duty to 50% and update
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
}
void setup() {
example_ledc_init();
}
void loop() {
}