/*
Forum: https://forum.arduino.cc/t/nesso-n1-how-do-i-write-pwm-with-ledc/1427843/17
Wokwi: https://wokwi.com/projects/454591390217129985
2026/01/30
ec2021
Generating sound on pin 11 using espressif ledc.h library
Code was generated by ChatGPT but with
.speed_mode = LEDC_HIGH_SPEED_MODE
which leads to a compilation error:
"error: 'LEDC_HIGH_SPEED_MODE' was not declared in this scope; did you mean 'LEDC_LOW_SPEED_MODE'?""
Compiles and works after changing to
.speed_mode = LEDC_LOW_SPEED_MODE
*/
#include <driver/ledc.h>
const int BEEP_PIN = 11;
const int PWM_CHANNEL = 0;
const int PWM_FREQ = 2000;
const int PWM_RES = 8; // 8-bit resolution
void setup() {
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.duty_resolution = (ledc_timer_bit_t)PWM_RES,
.timer_num = LEDC_TIMER_0,
.freq_hz = PWM_FREQ,
.clk_cfg = LEDC_AUTO_CLK
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t ledc_channel = {
.gpio_num = BEEP_PIN,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = (ledc_channel_t)PWM_CHANNEL,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = LEDC_TIMER_0,
.duty = 128, // 50%
.hpoint = 0
};
ledc_channel_config(&ledc_channel);
}
void loop() { }