Problems with alternative ESP32 arduino core 3

Hi, I have a working project that was working good with arduino ESP32 core 2 or platform = espressif32 @ ^6.7.0 in platformio.

Now I’m trying to switch to ESP32 core 3 and the code compiles and upload to the dev board without errors. This is my ini file:

[env:esp32dev]
platform = https://github.com/platformio/platform-espressif32.git#develop
platform_packages=
  framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.1
  framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.1/esp32-arduino-libs-3.0.1.zip
board = esp32dev
framework = arduino
monitor_speed = 115200
monitor_rts = 0
monitor_dtr = 0

I’m using classic ESP32 dev board.
The problems are related to ledc (pwm) parts of the code and digitalWrite commands. These are the errors I get on the serial monitor:

[122854][E][esp32-hal-ledc.c:219] ledcDetach(): pin 18 is not attached to LEDC
[122861][E][esp32-hal-gpio.c:166] __digitalWrite(): IO 18 is not set as GPIO.
[122868][E][esp32-hal-ledc.c:63] ledcAttachChannel(): Channel 0 is not available (maximum 16) or already used!
[122878][E][esp32-hal-ledc.c:219] ledcDetach(): pin 4 is not attached to LEDC
[122885][E][esp32-hal-gpio.c:166] __digitalWrite(): IO 4 is not set as GPIO.
[122892][E][esp32-hal-ledc.c:63] ledcAttachChannel(): Channel 1 is not available (maximum 16) or already used!
[122902][E][esp32-hal-ledc.c:219] ledcDetach(): pin 12 is not attached to LEDC
[122909][E][esp32-hal-gpio.c:166] __digitalWrite(): IO 12 is not set as GPIO.
[122916][E][esp32-hal-ledc.c:63] ledcAttachChannel(): Channel 2 is not available (maximum 16) or already used!
[122926][E][esp32-hal-ledc.c:219] ledcDetach(): pin 26 is not attached to LEDC
[122933][E][esp32-hal-gpio.c:166] __digitalWrite(): IO 26 is not set as GPIO.
[122939][E][esp32-hal-ledc.c:63] ledcAttachChannel(): Channel 3 is not available (maximum 16) or already used!

Any ideas?

Please provide a minimal code to reproduce the error.

Arduino 3.x has some breaking API changes. See Migration from 2.x to 3.0 - - — Arduino ESP32 latest documentation
Especially the ledc API has changed.

Did you perform a “Full Clean” after switching to Arduino 3.0.1 ?
Otherwise there might be some Arduino 2.x leftovers.

Meanwhile Arduino 3.0.2 has been released:

platform_packages=
  framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.2
  framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.2/esp32-arduino-libs-3.0.2.zip

Please note that Arduino 3.x is not (yet) officially supported by PlatformIO.
Hence the question: Does the problem also occur with the ArduinoIDE?

What do you mean by “Full Clean”? It works in ArduinoIDE, so as you said it must be “Arduino 2.x leftovers”. I deleted platform ESP32 6.7.0 I had but that didn’t helped.
I also tried to use platform = https://github.com/Jason2866/platform-espressif32.git#Arduino/IDF5 as a platform in the ini file.
Can you show me how ini file should loook?
This is the minimum code, just in the setup loop:

#include <Arduino.h>
void setup()
{
  Serial.begin(115200);
  delay(1000);
  ledcAttachChannel(MOTOR_1_IN1, PWM_FREQ, PWM_RESOLUTION, PWM_CHANNEL_1);
  ledcAttachChannel(MOTOR_1_IN2, PWM_FREQ, PWM_RESOLUTION, PWM_CHANNEL_1);
  ledcAttachChannel(MOTOR_2_IN3, PWM_FREQ, PWM_RESOLUTION, PWM_CHANNEL_2);
  ledcAttachChannel(MOTOR_2_IN4, PWM_FREQ, PWM_RESOLUTION, PWM_CHANNEL_2);
  ledcAttachChannel(MOTOR_3_IN1, PWM_FREQ, PWM_RESOLUTION, PWM_CHANNEL_3);
  ledcAttachChannel(MOTOR_3_IN2, PWM_FREQ, PWM_RESOLUTION, PWM_CHANNEL_3);
  ledcAttachChannel(MOTOR_4_IN3, PWM_FREQ, PWM_RESOLUTION, PWM_CHANNEL_4);
  ledcAttachChannel(MOTOR_4_IN4, PWM_FREQ, PWM_RESOLUTION, PWM_CHANNEL_4);
}
void loop()
{
}

And after resetting the board I get these errors:

[  1020][E][esp32-hal-ledc.c:63] ledcAttachChannel(): Channel 0 is not available (maximum 16) or already used!
[  1031][E][esp32-hal-ledc.c:63] ledcAttachChannel(): Channel 1 is not available (maximum 16) or already used!
[  1040][E][esp32-hal-ledc.c:63] ledcAttachChannel(): Channel 2 is not available (maximum 16) or already used!
[  1050][E][esp32-hal-ledc.c:63] ledcAttachChannel(): Channel 3 is not available (maximum 16) or already used!

First of all: In contrast to the ArduinoIDE, there is no official PlatformIO support for Arduino 3.x yet. Incompatibilities can therefore always occur. There is no guarantee.

There “might be” leftovers if you compiled the sketch before switching to Arduino 3.x.
You can perform a Full Clean either by clicking on the PIO-Icon / Project Tasks / Full-Clean:
image
or simply delete the .pio/build folder in the current project.

Your platformio.ini file looks okay.

I’m not (yet) familiar with the new Arduino 3.x API’s.

Have you tried ledcAttach instead of ledcAttachChannel ?
After defining the missing constants, this code compiles, uploads and work without errors on the serial monitor:

void setup() {
    Serial.begin(115200);
    delay(1000);
    ledcAttach(MOTOR_1_IN1, PWM_FREQ, PWM_RESOLUTION);
    ledcAttach(MOTOR_1_IN2, PWM_FREQ, PWM_RESOLUTION);
    ledcAttach(MOTOR_2_IN3, PWM_FREQ, PWM_RESOLUTION);
    ledcAttach(MOTOR_2_IN4, PWM_FREQ, PWM_RESOLUTION);
    ledcAttach(MOTOR_3_IN1, PWM_FREQ, PWM_RESOLUTION);
    ledcAttach(MOTOR_3_IN2, PWM_FREQ, PWM_RESOLUTION);
    ledcAttach(MOTOR_4_IN3, PWM_FREQ, PWM_RESOLUTION);
    ledcAttach(MOTOR_4_IN4, PWM_FREQ, PWM_RESOLUTION);
}

void loop() {}

I need ledcAttachChannel() function because for every of my 4 motoros, I need a different PWM channel. Btw, I tried ledcAttach() just to check that and there are no errors on the Serial with it.

Yes, I know about that but I’ve seen that people had success with it? I find it odd that no one experienced a problem with this ledc function. I did a full clean but that didn’t help.
Any other ideas?

For some reason, when using ledcAttachChannel, all channels seem to be already allocated.

In contrast, ledcAttach automatically selects a “free” channel - See arduino-esp32/cores/esp32/esp32-hal-ledc.c at master · espressif/arduino-esp32 · GitHub

Unfortunately I could not find anything in the source code why the channels are considered allocated.

Ok, thank you for your time! Just one more question, is there maybe a way to use old ledc API with this core?

The API has changed with version 3.x! (See link “Migration from 2.x to 3.0” from above).
If you want to use the old API you have to stay with Arduino 2.x!

Ok, that was a stupid question… :slight_smile:
I really hope there will be official PlatformIO support for Arduino 3…