• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 ledcDetach(pin_num) fails how can I findout why

Q&A Forum › Category: ESP32 › ESP32 ledcDetach(pin_num) fails how can I findout why
0 Vote Up Vote Down
David Petzer asked 7 months ago

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.

Question Tags: ledc pwm detach
4 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 7 months ago

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

0 Vote Up Vote Down
David Petzer answered 7 months ago

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);
}

0 Vote Up Vote Down
David Petzer answered 7 months ago

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.

0 Vote Up Vote Down
Sara Santos Staff answered 7 months ago

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

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.