digitalWrite to rp2040 RGB LEDs?

Is it possible to call digitalWrite() to write to one of the LEDs connected to the NINA, from inside an interrupt callback?

The reason I ask is because when I tried that, it bricked my nano rp2040.

Also, why can't I do something like digitalWrite(LEDB, 1); ? Why should that give me a compile error?

1 Like

what the error?

/Users/neil/devel/rp2040_clock/interrupt_test/interrupt_test.ino: In function 'void setup()':
interrupt_test:67:23: error: call to 'NinaPin::operator int' declared with attribute error: Change me to a #define
   digitalWrite(LEDB, 1);
                       ^

try to add macro in the first lines of sketch (before setup())

#define led LEDB

and use it instead of LEDB in the digitalWrite

digitalWrite(led,1);

That still gives the same error.

Have you #included the WiFiNINA library ?

See Nano RP2040 Connect RGB LED Tutorial - Learn Embedded Systems

Yes. It works if I do digitalWrite(LEDB, HIGH); but not if I put 1 instead of 'HIGH'. That's my question. Why can't I just put a number there?

Perhaps your question could have been a little more specific

How is HIGH defined for the RP2040 ?

If I print it out using Serial.println(), it prints 1. But I don't know where to look to find the macro that defines it.

Here's a simplified sketch that shows the problem:

#include <WiFiNINA.h>

void setup() {
  pinMode(LEDB, OUTPUT);
  digitalWrite(LEDB, HIGH); // This compiles okay.
  digitalWrite(LEDB, 1);    // This throws a compile error.
}

void loop() {
  // put your main code here, to run repeatedly:

}

Printing it is good enough and a value of 1 is no surprise

Please post the full error message

I already posted it in #3 above. Here it is again:

/Users/neil/devel/rp2040_clock/interrupt_test/interrupt_test.ino: In function 'void setup()':
interrupt_test:67:23: error: call to 'NinaPin::operator int' declared with attribute error: Change me to a #define
   digitalWrite(LEDB, 1);
                       ^

Sorry, I missed it

There was talk a while ago about making the second parameter to digitalWrite() a #define with enum values which would have prevented the use of

digitalWrite(ledPin, !digitalRead(ledPin));

which would have prevented that code being used to flip the state of an output pin

See Return pinMode, digitalWrite, digitalRead to working with numbers, not enums · Issue #168 · SpenceKonde/megaTinyCore · GitHub

I wonder if something like this has crept into the RP2040 implementation

1 Like

You have to cast as follows

digitalWrite(LEDB, (PinStatus) 1);    // This throws a compile error without (PinStatus) cast

The reason is digitalWrite function for NinaPins, such as LEDx is defined as

void digitalWrite(NinaPin pin, PinStatus value)

PinStatus is defined as

1 Like

Okay, I decided to go with this, which seems to work:

#define control_led(p, v) (v == 0 ? (digitalWrite(p, LOW)) : (digitalWrite(p, HIGH)))

Now to find out why I can't write to those pins inside an interrupt callback.

@khoih-prog thanks for the details

So, as I speculated, the same idiotic implementation as was nearly done for the main Arduino boards

the RGB LED is on the Nina module. the digitalWrite for RGB pins is overloaded to call WiFiNINA functions which communicate with the NINA module over SPI. this of course will not work in interrupt.
but it can't brick you board.

When I say bricked... what I meant was that it would no longer accept uploads from the computer. I could see that something was running, because the orange LED was blinking, though it wasn't blinking in the normal way. I was able to recover it by grounding the REC pin and pressing reset.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.