I was using the led pwm controller (ledc) to flash an LED on IO12 of an Ardafruit Feather Hazzah board.
I managed to attach the pin, set frequency and duty cycle. All was working well.
Then I wanted to swap to pin 14 just to make sure I had correctly located that pin.
To my surprise Pin12 was still connected to the PWM driver and flashing the LED.
Pin14 was now also connected and it could flash the LED.
I tried to use ledcDetach(12); to detach the pin.
This did not work. The function returns false and the pin is still being driven by the ledcPWM module.
I can change the frequency of the ledcPWM and both pin12 and pin14 frequencies change.
Here is the pwmc related code I have in the setup(){ } section.
Pastebin Link
if(!ledcDetach(12)){
Serial.println(“LEDC Detach failed for pin 12”);
}else{
Serial.println(“LEDC Detach success for pin 12”);
};
if(!ledcDetach(14)){
Serial.println(“LEDC Detach failed for pin 14”);
}else{
Serial.println(“LEDC Detach success for pin 14”);
};
if(ledcAttach(14, ledPWMFreq, ledPWMRes)){
Serial.println(“Attached pin14 to PWM”);
};
Here is the serial output:
16:45:29.882 -> entry 0x400805cc 16:45:31.265 -> LEDC Detach failed for pin 12 16:45:31.265 -> LEDC Detach failed for pin 14 16:45:31.265 -> Attached pin14 to PWM
Any assistance would be appreciated.
Hi.
I’m not sure what might be wrong because I never tried it.
But, I suggest setting the pin to LOW before calling the ledcDetach function.
Let me know if that works.
Regards,
Sara
Thanks Sara, I have tried what you asked. It seems to have worked given I can now control the pins using normal digital writes. I will do some more experimenting later when I have time reattaching, detaching and using the pins for input as well as output. For now here is what I have put into the setup section and I am getting a state change on pin 14 every 5 seconds.
void setup() {
Serial.begin(115200);
delay(500);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
digitalWrite(12, LOW);
delay(500);
digitalWrite(14, LOW);
delay(500);
// configure LED PWM
if(!ledcDetach(12)){
Serial.println("LEDC Detach failed for pin 12");
}else{
Serial.println("LEDC Detach success for pin 12");
}
if(!ledcDetach(14)){
Serial.println("LEDC Detach failed for pin 14");
}else{
Serial.println("LEDC Detach success for pin 14");
}
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Looping");
digitalWrite(14, !digitalRead(14));
delay(5000);
}
Spent a few hours fiddling with this again tonight. There is definitely something strange going on.
If I use pin 14 for the LEDC the output pulse appears on pin14 immediately then appears on pin 12 after about 4 seconds.
If I use pin12 then the output does not appear on pin14.
Also ledcRead and ledcReadFreq only seem to work when using pin 12.
There is a bit more experimenting to do here but I am starting to think I have a hardware fault. Time to buy another board so I can rule that out.
Ok.
then, let me know the results.
You may also find useful, taking a look at the complete documentation about the LEDC control of the ESP32: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/ledc.html
Regards,
Sara