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

Syntax

public class Led : ILed

Remarks

An LED is a diode (a component that attempts to let current flow only one way through it) that emits light when voltage is applied to it. Because it is a diode, it only works when a voltage is applied one way. For this reason, LEDs usually have one lead longer (the Cathode) than the other (the Anode). The cathode (longer leg) indicates that it should be hooked to the positive (+) side of a circuit.

Unlike the PwmLed, in which you can pass the forward voltage (voltage drop) of the led to limit its current, to use Led it is required to limit the current to the LED using a resistor.

Examples

The following example shows how to turn on and off the LED using the IsOn property, and uses a StartBlink(onDuration, offDuration) API method to make the LED blink staying on for 500ms (0.5s) and off for 1000ms (1s):

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

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

    public class LedApp : App<F7Micro, LedApp>
    {
        public LedApp()
        {
            // create a new Led on pin 8
            var led = new Led(Device.CreateDigitalOutputPort(Device.Pins.D08));

            while (true)
            {
                led.IsOn = true;    // Led ON
                Thread.Sleep(3000); // 3 seconds
                led.IsOn = false;   // Led OFF
                Thread.Sleep(2000); // 2 seconds
                led.IsOn = true;    // Led ON
                Thread.Sleep(1000); // 1 second

                led.StartBlink(500, 1000);
                Thread.Sleep(5000); // 5 seconds
                led.Stop();
                Thread.Sleep(1500); // 1.5 seconds, ensure last blink cycle is complete
            }
        }
    }
}
Example Circuit

Constructors

View Source

Led(IDigitalOutputPort)

Creates a LED through a DigitalOutPutPort from an IO Expander

Declaration
public Led(IDigitalOutputPort port)

Parameters

Type Name Description
IDigitalOutputPort port
View Source

Led(IIODevice, IPin)

Creates a LED through a pin directly from the Digital IO of the board

Declaration
public Led(IIODevice device, IPin pin)

Parameters

Type Name Description
IIODevice device
IPin pin

Fields

View Source

_animationTask

Declaration
protected Task _animationTask

Field Value

Type Description
System.Threading.Tasks.Task
View Source

_isOn

Declaration
protected bool _isOn

Field Value

Type Description
System.Boolean
View Source

_running

Declaration
protected bool _running

Field Value

Type Description
System.Boolean

Properties

View Source

IsOn

Gets or sets a value indicating whether this Led is on.

Declaration
public bool IsOn { get; set; }

Property Value

Type Description
System.Boolean

true if is on; otherwise, false.

View Source

Port

Gets the port that is driving the LED

Declaration
public IDigitalOutputPort Port { get; protected set; }

Property Value

Type Description
IDigitalOutputPort

The port

Methods

View Source

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

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

Parameters

Type Name Description
System.UInt32 onDuration
System.UInt32 offDuration
View Source

Stop()

Stops the LED when its blinking and/or turns it off.

Declaration
public void Stop()