Remarks

RgbPwmLed
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for RgbPwmLed

RgbPwmLed represents an RGB LED whose color is controlled by the duty-cycle of three PWM signals. Can be used both with LEDs that have been current limited with in-series resistors, or LEDs without resistors.

Controlling an RGB LED via a PWM signal is more power efficient than using a current-limiting resistor, and it provides more control; allowing thousands of different colors, as opposed to the 8 colors of non-PWM powered RGB LED.

To use without resistors, pass in the forward voltages (voltage drop) of each of the LED components to the redLedForwardVoltage, greenLedForwardVoltage, and blueLedForwardVoltage, constructor parameters, and the class will limit its output to the maximum forward voltage rating for those LEDs.

To use with an LED that has a resistor in series, pass 0.0 or TypicalForwardVoltage.ResistorLimited for the forwardVoltage parameter.

Code Example

RgbPwmLed onboardLed;

public MeadowApp()
{
    ConfigurePeripherals();
    TestColors();
    RunColors();
}

public void ConfigurePeripherals()
{
    Console.WriteLine("Creating peripherals...");
    this.onboardLed = new RgbPwmLed(
        Device,
        Device.Pins.OnboardLedRed,
        Device.Pins.OnboardLedGreen,
        Device.Pins.OnboardLedBlue,
        commonType: CommonType.CommonAnode);
}

public void TestColors()
{
    onboardLed.SetColor(Color.Crimson);
    Thread.Sleep(3000);
    onboardLed.SetColor(Color.MediumPurple);
    Thread.Sleep(3000);
    onboardLed.SetColor(Color.FromHex("#23abe3"));
}

public void RunColors()
{
    while (true) {

        // loop through the entire hue spectrum (360 degrees)
        for (int i = 0; i < 360; i++) {
            var hue = ((double)i / 360F);
            Console.WriteLine($"Hue: {hue}");

            // set the color of the RGB
            onboardLed.SetColor(Color.FromHsba((hue), 1, 1));

            Thread.Sleep(18);
        }
    }
}

Sample project(s) available on GitHub

Example Code

The following example code loops through the entire 360º of hue spectrum and displays that color on the RGB LED.

public MeadowApp : App<F7Micro, MeadowApp>
{
    public MeadowApp()
    {
        // create a new RgbPwmLed on pin 8
        var pwmLed = new RgbPwmLed(
            redPin: Device.Pins.D11,
            greenPin: Device.Pins.D10,
            bluePin: Device.Pins.D09,
            redLedForwardVoltage: 1.05f,
            greenLedForwardVoltage: 1.5f,
            blueLedForwardVoltage: 1.5f);
        );

        // alternate between blinking and pulsing the LED 
        while (true)
        {
            for (int i = 0; i < 360; i++)
            {
                var hue = ((double)i / 360F);
                Debug.Print(hue.ToString());

                // set the color of the RGB
                rgbPwmLed.SetColor(Color.FromHsba(((double)i / 360F), 1, 1));

                // for a fun, fast rotation through the hue spectrum:
                //Thread.Sleep (1);
                // for a gentle walk through the forest of colors;
                Thread.Sleep(18);
            }
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Leds.RgbPwmLed/RgbPwmLed_Fritzing.svg"

Characteristic Locus
Inheritance System.Object > RgbPwmLed
Namespace Meadow.Foundation.Leds
Assembly Meadow.Foundation.dll

Syntax

public class RgbPwmLed : object

Constructors

RgbPwmLed(IPwmOutputController, IPin, IPin, IPin, IRgbLed.CommonType)

Create instance of RgbPwmLed

Declaration
public RgbPwmLed(IPwmOutputController device, IPin redPwmPin, IPin greenPwmPin, IPin bluePwmPin, IRgbLed.CommonType commonType = IRgbLed.CommonType.CommonCathode)

Parameters

Type Name Description
IPwmOutputController device
IPin redPwmPin
IPin greenPwmPin
IPin bluePwmPin
IRgbLed.CommonType commonType

RgbPwmLed(IPwmOutputController, IPin, IPin, IPin, Voltage, Voltage, Voltage, IRgbLed.CommonType)

Instantiates a RgbPwmLed object with the especified IO device, connected to three digital pins for red, green and blue channels, respectively

Declaration
public RgbPwmLed(IPwmOutputController device, IPin redPwmPin, IPin greenPwmPin, IPin bluePwmPin, Voltage redLedForwardVoltage, Voltage greenLedForwardVoltage, Voltage blueLedForwardVoltage, IRgbLed.CommonType commonType = IRgbLed.CommonType.CommonCathode)

Parameters

Type Name Description
IPwmOutputController device
IPin redPwmPin
IPin greenPwmPin
IPin bluePwmPin
Voltage redLedForwardVoltage
Voltage greenLedForwardVoltage
Voltage blueLedForwardVoltage
IRgbLed.CommonType commonType

RgbPwmLed(IPwmPort, IPwmPort, IPwmPort, IRgbLed.CommonType)

Create instance of RgbPwmLed

Declaration
public RgbPwmLed(IPwmPort redPwm, IPwmPort greenPwm, IPwmPort bluePwm, IRgbLed.CommonType commonType = IRgbLed.CommonType.CommonCathode)

Parameters

Type Name Description
IPwmPort redPwm
IPwmPort greenPwm
IPwmPort bluePwm
IRgbLed.CommonType commonType

RgbPwmLed(IPwmPort, IPwmPort, IPwmPort, Voltage, Voltage, Voltage, IRgbLed.CommonType)

Implementation notes: Architecturally, it would be much cleaner to construct this class as three PwmLeds. Then each one's implementation would be self-contained. However, that would require three additional threads during ON; one contained by each PwmLed. For this reason, I'm basically duplicating the functionality for all three in here.

Declaration
public RgbPwmLed(IPwmPort redPwm, IPwmPort greenPwm, IPwmPort bluePwm, Voltage redLedForwardVoltage, Voltage greenLedForwardVoltage, Voltage blueLedForwardVoltage, IRgbLed.CommonType commonType = IRgbLed.CommonType.CommonCathode)

Parameters

Type Name Description
IPwmPort redPwm
IPwmPort greenPwm
IPwmPort bluePwm
Voltage redLedForwardVoltage
Voltage greenLedForwardVoltage
Voltage blueLedForwardVoltage
IRgbLed.CommonType commonType

Properties

BlueForwardVoltage

Get the blue LED forward voltage

Declaration
public Voltage BlueForwardVoltage { get; protected set; }

Property Value

Type Description
Voltage

BluePwm

Get the blue LED port

Declaration
protected IPwmPort BluePwm { get; set; }

Property Value

Type Description
IPwmPort

Brightness

The brightness value assigned to the LED relative to Color

Declaration
public float Brightness { get; protected set; }

Property Value

Type Description
System.Single

Color

The color the LED has been set to.

Declaration
public Color Color { get; protected set; }

Property Value

Type Description
Color

Common

Gets the common type

Declaration
public IRgbLed.CommonType Common { get; protected set; }

Property Value

Type Description
IRgbLed.CommonType

GreenForwardVoltage

Get the green LED forward voltage

Declaration
public Voltage GreenForwardVoltage { get; protected set; }

Property Value

Type Description
Voltage

GreenPwm

Get the green LED port

Declaration
protected IPwmPort GreenPwm { get; set; }

Property Value

Type Description
IPwmPort

IsOn

Turns on LED with current color or turns it off

Declaration
public bool IsOn { get; set; }

Property Value

Type Description
System.Boolean

MAX_FORWARD_VOLTAGE

Maximum forward voltage (3.3 Volts)

Declaration
public Voltage MAX_FORWARD_VOLTAGE { get; }

Property Value

Type Description
Voltage

MIN_FORWARD_VOLTAGE

Minimum forward voltage (0 Volts)

Declaration
public Voltage MIN_FORWARD_VOLTAGE { get; }

Property Value

Type Description
Voltage

RedForwardVoltage

Get the red LED forward voltage

Declaration
public Voltage RedForwardVoltage { get; protected set; }

Property Value

Type Description
Voltage

RedPwm

Get the red LED port

Declaration
protected IPwmPort RedPwm { get; set; }

Property Value

Type Description
IPwmPort

Methods

ResetPwms()

Resets all PWM ports

Declaration
protected void ResetPwms()

SetColor(Color, Single)

Sets the current color of the LED.

Declaration
public void SetColor(Color color, float brightness = null)

Parameters

Type Name Description
Color color
System.Single brightness

Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting.

Declaration
public void StartBlink(Color color, float highBrightness = 1F, float lowBrightness = 0F)

Parameters

Type Name Description
Color color
System.Single highBrightness
System.Single lowBrightness

Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness setting, using the durations provided.

Declaration
public void StartBlink(Color color, TimeSpan onDuration, TimeSpan offDuration, float highBrightness = 1F, float lowBrightness = 0F)

Parameters

Type Name Description
Color color
TimeSpan onDuration
TimeSpan offDuration
System.Single highBrightness
System.Single lowBrightness

StartBlinkAsync(Color, TimeSpan, TimeSpan, Single, Single, CancellationToken)

Start blinking led

Declaration
protected Task StartBlinkAsync(Color color, TimeSpan onDuration, TimeSpan offDuration, float highBrightness, float lowBrightness, CancellationToken cancellationToken)

Parameters

Type Name Description
Color color

color to blink

TimeSpan onDuration

on duration in ms

TimeSpan offDuration

off duration in ms

System.Single highBrightness

maximum brightness

System.Single lowBrightness

minimum brightness

CancellationToken cancellationToken

token to cancel blink

Returns

Type Description
Task

StartPulse(Color, Single, Single)

Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting.

Declaration
public void StartPulse(Color color, float highBrightness = null, float lowBrightness = 0.15F)

Parameters

Type Name Description
Color color
System.Single highBrightness
System.Single lowBrightness

StartPulse(Color, TimeSpan, Single, Single)

Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the durations provided.

Declaration
public void StartPulse(Color color, TimeSpan pulseDuration, float highBrightness = null, float lowBrightness = 0.15F)

Parameters

Type Name Description
Color color
TimeSpan pulseDuration
System.Single highBrightness
System.Single lowBrightness

StartPulseAsync(Color, TimeSpan, Single, Single, CancellationToken)

Start led pulsing

Declaration
protected Task StartPulseAsync(Color color, TimeSpan pulseDuration, float highBrightness, float lowBrightness, CancellationToken cancellationToken)

Parameters

Type Name Description
Color color

color to pulse

TimeSpan pulseDuration

pulse duration in ms

System.Single highBrightness

maximum brightness

System.Single lowBrightness

minimum brightness

CancellationToken cancellationToken

token to cancel pulse

Returns

Type Description
Task

Stop()

Stops any running animations.

Declaration
public void Stop()