ledcAttachPin ledcSetup error and how to solve it?

Im using arduino IDE 2.3.2 with esp32 wrrom kit and Im trying to generate a simple pwm example and Im getting this error:

Compilation error: 'ledcSetup' was not declared in this scope

The esp32 library is the 3.0.4.
I read in the forum abouyt this error but how do I solve it?
Thanks
Gaston

1 Like

Please post your sketch, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like

Show the full error.

https://docs.espressif.com/projects/arduino-esp32/en/latest/migration_guides/2.x_to_3.0.html#ledc

2 Likes

did you #include <esp32-hal-ledc.h> ?
ledc API has changed, theres no ledcAttachPin anymore

I forgot to mention that this is a new installation of arduino IDE 2.3, I already installed the esp32 espresif. So I need to install other libraries?¡

#include <Arduino.h>
#include <esp32-hal-ledc.h>
// Define LEDC channel, GPIO pin, and resolution
#define LEDC_CHANNEL 0
#define LEDC_PIN 12
#define LEDC_RESOLUTION 10 // Set resolution to 10 bits
void setup() {
    // Initialize LEDC channel
    ledcSetup(LEDC_CHANNEL, 50, LEDC_RESOLUTION); // Set frequency to 50Hz, resolution to 10 bits
    ledcAttachPin(LEDC_PIN, LEDC_CHANNEL); // Associate GPIO pin with LEDC channel
}
void loop() {
    int dutyCycle = (pow(2, LEDC_RESOLUTION) - 1) * 0.075; // Calculate duty cycle value using 10-bit resolution
    ledcWrite(LEDC_CHANNEL, dutyCycle);
    // Delay for 1 second
    delay(1000);

    dutyCycle = (pow(2, LEDC_RESOLUTION) - 1) * 0.0875; // Calculate duty cycle value using 10-bit resolution
    ledcWrite(LEDC_CHANNEL, dutyCycle);
    delay(1000);

    dutyCycle = (pow(2, LEDC_RESOLUTION) - 1) * 0.075; // Calculate duty cycle value using 10-bit resolution
    ledcWrite(LEDC_CHANNEL, dutyCycle);
    delay(1000);

    dutyCycle = (pow(2, LEDC_RESOLUTION) - 1) * 0.0625; // Calculate duty cycle value using 10-bit resolution
    ledcWrite(LEDC_CHANNEL, dutyCycle);
    delay(1000);
}

the error output

/home/gaston/Arduino/sketch_sep16b/sketch_sep16b.ino:9:5: error: 'ledcSetup' was not declared in this scope
    9 |     ledcSetup(LEDC_CHANNEL, 50, LEDC_RESOLUTION); // Set frequency to 50Hz, resolution to 10 bits
      |     ^~~~~~~~~
/home/gaston/Arduino/sketch_sep16b/sketch_sep16b.ino:10:5: error: 'ledcAttachPin' was not declared in this scope; did you mean 'ledcAttach'?
   10 |     ledcAttachPin(LEDC_PIN, LEDC_CHANNEL); // Associate GPIO pin with LEDC channel
      |     ^~~~~~~~~~~~~
      |     ledcAttach

exit status 1

Compilation error: 'ledcSetup' was not declared in this scope

@ZX80 tried to point you to the Espressif documents that explain what are the differences between version 2.x and 3.x of the ESP32 API.

On 3.x version, ledcSetup() and ledcAttachPin() are no longer available. That means you have 2 options:

Rollback the version of the ESP32 board to the version 2.x OR modify your code according to the new API. Since the code is nor big or complex, I suggest the second option.

void setup() {
    ledcAttach(LEDC_PIN, 50, LEDC_RESOLUTION); 
}

and all ledcWrite functions thereafter will need to be:

ledcWrite(LEDC_PIN, dutyCycle);
4 Likes

Maybe try this version:

#define LEDC_PIN 12
#define LEDC_RESOLUTION 10 // Set resolution to 10 bits
float multiplyfactor[4] = {0.075,0.087,0.075,0.0625};
void setup() {
    ledcAttach(LEDC_PIN, 50, LEDC_RESOLUTION);
}
void loop() {
    for (int i=0; i<=3; i++){
      int dutyCycle = (pow(2, LEDC_RESOLUTION) - 1) * multiplyfactor[i];
      ledcWrite(LEDC_PIN, dutyCycle);
      delay(1000);
    }
}
1 Like

thanks Brazilino it was that!!!
Regards
GAstón

1 Like

does this work on esp8266 too?

no.

LEDC not working in ESP8266? it means LEDC is specifically for ESP32 only?

Thanks

1 Like