Show Rainbow Colors With An Rgb Led And Netduino

About the project

Learn how you can control a RGB LED with Netduino. Foundation, a powerful platform to build connected things quickly and easily with NETMF.

Project info

Difficulty: Easy

Platforms: MicrosoftNetduino

Estimated time: 1 hour

License: GNU General Public License, version 3 or later (GPL3+)

Items used in this project

Hardware components

Netduino3 WiFi Netduino3 WiFi x 1
Netduino3 Ethernet Netduino3 Ethernet x 1
Netduino3 Netduino3 x 1
Netduino Plus 2 Netduino Plus 2 x 1
Netduino 2 Netduino 2 x 1
SparkFun LED - RGB Clear Common Anode SparkFun LED - RGB Clear Common Anode x 1
Breadboard (generic) Breadboard (generic) x 1

Software apps and online services

Microsoft Visual Studio 2015 Microsoft Visual Studio 2015

Story

This project uses the RgbPwmLed class that is included in the Netduino.Foundation Library

This guide consists of three parts:

  • Part 1: Cycle through the Red, Green and Blue pins on a RGB LED to ensure they are wired up correctly.
  • Part 2: Make the LED glow through all the possible hues with just a few lines of code.

RGB (Red, Green, Blue) LED lights are diodes that have four legs - one for each of the colors mentioned and one for a common cathode (ground) or common anode (vcc), which is also the longest one.

Common Cathode and Common Anode RGB LEDsCommon Cathode and Common Anode RGB LEDs

In this project, you can work with either common anode or common cathode RGB LED. Just make sure to build the circuit that corresponds to the LED you're using.

Netduino.Foundation is a platform for quickly and easily build connected things using the.NET MicroFramework on Netduino. Created by Wilderness Labs,

 it's completely open source and maintained by the Netduino community.

If you're new in Netduino development, I suggest you go to the Getting Started With Netduino project to properly set up your development environment.

Assemble the circuit

Common Cathode RGB LED

Circuit of a common cathode RGB LED

Common Anode RGB LED

Circuit of a common anode RGB LEDCircuit of a common anode RGB LED

Note: Notice that resisters aren't used in neither circuits. Normally, passing full voltage can burn the LED. However, Netduino.Foundation

 can automatically control the forward voltage using a PWM signal.

Create a Netduino Project

Create a Netduino Project in Visual Studio 2015 for Windows or the latest Visual Studio for Mac; name the project RgbPwmLed.

Add the Netduino.Foundation NuGet Package

Windows:

Right-click on your 

RgbPwmLed 

project and click 

Manage NuGet Packages

. In the 

Browse 

tab, search for 

Netduino.Foundation; 

it should be the first search result. Click the 

Install 

button.

MacOS

Alt-click on your RgbPwmLed project in the Solution Explorer, and click Add => Add Nuget Package to open the Nuget Package window. Search for the Netduino.Foundation package and click Add Package to add it to your project.

Part 1: Making the RGB LED glow Red, Green, and Blue

In your Project.cs 

 file, paste the following code:

  1. using

    Microsoft

    .

    SPOT

    ;

  2. using

    Netduino

    .

    Foundation

    ;

  3. using

    System

    .

    Threading

    ;

  4. using

    N

    =

    SecretLabs

    .

    NETMF

    .

    Hardware

    .

    Netduino

    ;

  5. namespace

    RgbPwmLed

  6. {

  7. public

    class

    Program

  8. {

  9. public

    static

    void

    Main

    ()

  10. {

  11. // create a new pwm controlled RGB LED on pins

  12. // Red = 11, Green = 10, and Blue = 9.

  13. var

    rgbPwmLed

    =

    new

    Netduino

    .

    Foundation

    .

    LEDs

    .

    RgbPwmLed

    (

  14. N

    .

    PWMChannels

    .

    PWM_PIN_D11

    ,

  15. N

    .

    PWMChannels

    .

    PWM_PIN_D10

    ,

  16. N

    .

    PWMChannels

    .

    PWM_PIN_D9

    ,

  17. 2.1f

    ,

  18. 3.0f

    ,

  19. 3.0f

    ,

  20. false

    );

  21. // run forever

  22. while

    (

    true

    )

  23. {

  24. rgbPwmLed

    .

    SetColor

    (

    Color

    .

    FromHex

    (

    "#FF0000"

    ));

  25. Debug

    .

    Print

    (

    "=========== RED! ==========="

    );

  26. Thread

    .

    Sleep

    (

    1000

    );

  27. rgbPwmLed

    .

    SetColor

    (

    Color

    .

    FromHex

    (

    "#00FF00"

    ));

  28. Debug

    .

    Print

    (

    "=========== GREEN! ==========="

    );

  29. Thread

    .

    Sleep

    (

    1000

    );

  30. rgbPwmLed

    .

    SetColor

    (

    Color

    .

    FromHex

    (

    "#0000FF"

    ));

  31. Debug

    .

    Print

    (

    "=========== BLUE! ==========="

    );

  32. Thread

    .

    Sleep

    (

    1000

    );

  33. }

  34. }

  35. }

  36. }

In the Main method, we created a new RgbPwmLed object named rgbPwmLed. In the constructor, we're specifying the pins using to control the LED. Since we're not using resistors, we are including the forward voltage for each pin separately (red is 2.1V, and green and blue 3.0V).

 The last parameter in the constructor is optional and its the boolean is CommonCathode flag 

(true by default). Set this value accordingly based on the circuit you're working on.

Now with the rgbPwmLed 

object, inside a while infinite loop, we're invoking the SetColor(Color color) 

method, passing the Red, Green and Blue colors in Hex code, and putting the thread to sleep for one second intervals. You can also declare colors specifying in RGB or HSA values, and even predefined named colors.

Additionally, to keep track on which color is currently showing on the LED, you can use Debug.Print 

to indicate what color is the program showing at all times, just to make sure you wired all the LED legs correctly.

Run the Project

Click the run button in Visual Studio to see your RGB LED in action! You should see the LED cycling through red, green and blue colors every second, like to the following gif:

Netduino cycling pins on a RGB LED

Part 2: Making the RGB LED glow through all hues

In this part, lets spice up the project a bit. Let's write a small algorithm to go through all 360 possible colors in the Hue spectrum. Why 360? Using the Hue, Saturation, Brightness and Alpha color model, this has a cylindrical geometry, and going through each degree at full saturation and alpha values, it will display all colors inside the RGB spectrum.

Delete the code inside the While(true) 

cycle, and paste the For 

loop instead. It should look like this:

  1. ...

  2. while

    (

    true

    )

  3. {

  4. // loop through the entire hue spectrum (360 degrees)

  5. for

    (

    int

    i

    =

    0

    ;

    i

    <

    360

    ;

    i

    ++)

  6. {

  7. var

    hue

    =

    ((

    double

    )

    i

    /

    360F

    );

  8. Debug

    .

    Print

    (

    hue

    .

    ToString

    ());

  9. // set the color of the RGB

  10. rgbPwmLed

    .

    SetColor

    (

    Color

    .

    FromHsba

    (((

    double

    )

    i

    /

    360F

    ),

    1

    ,

    1

    ));

  11. // for a fun, fast rotation through the hue spectrum:

  12. //Thread.Sleep (1);

  13. // for a moderate walk through the forest of colors;

  14. Thread

    .

    Sleep

    (

    10

    );

  15. }

  16. }

  17. ...

Inside this For, we're declaring a local double hue and assign the result of dividing i/360. After that, we call the SetColor(Color.FromHsba(hue, 1, 1)) method on the rgbPwmLed object, and finally do a Thread.Sleep(10) to see all the colors as a moderate speed.

Run the Project

When you run the project now, you should see something like this:

RGB LED cycling through all hue colors

Part 3: Using StartRunningColors method

Another useful API method that you should check out is StartRunning Colors, which consist on passing a list of Colors along with time durations for each one. The following code snippet makes the LED glow Color.White 

for 1000ms (1s), then Color.Aqua 

for 500ms (0.5s), then Color.ForrestGreen 

for 1000ms (1s), and finally Color.OrangeRed 

for 500ms (0.5s).

  1. ...

  2. while

    (

    true

    )

  3. {

  4. // run through various colors for 10 seconds

  5. rgbPwmLed

    .

    StartRunningColors

    (

  6. new

    System

    .

    Collections

    .

    ArrayList

  7. {

  8. Color

    .

    White

    ,

  9. Color

    .

    Aqua

    ,

  10. Color

    .

    ForestGreen

    ,

  11. Color

    .

    OrangeRed

  12. },

  13. new

    int

    []

    {

    1000

    ,

    500

    ,

    1000

    ,

    500

    });

  14. Thread

    .

    Sleep

    (

    10000

    );

  15. }

  16. ...

Run the Project

When you run the project now, you'll see something like this:

RGB LED using StartRunningColors

I suggest you check the RgbPwmLed official documentation so you can see what other interesting API methods are available to make your work easier when working with RGB LEDs.

Check out Netduino.Foundation!

This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Netduino.Foundation.

  • It comes with a 

    Huge Peripheral Driver Library with drivers for the most common sensors and peripherals available in the market.
  • All the peripheral drivers are simplified with built-in functionality, exposed by a clean, modern API.
  • This project is backed by a growing community that is constantly working on building cool connected things and always excited to help new-comers and discuss new projects.

References

Code

RgbPwmLed

Code used for this project

RgbPwmLed

Code used for this project

Credits

Photo of Wilderness Labs

Wilderness Labs

Creators of Meadow. Makers of Netduino. We power connected things.

   

Leave your feedback...