#include <driver/ledc.h>
#include <hal/ledc_types.h>
// fade LED PIN (replace with LED_BUILTIN constant for built-in LED)
#define LED_PIN 5
int freq = 1; // how bright the LED is
ledc_timer_bit_t bits = LEDC_TIMER_8_BIT;
ledc_mode_t mode = LEDC_LOW_SPEED_MODE;
ledc_timer_t timer = LEDC_TIMER_0;
ledc_channel_t channel = LEDC_CHANNEL_0;
ledc_clk_cfg_t clockcfg = LEDC_USE_REF_TICK;
void setup() {
Serial.begin(115200);
ledc_timer_config_t timercfg = {
.speed_mode = mode,
.duty_resolution = bits,
.timer_num = timer,
.freq_hz = 1000,
.clk_cfg = clockcfg
};
ledc_timer_config(&timercfg);
ledc_channel_config_t channelcfg = {
.gpio_num = LED_PIN,
.speed_mode = mode,
.channel = channel,
.timer_sel = timer,
.duty = 1 << (bits - 1),
.hpoint = 0,
.flags = {.output_invert = 0}
};
ledc_channel_config(&channelcfg);
}
void loop() {
uint64_t clk = LEDC_REF_CLK_HZ << (8-bits);
Serial.print(clk);Serial.print(" ");
uint32_t div = (uint64_t)(LEDC_REF_CLK_HZ << (8-bits)) / freq;
Serial.print(div);Serial.print(" ");
ledc_timer_set(mode, timer, div, bits, (ledc_clk_src_t) clockcfg);
Serial.print(freq);
Serial.print("/");
Serial.println(ledc_get_freq(mode, timer));
freq *= 2;
if (freq > 20000) {
delay(10000);
freq = 1;
}
delay(600);
}