MKR Wifi1010 RGB led

Hello.

I just bought my wifi1010 and I have been playing around with it. I noticed that there is an RGB led on board. I was searching around how to use the led than I looked at the schematic and noticed that it is connected to ne wifi GPIO module.

How do I control the leds?

I'm on the same boat.

The documentation on this board is pretty scarce, very few examples and almost any information about the capabilities of the ESP32 exposed via Arduino SDK.

I've been playing with this module and think I have the RGB LEDs figured out. Sorry for the lengthy explanation, just want to be meticulous.

First, I'm using the WiFiNINA library. See here and install it if necessary.

There's no direct support in the library for the LEDs but it's pretty easy to add.

Locate the /src folder and find the WiFi.cpp and WiFi.h files there. Open both in the editor of your choice.

You want to add a few defines and four functions to the WiFi class. The defines will describe the pin numbers associated with the LEDs on the WiFi module. I added these just before the class constructor in WiFi.cpp:

.
.
.
extern "C" {
  #include "utility/wl_definitions.h"
  #include "utility/wl_types.h"
  #include "utility/debug.h"
}

#define GREEN_LED_pin   25
#define BLUE_LED_pin    27
#define RED_LED_pin     26


WiFiClass::WiFiClass()
{
}
.
.
.

The pin definitions took a bit of trial and error. The schematic for the board shows the red LED being connected to pin 18 which is labeled "GPIO27" but it seems to be "pin 26" as far as the code is concerned. This pattern works on my board; YMMV.

You now need a way to initialize the pins to be digital outputs. Fortunately, the WiFiDrv class gives us that. Add the following function to the class (I added it immediately after the constructor):

void WiFiClass::WiFiInitLEDs( void )
{
    WiFiDrv::pinMode( BLUE_LED_pin, OUTPUT );     //BLUE
    WiFiDrv::pinMode( RED_LED_pin, OUTPUT );     //RED
    WiFiDrv::pinMode( GREEN_LED_pin, OUTPUT );     //GREEN}

And you need a way to turn each of the LEDs on and off. There's a few ways to do this but I just chose to make a function for each LED:

void WiFiClass::WiFiGreenLED( uint8_t setting )
{
    //green led
    WiFiDrv::digitalWrite( GREEN_LED_pin, setting );
}

void WiFiClass::WiFiRedLED( uint8_t setting )
{
    //red led
    WiFiDrv::digitalWrite( RED_LED_pin, setting );
}

void WiFiClass::WiFiBlueLED( uint8_t setting )
{
    //blue led
    WiFiDrv::digitalWrite( BLUE_LED_pin, setting );
}

In each of these functions, "setting" is just HIGH (LED on) or LOW (LED off.)

In WiFi.h, add prototypes for each of these four functions:

public:
    WiFiClass();

    void WiFiInitLEDs( void );
    void WiFiBlueLED( uint8_t setting );
    void WiFiGreenLED( uint8_t setting );
    void WiFiRedLED( uint8_t setting );

    /*
     * Get firmware version
     */
    static const char* firmwareVersion(); ...

Save and close these two files.

In your .ino sketch, you can use these as follows:

uint8_t WiFiLEDs = 0;
#define BLUE_LED    0b00000100
#define GREEN_LED   0b00000010
#define RED_LED     0b00000001

.
.
.

  // attempt to connect to Wifi network:
  WiFi.WiFiInitLEDs();
  WiFiLEDs |= BLUE_LED;
  SetWiFiLEDs(WiFiLEDs);
  
  while (status != WL_CONNECTED) 
  {
    Serial.print("Attempting to connect to "); Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
    Serial.print("Status : ");
    Serial.println(status);
    if( status == WL_CONNECTED )
      continue;

    // wait 10 seconds for connection:
    delay(10000);
    
   }//while
  WiFiLEDs = GREEN_LED;
  SetWiFiLEDs(WiFiLEDs);
  Serial.print("Connected to ");Serial.println(ssid);

.
.
.

void SetWiFiLEDs( uint8_t pattern )
{
  WiFi.WiFiRedLED((pattern & 0b00000001)?HIGH:LOW);
  WiFi.WiFiGreenLED((pattern & 0b00000010)?HIGH:LOW);
  WiFi.WiFiBlueLED((pattern & 0b00000100)?HIGH:LOW);
  
}//SetWiFiLEDs

You can use WiFi.WiFiRedLED() directly, or do something like I did that makes individual control easier or whatever.

HTH.

I´ve followed all the steps you described above, but i can´t locate de WiFiDrv class to paste this:

void WiFiClass::WiFiInitLEDs( void )
{
WiFiDrv::pinMode( BLUE_LED_pin, OUTPUT ); //BLUE
WiFiDrv::pinMode( RED_LED_pin, OUTPUT ); //RED
WiFiDrv::pinMode( GREEN_LED_pin, OUTPUT ); //GREEN}

and also this code lines:

void WiFiClass::WiFiGreenLED( uint8_t setting )
{
//green led
WiFiDrv::digitalWrite( GREEN_LED_pin, setting );
}

void WiFiClass::WiFiRedLED( uint8_t setting )
{
//red led
WiFiDrv::digitalWrite( RED_LED_pin, setting );
}

void WiFiClass::WiFiBlueLED( uint8_t setting )
{
//blue led
WiFiDrv::digitalWrite( BLUE_LED_pin, setting );
}

goes inside the WiFiDrv class?

i hope you can help me!!!

Digging through the WiFiNINA library, I found a simpler solution. Very straightforward.

WiFi.setLEDs(0, 255, 0); // Attention: green, red, blue

Topic reopened at the request of TransWarp who has something to add

Thanks for very fast reopen, awesome :slight_smile:

In WiFiNINA Version 1.8.3 (7. March 2021) is no WiFi.setLEDs(), @Lagom please add your code or leave a hint that it must be written by yourself, otherwise beginners waste time for search for it.

I answer Eduardojls question and add WiFiDrv::analogWrite():

You need to add code in only 2 Files: ?/Arduino/libraries/WiFiNINA/src/WiFi.cpp + ?/Arduino/libraries/WiFiNINA/src/WiFi.h

You can also change from digitalWrite to analogWrite, but keep that in Mind if some Issues happens.
In WiFi.cpp add at the begin between:


#include “utility/wifi_drv.h”
#include “WiFi.h”

extern “C” {
#include “utility/wl_definitions.h”
#include “utility/wl_types.h”
#include “utility/debug.h”
}

#define GREEN_LED_pin 25
#define BLUE_LED_pin 27
#define RED_LED_pin 26

WiFiClass::WiFiClass() : _timeout(50000)
{
}

void WiFiClass::init()
{
WiFiDrv::wifiDriverInit();
}

void WiFiClass::WiFiInitLEDs( void )
{
WiFiDrv::pinMode( BLUE_LED_pin, OUTPUT ); //BLUE
WiFiDrv::pinMode( RED_LED_pin, OUTPUT ); //RED
WiFiDrv::pinMode( GREEN_LED_pin, OUTPUT ); //GREEN}
}

void WiFiClass::WiFiGreenLED( uint8_t setting )
{
//green led
// WiFiDrv::digitalWrite( GREEN_LED_pin, setting );
WiFiDrv::analogWrite( GREEN_LED_pin, setting );

}

void WiFiClass::WiFiRedLED( uint8_t setting )
{
//red led
// WiFiDrv::digitalWrite( RED_LED_pin, setting );
WiFiDrv::analogWrite( RED_LED_pin, setting );

}

void WiFiClass::WiFiBlueLED( uint8_t setting )
{
//blue led
// WiFiDrv::digitalWrite( BLUE_LED_pin, setting );
WiFiDrv::analogWrite( BLUE_LED_pin, setting );
}

const char* WiFiClass::firmwareVersion()
{
return WiFiDrv::getFwVersion();
}

In WiFi.h add at the begin between:


#include “WiFiServer.h”
#include “WiFiStorage.h”

class WiFiClass
{
private:

static void init();
unsigned long _timeout;
public:
WiFiClass();

void WiFiInitLEDs( void );
void WiFiBlueLED( uint8_t setting );
void WiFiGreenLED( uint8_t setting );
void WiFiRedLED( uint8_t setting );

/*

  • Get firmware version
    /
    static const char
    firmwareVersion();

So you can set in the .ino all 16.7 Millions colors of RGB:
#include <SPI.h>
#include <WiFiNINA.h>

void setup() {
WiFi.WiFiInitLEDs();
}

void loop() {
//Yellow
WiFi.WiFiRedLED(255);
WiFi.WiFiGreenLED(255);
WiFi.WiFiBlueLED(0);
delay(500);

//Green low
WiFi.WiFiRedLED(0);
WiFi.WiFiGreenLED(12);
WiFi.WiFiBlueLED(0);
delay(500);

//Green half
WiFi.WiFiRedLED(0);
WiFi.WiFiGreenLED(127);
WiFi.WiFiBlueLED(0);
delay(500);

//Green full
WiFi.WiFiRedLED(0);
WiFi.WiFiGreenLED(255);
WiFi.WiFiBlueLED(0);
delay(500);
}

Add fading loops if you like. :sunglasses:

In WiFi.cpp you have

void WiFiClass::setLEDs(uint8_t red, uint8_t green, uint8_t blue) {
  WiFiDrv::pinMode(25, OUTPUT);
  WiFiDrv::pinMode(26, OUTPUT);
  WiFiDrv::pinMode(27, OUTPUT);
  WiFiDrv::analogWrite(25, green);
  WiFiDrv::analogWrite(26, red);
  WiFiDrv::analogWrite(27, blue);
}

hence

WiFi.setLEDs(0, 255, 0); // Attention: green, red, blue

Strange, not in my file, I use only the WiFiNINA direct from the Arduino Library Manager and added only the few lines above. (Linux, Mac or Windows should not be an issue?)
Is there another WiFi.cpp that overlays it ?

@TransWarp

Please follow the advice on posting code given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

Oh, sorry, now I see that the slash stand for code, will obey. Shall I edit my post or do subsequent changes make it worse?

Please put it in a new post so that previous comments about it still make sense

I answer Eduardojls question and add WiFiDrv::analogWrite():

You need to add code in only 2 Files: ?/Arduino/libraries/WiFiNINA/src/WiFi.cpp + ?/Arduino/libraries/WiFiNINA/src/WiFi.h

You can also change from digitalWrite to analogWrite, but keep that in Mind if some Issues happens.

In WiFi.cpp add at the begin between:

...
#include "utility/wifi_drv.h"
#include "WiFi.h"

extern "C" {
  #include "utility/wl_definitions.h"
  #include "utility/wl_types.h"
  #include "utility/debug.h"
}

#define GREEN_LED_pin   25
#define BLUE_LED_pin    27
#define RED_LED_pin     26

WiFiClass::WiFiClass() : _timeout(50000)
{
}

void WiFiClass::init()
{
    WiFiDrv::wifiDriverInit();
}

void WiFiClass::WiFiInitLEDs( void )
{
    WiFiDrv::pinMode( BLUE_LED_pin, OUTPUT );     //BLUE
    WiFiDrv::pinMode( RED_LED_pin, OUTPUT );     //RED
    WiFiDrv::pinMode( GREEN_LED_pin, OUTPUT );     //GREEN}
}


void WiFiClass::WiFiGreenLED( uint8_t setting )
{
    //green led
//    WiFiDrv::digitalWrite( GREEN_LED_pin, setting );
    WiFiDrv::analogWrite( GREEN_LED_pin, setting );

}

void WiFiClass::WiFiRedLED( uint8_t setting )
{
    //red led
//    WiFiDrv::digitalWrite( RED_LED_pin, setting );
    WiFiDrv::analogWrite( RED_LED_pin, setting );

}

void WiFiClass::WiFiBlueLED( uint8_t setting )
{
    //blue led
//    WiFiDrv::digitalWrite( BLUE_LED_pin, setting );
    WiFiDrv::analogWrite( BLUE_LED_pin, setting );
}



const char* WiFiClass::firmwareVersion()
{
	return WiFiDrv::getFwVersion();
}
...

In WiFi.h add at the begin between:

...
#include "WiFiServer.h"
#include "WiFiStorage.h"

class WiFiClass
{
private:

    static void init();
    unsigned long _timeout;
public:
    WiFiClass();
    
    void WiFiInitLEDs( void );
    void WiFiBlueLED( uint8_t setting );
    void WiFiGreenLED( uint8_t setting );
    void WiFiRedLED( uint8_t setting );

    /*
     * Get firmware version
     */
    static const char* firmwareVersion();
...

So you can set in the .ino all 16.7 Millions colors of RGB:

#include <SPI.h>
#include <WiFiNINA.h>

void setup() {
    WiFi.WiFiInitLEDs();
}

void loop() {
    //Yellow
    WiFi.WiFiRedLED(255);
    WiFi.WiFiGreenLED(255);
    WiFi.WiFiBlueLED(0);
    delay(500);

    //Green low
    WiFi.WiFiRedLED(0);
    WiFi.WiFiGreenLED(12);
    WiFi.WiFiBlueLED(0);
    delay(500);


    //Green half
    WiFi.WiFiRedLED(0);
    WiFi.WiFiGreenLED(127);
    WiFi.WiFiBlueLED(0);
    delay(500);

    //Green full
    WiFi.WiFiRedLED(0);
    WiFi.WiFiGreenLED(255);
    WiFi.WiFiBlueLED(0);
    delay(500);
}

Add fading loops if you like :sunglasses:

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