ledcSetup or ledcAttachPin was not declared: ESP32 Arduino core 3 PWM change

In ESP32 Arduino core 3.x the older ledcSetup and ledcAttachPin calls were replaced by ledcAttach. Old tutorial code then fails to compile.

What the message means

The new calls take the pin directly. ledcAttach(pin, frequency, resolution) sets up the channel, and ledcWrite(pin, duty) writes to the pin.

Fix it in this order

  1. Check your core version in Tools > Board > Boards Manager.
  2. Replace ledcSetup(channel, freq, res) and ledcAttachPin(pin, channel) with ledcAttach(pin, freq, res).
  3. Call ledcWrite with the pin number, not the channel number.
  4. Or install core 2.x again if you must keep old code unchanged.
  5. Read the release notes of the core version before upgrading a working project.

Why it happens

Version 3 of the ESP32 Arduino core removed ledcSetup() and ledcAttachPin(). PWM is now set up with ledcAttach(pin, frequency, resolution) and written with ledcWrite(pin, duty), using the pin number instead of a channel.

const int PWM_PIN = 18;
void setup() { ledcAttach(PWM_PIN, 5000, 8); }   // pin, Hz, bits
void loop() { ledcWrite(PWM_PIN, 128); }

Example sketch: change the pin for your board and check your installed core version in the Boards Manager. The old calls only work on core 2.x.

If you see this, do this

You seeDo this
was not declared in this scopeCore 3 API change.
Code works on an older PC onlyDifferent core versions.

was not declared in this scope, PWM frequency.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.

Why this happens

Version 3 of the ESP32 Arduino core is built on a newer ESP-IDF, and its PWM interface was redesigned. The old calls expected you to choose a channel, set it up, and attach a pin to it. The new calls take the pin directly and manage the channel for you, so ledcSetup and ledcAttachPin no longer exist and the compiler reports them as not declared.

There is a hardware limit that did not change. The duty resolution you can use depends on the frequency. With the 80 MHz clock, the largest resolution in bits is the largest n where 2^n is no more than 80,000,000 divided by the frequency. At 5 kHz, that is 16,000, so 13 bits is the most.

How to confirm the cause

  • Open the Boards Manager and read the version of the esp32 package installed.
  • In the code, test the version with the macro ESP_ARDUINO_VERSION_MAJOR. Print it or use it in an #if.
  • Check whether the error is for the old functions (version 3) or for the new ones (version 2). Which one is missing tells you which core you have.
  • Check that the resolution you chose is allowed for the frequency, using the rule above.
  • Check whether a library you include still uses the old calls inside it.

Worked example

A sketch that works on both versions. It uses a helper so the calling code does not change.

const int PWM_PIN = 18, FREQ = 5000, BITS = 8;
void pwmWrite(int duty) {
#if ESP_ARDUINO_VERSION_MAJOR >= 3
  ledcWrite(PWM_PIN, duty);
#else
  ledcWrite(0, duty);          // version 2 writes by channel
#endif
}
void setup() {
#if ESP_ARDUINO_VERSION_MAJOR >= 3
  ledcAttach(PWM_PIN, FREQ, BITS);
#else
  ledcSetup(0, FREQ, BITS); ledcAttachPin(PWM_PIN, 0);
#endif
}
void loop() { pwmWrite(64); }  // 64 / 255 = 25 % duty

Common mistakes

  • Passing a channel number to ledcWrite on version 3, where it wants the pin.
  • Using a duty value above the maximum for the resolution. 8 bits allows 0 to 255.
  • Using ledcDetachPin on version 3, where the function has a new name.
  • Mixing a project that needs version 2 with a computer that has version 3, so the same code fails on one machine.
  • Choosing a resolution that the frequency cannot support.

If this did not fix it

  • A library, not your code, calls the old functions. Update the library or use a core version it supports.
  • Using a different board package, for example for another chip family, with different PWM capabilities.
  • A pin that cannot output, such as an input-only pin.