Problem with analogWrite()

Hello,

Board: Arduino Nano ESP32
IDE: 2.3.4
Core: ESP32 3.0.7

Same behavior with ESP32 Core 3.1.1 after update and same behavior with Arduino ESP32 Core 2.0.18.

I stumbled across a problem with analogWrite.

Short story. I have programmed an eye blinker. It works with one instance. Then I created a second and third instance and suddenly the LEDs (pins) go crazy.

I suspected an error in my program, reduced everything, still have arbitrary pin function with several instances.

Only when I replaced analogWrite with digitalWrite did everything work as it should.

Now I have written a test program and see that analogWrite really has a problem. Not all pins are controlled. After the 5th led nothing happens, they stay off.

Same problem with ledcAtach and ledcWrite.

With DigitalWrite all pins are controlled.

Is the problem known? Is there a solution?

Additional question.
Why can't the pins D17 to D24 be used with analogWrite? I get a declaration error. I thought all pins are PWM capable?

// --- Arduino Numbers ---
unsigned pin [] {D1, D0, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13}; //, D17, D18, D19, D20, D21, D22, D23, D24};

// --- ESP GPIO ---
//unsigned pin [] {43, 44, 5, 6, 7 ,8 ,9, 10, 17, 18, 21, 38, 47, 48, 1, 2, 3, 4, 11, 12, 13, 14};

void pinDigital (void)
{
  for (auto &p : pin) {
    digitalWrite(p, HIGH);
    delay(500);
    digitalWrite(p, LOW);
    delay(500);
  }
}

void pinAnalog (void)
{
  for (auto &p : pin) {
    analogWrite(p, 255);
    delay(500);
    analogWrite(p, 0);
    delay(500);
  }
}

void pinWriteC (void)
{
  for (auto &p : pin) {
    ledcWrite(p, 255);
    delay(500);
    ledcWrite(p, 0);
    delay(500);
  }
}

void setup()
{
  for (auto &p : pin) {
    ledcAttach(p, 500, 8);
  }
}

void loop()
{
  //pinDigital();
  //pinAnalog();
  pinWriteC();
}

Do you have current limit resistors on each LED? Post a picture and a schematic.

Hi, @Doc_Arduino
From this link;

Tom.. :smiley: :+1: :coffee: :australia:

Hello,

everything is fine with my schematics.

I had only looked at the pinout overview.
Arduino Nano ESP32 Pinout.pdf (1,9 MB)

If there are only 5 PWM pins in the overview, then this is correct. These pins would then be D0 to D4. Unfortunately, this is not mentioned anywhere in the documentation.

What I have found now describes it differently again. According to this, D0 to D13 should be PWM-capable.
https://docs.arduino.cc/tutorials/nano-esp32/cheat-sheet/#pwm

it is crazy

Hi,

according to my current tests, you can already use D0 to D13 analog. Just not more than 5 at the same time.
I can select all pins between D0 and D13 with 2 instances, 4 leds in total. With a 3rd instance (6 leds) everything goes crazy.

You have to know that first. Perhaps the documentation could be improved in detail?

Does the same limitation apply to the LEDC functionality of the ESP32-S3 ?

As to the documentation, this page Arduino® Nano ESP32 — Arduino Official Store includes the following

and https://docs.arduino.cc/tutorials/nano-esp32/cheat-sheet/ includes the following

Hi,

Hello,

analogWrite and LedC behave in the same way. Only 5 pins can be used at the same time. So only a maximum of 5 out of 14 pins can be used. Which 5 pins of the 14 does not matter. That is my current realization.

@TomGeorge Thanks!

With the LedC Lib I would have understood that all pins can be used at the same time. Unfortunate formulation of the documentation, I would say.

With the LedC Lib it is also not clear to me what I should do with the channel parameter. I would always hope for the automatic distribution.

// --- Arduino Numbers ---
//unsigned pin [] {D1, D0, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13}; //, D17, D18, D19, D20, D21, D22, D23, D24};
unsigned pin [] {D4, D5, D6, D7, D8, D9};

// --- ESP GPIO ---
//unsigned pin [] {43, 44, 5, 6, 7 ,8 ,9, 10, 17, 18, 21, 38, 47, 48, 1, 2, 3, 4, 11, 12, 13, 14};

void pinDigital (void)
{
  for (auto &p : pin) {
    digitalWrite(p, HIGH);
    delay(500);
    digitalWrite(p, LOW);
    delay(500);
  }
}

void pinAnalog (void)
{
  for (auto &p : pin) {
    analogWrite(p, 255);
    delay(500);
    analogWrite(p, 0);
    delay(500);
  }
}

void pinWriteC (void)
{
  for (auto &p : pin) {
    ledcWrite(p, 255);
    delay(500);
    ledcWrite(p, 0);
    delay(500);
  }
}

void setup()
{
  for (auto &p : pin) {
    ledcAttach(p, 500, 8);
  }
}

void loop()
{
  //pinDigital();
  //pinAnalog();
  pinWriteC();
}