Skip to main content

Meadow.Foundation.Leds.RgbPwmLed

RgbPwmLed
StatusStatus badge: working
Source codeGitHub
NuGet packageNuGet Gallery for Meadow.Foundation

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 override Task Initialize()
{
Resolver.Log.Info("Creating peripherals...");

onboardLed = new RgbPwmLed(
Device.Pins.OnboardLedRed,
Device.Pins.OnboardLedGreen,
Device.Pins.OnboardLedBlue);

return Task.CompletedTask;
}

public override Task Run()
{
return TestColors();
}

public async Task TestColors()
{
while (true)
{
Resolver.Log.Info("SetColor(RgbLedColors.Red);");
onboardLed.SetColor(Color.Red);
await Task.Delay(3000);

Resolver.Log.Info("StartPulse();");
await onboardLed.StartPulse();
await Task.Delay(3000);

Resolver.Log.Info("StartPulse(RgbLedColors.Green);");
await onboardLed.StartPulse(Color.Green);
await Task.Delay(3000);

Resolver.Log.Info("SetColor(RgbLedColors.Yellow);");
onboardLed.SetColor(Color.Yellow);
await Task.Delay(3000);

Resolver.Log.Info("StartPulse(RgbLedColors.Cyan, 200, 200);");
await onboardLed.StartPulse(Color.Cyan, TimeSpan.FromMilliseconds(400));
await Task.Delay(3000);

await onboardLed.StopAnimation();
}
}

Sample project(s) available on GitHub

Wiring Example

Class RgbPwmLed

Utility functions to provide blinking and pulsing for RgbPwmLed

Assembly: Meadow.Foundation.dll
View Source
Declaration
public class RgbPwmLed : IRgbPwmLed, IPwmLed, IRgbLed, ILed, IDisposable

Implements:
Meadow.Peripherals.Leds.IRgbPwmLed, Meadow.Peripherals.Leds.IPwmLed, Meadow.Peripherals.Leds.IRgbLed, Meadow.Peripherals.Leds.ILed, System.IDisposable

Properties

MAX_FORWARD_VOLTAGE

Maximum forward voltage (3.3 Volts)

View Source
Declaration
public Voltage MAX_FORWARD_VOLTAGE { get; }

MIN_FORWARD_VOLTAGE

Minimum forward voltage (0 Volts)

View Source
Declaration
public Voltage MIN_FORWARD_VOLTAGE { get; }

IsOn

Gets or sets a value indicating whether the LED is on.

View Source
Declaration
public bool IsOn { get; set; }

Color

The current LED color

View Source
Declaration
public Color Color { get; protected set; }

Brightness

Gets the brightness of the LED, controlled by a PWM signal

View Source
Declaration
public float Brightness { get; protected set; }

RedPwm

The red LED port

View Source
Declaration
protected IPwmPort RedPwm { get; set; }

BluePwm

The blue LED port

View Source
Declaration
protected IPwmPort BluePwm { get; set; }

GreenPwm

The green LED port

View Source
Declaration
protected IPwmPort GreenPwm { get; set; }

Common

The common type (common anode or common cathode)

View Source
Declaration
public CommonType Common { get; protected set; }

RedForwardVoltage

The red LED forward voltage

View Source
Declaration
public Voltage RedForwardVoltage { get; protected set; }

GreenForwardVoltage

The green LED forward voltage

View Source
Declaration
public Voltage GreenForwardVoltage { get; protected set; }

BlueForwardVoltage

The blue LED forward voltage

View Source
Declaration
public Voltage BlueForwardVoltage { get; protected set; }

IsDisposed

Is the object disposed

View Source
Declaration
public bool IsDisposed { get; }

Methods

StopAnimation()

Stops blink animation.

View Source
Declaration
public Task StopAnimation()
Returns

System.Threading.Tasks.Task

Blink animation that turns the LED on (500ms) and off (500ms)

View Source
Declaration
public Task StartBlink()
Returns

System.Threading.Tasks.Task

StartBlink(RgbLedColors)

Start the Blink animation which sets turns the LED on and off on an interval of 1 second (500ms on, 500ms off)

View Source
Declaration
public Task StartBlink(RgbLedColors color)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
Meadow.Peripherals.Leds.RgbLedColorscolorThe LED color

StartBlink(TimeSpan, TimeSpan)

Blink animation that turns the LED on and off based on the OnDuration and offDuration values in ms

View Source
Declaration
public Task StartBlink(TimeSpan onDuration, TimeSpan offDuration)
Returns

System.Threading.Tasks.Task

Parameters
TypeName
System.TimeSpanonDuration
System.TimeSpanoffDuration

StartBlink(RgbLedColors, TimeSpan, TimeSpan)

Start the Blink animation which sets turns the LED on and off with the specified durations and color

View Source
Declaration
public Task StartBlink(RgbLedColors color, TimeSpan onDuration, TimeSpan offDuration)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
Meadow.Peripherals.Leds.RgbLedColorscolorThe LED color
System.TimeSpanonDurationThe duration the LED stays on
System.TimeSpanoffDurationThe duration the LED stays off

StartBlink(float, float)

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

View Source
Declaration
public Task StartBlink(float highBrightness = 1, float lowBrightness = 0)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
System.SinglehighBrightnessThe maximum brightness of the animation
System.SinglelowBrightnessThe minimum brightness of the animation

StartBlink(Color, float, float)

Start the Blink animation which sets the brightness of the LED alternating between a low and high brightness on an interval of 1 second (500ms on, 500ms off)

View Source
Declaration
public Task StartBlink(Color color, float highBrightness = 1, float lowBrightness = 0)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
Meadow.ColorcolorThe LED color
System.SinglehighBrightnessThe maximum brightness of the animation
System.SinglelowBrightnessThe minimum brightness of the animation

StartBlink(TimeSpan, TimeSpan, float, float)

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

View Source
Declaration
public Task StartBlink(TimeSpan onDuration, TimeSpan offDuration, float highBrightness = 1, float lowBrightness = 0)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
System.TimeSpanonDuration
System.TimeSpanoffDuration
System.SinglehighBrightnessThe maximum brightness of the animation
System.SinglelowBrightnessThe minimum brightness of the animation

StartBlink(Color, TimeSpan, TimeSpan, float, float)

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

View Source
Declaration
public Task StartBlink(Color color, TimeSpan onDuration, TimeSpan offDuration, float highBrightness = 1, float lowBrightness = 0)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
Meadow.ColorcolorThe LED color
System.TimeSpanonDurationThe duration the LED stays on
System.TimeSpanoffDurationThe duration the LED stays off
System.SinglehighBrightnessThe maximum brightness of the animation
System.SinglelowBrightnessThe minimum brightness of the animation

StartPulse(float, float)

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

View Source
Declaration
public Task StartPulse(float highBrightness = 1, float lowBrightness = 0.15)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
System.SinglehighBrightnessThe maximum brightness of the animation
System.SinglelowBrightnessThe minimum brightness of the animation

StartPulse(Color, float, float)

Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting with a cycle time of 600ms

View Source
Declaration
public Task StartPulse(Color color, float highBrightness = 1, float lowBrightness = 0.15)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
Meadow.ColorcolorThe LED color
System.SinglehighBrightnessThe maximum brightness of the animation
System.SinglelowBrightnessThe minimum brightness of the animation

StartPulse(Color, TimeSpan, float, float)

Start the Pulse animation which gradually alternates the brightness of the LED between a low and high brightness setting, using the duration provided and specified color

View Source
Declaration
public Task StartPulse(Color color, TimeSpan pulseDuration, float highBrightness = 1, float lowBrightness = 0.15)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
Meadow.ColorcolorThe LED color
System.TimeSpanpulseDurationThe pulse animation duration
System.SinglehighBrightnessThe maximum brightness of the animation
System.SinglelowBrightnessThe minimum brightness of the animation

StartPulse(TimeSpan, float, float)

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

View Source
Declaration
public Task StartPulse(TimeSpan pulseDuration, float highBrightness = 1, float lowBrightness = 0.15)
Returns

System.Threading.Tasks.Task

Parameters
TypeNameDescription
System.TimeSpanpulseDurationThe pulse animation duration
System.SinglehighBrightnessThe maximum brightness of the animation
System.SinglelowBrightnessThe minimum brightness of the animation

ValidateBrightness(float, float)

Validates LED brightness to ensure they're within the range 0 (off) - 1 (full brightness)

View Source
Declaration
protected void ValidateBrightness(float highBrightness, float lowBrightness)
Parameters
TypeNameDescription
System.SinglehighBrightnessThe maximum brightness of the animation
System.SinglelowBrightnessThe minimum brightness of the animation

ValidateForwardVoltages(Voltage, Voltage, Voltage)

Validates forward voltages to ensure they're within the range MIN_FORWARD_VOLTAGE to MAX_FORWARD_VOLTAGE

View Source
Declaration
protected void ValidateForwardVoltages(Voltage redLedForwardVoltage, Voltage greenLedForwardVoltage, Voltage blueLedForwardVoltage)
Parameters
TypeNameDescription
Meadow.Units.VoltageredLedForwardVoltageThe forward voltage for the red LED
Meadow.Units.VoltagegreenLedForwardVoltageThe forward voltage for the green LED
Meadow.Units.VoltageblueLedForwardVoltageThe forward voltage for the blue LED

ResetPwmPorts()

Resets all PWM ports

View Source
Declaration
protected void ResetPwmPorts()

SetBrightness(float)

Set the led brightness

View Source
Declaration
public void SetBrightness(float brightness)
Parameters
TypeNameDescription
System.SinglebrightnessValid values are from 0 to 1, inclusive

SetColor(Color, float)

Sets the current color of the LED

View Source
Declaration
public void SetColor(Color color, float brightness = 1)
Parameters
TypeNameDescription
Meadow.ColorcolorThe LED color
System.SinglebrightnessValid values are from 0 to 1, inclusive

SetColor(RgbLedColors)

Sets the current color of the LED.

View Source
Declaration
public void SetColor(RgbLedColors color)
Parameters
TypeNameDescription
Meadow.Peripherals.Leds.RgbLedColorscolorThe color value

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

View Source
Declaration
public void Dispose()

Dispose(bool)

Dispose of the object

View Source
Declaration
public virtual void Dispose(bool disposing)
Parameters
TypeNameDescription
System.BooleandisposingIs disposing

Implements

  • Meadow.Peripherals.Leds.IRgbPwmLed
  • Meadow.Peripherals.Leds.IPwmLed
  • Meadow.Peripherals.Leds.IRgbLed
  • Meadow.Peripherals.Leds.ILed
  • System.IDisposable