I am a beginner for using the ESP32 board and I am using Arduino IDE to write the code. Recently, I have been learning the LEDC function to control the PWM of LED but I met a question related to the frequency setting of LEDC.
For the code shown below, the frequency was 100 Hz and my LED was not bright but it could be a breathing LED if the frequency was set to 2000 Hz. I don't understand the reason. I don't know if my understanding related to the frequency was right or not. When I set the frequency to be 100hz, the cycle of PWM should be 1 s/100=10 ms. My resolution was 256, so each duty should be around 39 us (10 ms/256 =39us). When my loop begins and the duty should be 1 firstly, it means for the PWM cycle duration (10 ms), only 39 us should be high voltage (3.3V) and the left 9.96 ms should be low voltage (0V). However, my delay was 5 ms, so the 0V would last only 4.96 ms, and then duty changed to 2, and the output would be 3.3V for 78us and 0V would last for 4.92 ms. Finally, when the duty reached 255, the 3.3V should last for 5ms, right? Then why the LED was not bright at all? I am sorry, maybe my question is too rookie.
#define LED_pin 10
#define channel 0
#define freq 100
#define resolution 8
void setup() {
// put your setup code here, to run once:
ledcSetup (channel, freq, resolution);
ledcAttachPin (LED_pin, 0);
}
void loop() {
// put your main code here, to run repeatedly:
for (int duty=1; duty<pow(2, resolution); duty++)
{
ledcWrite(channel, duty);
delay (5);
}
for (int duty=pow(2, resolution); duty>=0; duty--)
{
ledcWrite(channel, duty);
delay(5);
}
}