DigitalWrite does not work after AnalogWrite on RP2040 boards

I do believe this belongs here as arduino has rp2040 boards and analogWrite and DigitalWrite are arduino functions

Long story short, digitalWrite does not work anymore after analogWrite is called on that specific pin, I even try to reinitiallize the pin by pinMode and it still wouldnt budge. I also tried setting the pin to INPUT and back to OUTPUT still didnt work.

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  analogWrite(LED_BUILTIN, 50);
  delay(1000);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);
}


void loop() {

}

Are there work around to this problem? Aside from using analogWrite(LED_BUILTIN,0) and analogWrite(LED_BUILTIN,255) for off and on ?

IDE version : Arduino IDE v1.8.16
IDE board : Arduino MBED OS RP2040 Boards
Physical Board : Raspberry Pi Pico

What is it you're trying to achieve? Start with dim going to full brightness and then off?

I guess you can use analogWrite() throughout - a value of 1023 means full brightness, 255 is half and so on...others more experienced might be able to help better...

With an external LED experiment with different series resistance values (1k to 10k) to see which gives the best effect.

And anything you want to do should be inside loop(). Code in setup() will execute only once.

Your post was MOVED to its current location as it is more suitable.

Looks like a real bug. As a workaround, try doing

   analogWrite(LED_BUILTIN, -1);                   // turn off PWM
   gpio_set_function(LED_BUILTIN, GPIO_FUNC_SIO);  // revert pin to SW control of GPIO

Bug report submitted

1 Like

Thank you for reporting it sir, you are absolutely right that atleast pinMode() should free the pins of whatever it was set to.

isnt gpio_set_function a raspberry pi sdk function, i did not know you have access to it.

Anyway, that is a cool work around, that did the trick ! thank you ! It was very annoying to set pins as analogWrite(pin,0) and analogWrite(pin,255); as i only need the pins to be momentarily be a pwm pin

It is, and you do.
However, keeping track of whether you need to keep MBED in sync with things that are done "underneath" it is liable to be "complicated." (For example, the MBED pwm_api() has a pwm_free() function to turn off pwm, and it does "delete" on the pwm data structure (which is what happens with the negative PWM value.) But I2C and SPI don't have a similar free function, presumably because MBED has some additional "knowledge" about I2C and SPI at some higher level.