digitalWrite() working strangly on Nano 33 BLE tri-colour LED

Hi, this is my first post on the Arduino forum, I hope I get all the formatting correct.

I have a very strange problem. My code is based on the 'BlinkWithoutDelay' example. If I write ledState to the red LED, the blue LED will flash, but if I check the state of ledState in an 'if' statement, and write HIGH or LOW to the red LED accordingly... it works! Code snippets below.

loop() has exactly the same timing code and setting of ledState as BlinkWithoutDelay, just with added code to drive the LEDs:

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(LED_YELLOW, ledState);

    //digitalWrite(LED_RED, ledState);    // this line makes BLUE LED flash, not RED...!
    if (ledState == LOW) {
      digitalWrite(LED_RED, LOW);         // This code block works as expected...!
    } else {
      digitalWrite(LED_RED, HIGH);
    }
    
  }
}

If I uncomment the commented line (and comment out the 'if' block) then the blue LED flashes. Code as posted makes the red LED flash. Yellow LED is behaving as expected. If I read this on a forum I'd be inclined to not believe it so if you want I'll post a video :slight_smile:

I have included PinNames.h and defined names for the three coloured LEDs as well as the 'standard' yellow LED

#include "PinNames.h"
const PinName LED_GREEN = PinName::p16;
const PinName LED_RED = PinName::p24;
const PinName LED_BLUE = PinName::p6;
const PinName LED_YELLOW = PinName::p13;

I have set the pin modes and initial states in setup()

void setup() {
  // set the digital pin as output:
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_RED, HIGH);
  digitalWrite(LED_GREEN, HIGH);
  digitalWrite(LED_BLUE, HIGH);
}

Any ideas what is going on...?

Are you sure you are using the correct pin numbers for the LEDs? The pins_arduino.h file for the Nano 33 BLE defines the LED pins as

// LEDs
// ----
#define PIN_LED     (13u)
#define LED_BUILTIN PIN_LED
#define LEDR        (22u)
#define LEDG        (23u)
#define LEDB        (24u)
#define LED_PWR     (25u)

David,

Thanks, that fixed it. I took the port numbers off the schematic I have, but I guess it is out of date.

Could you tell me how to tell which pins_arduino.h file corresponds to nano BLE 33? A disk search locates 11 of these files, and I don't know how to tell which one applies.

Thanks

Tony

I spoke a bit too soon. Changing these definitions reversed the behaviour. Now the direct call to digitalWrite() works, but the 'if' block fails to control anything but the red LED, and that by writing to LED_BLUE - P0.24, which matches the connection to the red LED on my schematic...

I'm thinking LED_RED etc. must be defined elsewhere and the compiler must be resolving them differently depending on whether they are used in a function call or a statement. Or something else weird like that.

It seems PinNames.h was not my friend. I have it working consistently for the direct call to digitalWrite() and the 'if' block version, if I just use the #defines as posted by David.

Thanks,

Tony

The is another discussion on the LED pins on the Nano 33 BLE that will answer most of your questions:

Testing the RGB LED only glows Blue LED with the pinMode declaration from RED

Tony: ELO, from the Time album?