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

Syntax

public class PwmLed : IPwmLed

Remarks

Represents an LED whose voltage (and brightness) is controlled by the duty-cycle of a PWM signal. Can be used both with LEDs that have been current limited with in-series resistors, or LEDs without resistors.

Controlling an LED via a PWM signal is more power efficient than using a current-limiting resistor, and it provides more control; allowing multiple grades of brightness. However, it uses a PWM channel.

To use without resistors, pass in the forward voltage (voltage drop) of the LED to the forwardVoltage constructor parameter, and the class will limit its output to the maximum forward voltage rating of the LED.

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

Examples

The following example alternates between blinking and pulsing an LED:

using System.Threading;
using Meadow;
using Meadow.Devices;
using Meadow.Foundation.Leds;

namespace PwmLedSample
{
    public class Program
    {
        static IApp _app;
        public static void Main()
        {
            _app = new MeadowApp();
        }
    }

    public class MeadowApp : App<F7Micro, MeadowApp>
    {
        public MeadowApp()
        {
            // create a new PwmLed on pin 8
            var pwmLed = new PwmLed(
                Device.CreatePwmPort(Device.Pins.D08),
                TypicalForwardVoltage.Green
            );

            // alternate between blinking and pulsing the LED 
            while (true)
            {
                pwmLed.StartBlink();
                Thread.Sleep(5000); // 5 seconds

                pwmLed.StartPulse(lowBrightness: 0.2F);
                Thread.Sleep(10000); // 10 seconds
            }
        }
    }
}
Example Circuit

Constructors

View Source

PwmLed(IIODevice, IPin, Single)

Initializes a new instance of the PwmLed class.

Declaration
public PwmLed(IIODevice device, IPin pin, float forwardVoltage)

Parameters

Type Name Description
IIODevice device

IO Device

IPin pin

Pin

System.Single forwardVoltage

Forward voltage

View Source

PwmLed(IPwmPort, Single)

Creates a new PwmLed on the specified PWM pin and limited to the appropriate voltage based on the passed forwardVoltage. Typical LED forward voltages can be found in the TypicalForwardVoltage class.

Declaration
public PwmLed(IPwmPort pwmPort, float forwardVoltage)

Parameters

Type Name Description
IPwmPort pwmPort
System.Single forwardVoltage

Fields

View Source

_animationThread

Declaration
protected Thread _animationThread

Field Value

Type Description
System.Threading.Thread
View Source

_isOn

Declaration
protected bool _isOn

Field Value

Type Description
System.Boolean
View Source

_maximumPwmDuty

Declaration
protected float _maximumPwmDuty

Field Value

Type Description
System.Single
View Source

_running

Declaration
protected bool _running

Field Value

Type Description
System.Boolean

Properties

View Source

Brightness

Gets the brightness of the LED, controlled by a PWM signal, and limited by the calculated maximum voltage. Valid values are from 0 to 1, inclusive.

Declaration
public float Brightness { get; }

Property Value

Type Description
System.Single
View Source

ForwardVoltage

Gets the forward voltage value

Declaration
public float ForwardVoltage { get; protected set; }

Property Value

Type Description
System.Single
View Source

IsOn

Gets or Sets the state of the LED

Declaration
public bool IsOn { get; set; }

Property Value

Type Description
System.Boolean
View Source

Port

Declaration
protected IPwmPort Port { get; set; }

Property Value

Type Description
IPwmPort

Methods

View Source

SetBrightness(Single)

Sets the LED to a especific brightness.

Declaration
public void SetBrightness(float brightness)

Parameters

Type Name Description
System.Single brightness

Valid values are from 0 to 1, inclusive

View Source

Starts the blink animation.

Declaration
public void StartBlink(uint onDuration = 200U, uint offDuration = 200U)

Parameters

Type Name Description
System.UInt32 onDuration

On duration.

System.UInt32 offDuration

Off duration.

View Source

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(uint onDuration, uint offDuration, float highBrightness, float lowBrightness)

Parameters

Type Name Description
System.UInt32 onDuration
System.UInt32 offDuration
System.Single highBrightness
System.Single lowBrightness
View Source

StartPulse(Int32, 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(int pulseDuration = 600, float highBrightness = 1F, float lowBrightness = 0.15F)

Parameters

Type Name Description
System.Int32 pulseDuration
System.Single highBrightness
System.Single lowBrightness
View Source

Stop()

Stops any running animations.

Declaration
public void Stop()