Strange delay with analogWrite on XIAO ESP32-C3 vs. XIAO SAMD21

I’ve encountered strange behaviour where there’s siginificant 1-2 second delay between running analogwrite and having that new value actually be outputted from the pin.

I reduced the problem on very simple test that works as it should on XIAO SAMD21 but when uploaded to ESP32-C3 it causes 1-2 seconds delay between the input pin getting high and the output reflecting the change. If I change the analogWrite to digitalWrite the delay is gone and output reflects the input instantaneously on ESP32-C3 also.

Any thought’s what is going on here?

test sketch

int _m1Output = 5;
int _m1ScaledSpeed = 0;

void setup() {
Serial.begin(115200);
pinMode(_m1Output, OUTPUT);
pinMode(2, INPUT);
}

void loop() {
_m1ScaledSpeed = map(digitalRead(2), 0, 1, 0, 255);
Serial.println(_m1ScaledSpeed);
analogWrite(_m1Output, _m1ScaledSpeed);
}

Here’s short video clip running the code above (with different max out value).

video clip

What do you expect from analogWrite() on the XIAO ESP32C3?
It cannot provide analog voltage output because it does not have a built-in DAC.
For PWM output, please see the following instead of analogWrite().
//\Arduino15\packages\esp32\hardware\esp32\2.0.6\cores\esp32\esp32-hal-ledc.c and .h

edit:
I have confirmed that the following code works, needed delay() in the loop. I do not know why.

int pwm = 0;
void setup() {
  Serial.begin(115200);

  pinMode(D10, OUTPUT);
  pinMode(D1, INPUT_PULLUP);
}

void loop() {
  pwm = map(digitalRead(D1), 0, 1, 0, 255);
  Serial.println(pwm);
  analogWrite(D10, pwm);
  delay(100);  // needed
}

Thank you so much looking into this! I can also confirm that just adding delay even as short as 1ms makes this work on ESP32-C3.

I’m quite curious what’s going on here and why the delay makes the difference o ESP32-C3 vs. XIAO SAMD21.

Anyways, thanks for your help @msfujino