Using the RGB LED on the Arduino Nano RP2040 Connect

Source Code

Blinky

#include <WiFiNINA.h>

void setup() {
  // Set LED Pins as Outputs
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
}

void loop() {
  // Cycle Colour Blinking
  digitalWrite(LEDR,HIGH); // Turn On RED LED
  delay(250);
  digitalWrite(LEDR,LOW); // Turn Off RED LED

  digitalWrite(LEDG,HIGH); // Turn On GREEN LED
  delay(250);
  digitalWrite(LEDG,LOW); // Turn Off GREEN LED

  digitalWrite(LEDB,HIGH); // Turn On BLUE LED
  delay(250);
  digitalWrite(LEDB,LOW); // Turn Off BLUE LED
}

Pulsing/Fading LED

#include <WiFiNINA.h>

int rgbBlue = 0, dir=1;

void setup() {
  // Set the LED pins as outputs
  pinMode(LEDB, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  rgbBlue = rgbBlue + (1 * dir);

  if(rgbBlue > 255){
    rgbBlue = 255;
    dir = -1;
  }

   if(rgbBlue < 0){
    rgbBlue = 0;
    dir = 1;
  }
  analogWrite(LEDB, rgbBlue);
  delay(5);
}

Rainbow LED

#include <WiFiNINA.h>

int rgbValues[] = {255, 0, 0}; // 0=Red, 1=Green and 2=Blue
int upIndex=0, downIndex=1;

void setup() {
  // Set the RGB pins to output
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
}

void loop() {
  rgbValues[upIndex] += 1;
  rgbValues[downIndex] -= 1;

  if(rgbValues[upIndex] > 255){
    rgbValues[upIndex] = 255;
    upIndex = upIndex + 1;

    if(upIndex > 2){
      upIndex = 0;
    }
  }

  if(rgbValues[downIndex] < 0){
    rgbValues[downIndex] = 0;
    downIndex = downIndex + 1;

    if(downIndex > 2){
      downIndex = 0;
    }
  }

  analogWrite(LEDR, 255 - rgbValues[0]);
  analogWrite(LEDG, 255 - rgbValues[1]);
  analogWrite(LEDB, 255 - rgbValues[2]);  
  
  delay(5);
}

Leave a comment

Your email address will not be published. Required fields are marked *