Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

Characteristic Locus
Inheritance object RotaryEncoder > RotaryEncoderWithButton
Implements IDisposable IRotaryEncoderWithButton IRotaryEncoder IButton ISensor<bool>
Inherited Members RotaryEncoder.Rotated RotaryEncoder.APhasePort RotaryEncoder.BPhasePort RotaryEncoder.LastDirectionOfRotation RotaryEncoder.IsDisposed RotaryEncoder.Dispose() RotaryEncoder.Dispose(bool) object.Equals(object) object.Equals(object, object) object.GetHashCode() object.GetType() object.MemberwiseClone() object.ReferenceEquals(object, object) object.ToString()
Namespace Meadow.Foundation.Sensors.Rotary
Assembly Meadow.Foundation.dll

Syntax

public class RotaryEncoderWithButton : RotaryEncoder, IDisposable, IRotaryEncoderWithButton, IRotaryEncoder, IButton, ISensor<bool>

Constructors

RotaryEncoderWithButton(IPin, IPin, IPin, ResistorMode)

Instantiates a new RotaryEncoder on the specified pins that has an integrated button.

Declaration
public RotaryEncoderWithButton(IPin aPhasePin, IPin bPhasePin, IPin buttonPin, ResistorMode buttonResistorMode = ResistorMode.InternalPullDown)

Parameters

Type Name Description
IPin aPhasePin
IPin bPhasePin
IPin buttonPin
ResistorMode buttonResistorMode

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

Properties

Button

Returns the PushButton that represents the integrated button

Declaration
public PushButton Button { get; }

Property Value

Type Description
PushButton

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

LongClickedThreshold

The minimum duration for a long press

Declaration
public TimeSpan LongClickedThreshold { get; set; }

Property Value

Type Description
TimeSpan

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

State

Returns the push button's state

Declaration
public bool State { get; }

Property Value

Type Description
bool

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

Methods

ButtonClicked(object, EventArgs)

Method when button is clicked (down then up)

Declaration
protected void ButtonClicked(object sender, EventArgs e)

Parameters

Type Name Description
object sender

sender object

EventArgs e

event arguments

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

ButtonPressEnded(object, EventArgs)

Method called when button press is started (up state)

Declaration
protected void ButtonPressEnded(object sender, EventArgs e)

Parameters

Type Name Description
object sender

sender object

EventArgs e

event arguments

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

ButtonPressStarted(object, EventArgs)

Method called when button press is started (down state)

Declaration
protected void ButtonPressStarted(object sender, EventArgs e)

Parameters

Type Name Description
object sender

sender object

EventArgs e

event arguments

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

Read()

Convenience method to get the current sensor reading

Declaration
public Task<bool> Read()

Returns

Type Description
Task<bool>

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

Events

Clicked

Raised when the button circuit is re-opened after it has been closed

Declaration
public event EventHandler Clicked

Event Type

Type Description
EventHandler

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

LongClicked

Raised when the button circuit is pressed for LongPressDuration

Declaration
public event EventHandler LongClicked

Event Type

Type Description
EventHandler

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

PressEnded

Raised when a press ends

Declaration
public event EventHandler PressEnded

Event Type

Type Description
EventHandler

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"

PressStarted

Raised when a press starts

Declaration
public event EventHandler PressStarted

Event Type

Type Description
EventHandler

Remarks

RotaryEncoderWithButton
Status Status badge: working
Source code GitHub
NuGet package NuGet Gallery for Meadow.Foundation

Rotary encoders are similar in form factor to potentiometers, but instead of modifying a voltage output, they send a digital signal encoded using Gray Code when rotated that can be decoded to ascertain the direction of turn.

Rotary encoders have several advantages over potentiometers as input devices, namely:

  • They’re more power efficient; they only use power when actuated.
  • They’re not rotation-bound; they spin infinitely in either direction.
  • Many rotary encoders also have a built-in pushbutton.

Rotary encoders are used almost exclusively on things like volume knobs on stereos.

And because they’re not rotation bound, they are especially useful in the case in which a device might have multiple inputs to control the same parameter. For instance, a stereo’s volume might be controlled via a knob and a remote control. If a potentiometer were used for the volume knob, then the actual volume could get out of synch with the apparent value on the potentiometer when the volume was changed via the remote.

For this reason, rotary encoders are particularly useful in connected things, in which parameters might be controlled remotely.

Code Example

protected int value = 0;
protected RotaryEncoderWithButton rotaryEncoder;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing Hardware...");

    // Note: on the rotary encoder in the hack kit, the pinout is as
    // follows:
    //
    // | Encoder Name | Driver Pin Name |
    // |--------------|-----------------|
    // | `SW`         | `buttonPin`     |
    // | `DT`         | `aPhasePin`     |
    // | `CLK`        | `bPhasePin`     |

    // initialize the encoder
    rotaryEncoder = new RotaryEncoderWithButton(Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);

    //==== Classic Events
    rotaryEncoder.Rotated += RotaryEncoder_Rotated;

    rotaryEncoder.Clicked += (s, e) => Resolver.Log.Info("Button Clicked");

    rotaryEncoder.PressEnded += (s, e) => Resolver.Log.Info("Press ended");

    rotaryEncoder.PressStarted += (s, e) => Resolver.Log.Info("Press started");

    Resolver.Log.Info("Hardware initialization complete.");

    return Task.CompletedTask;
}

private void RotaryEncoder_Rotated(object sender, RotaryChangeResult e)
{
    switch (e.New)
    {
        case RotationDirection.Clockwise:
            value++;
            Resolver.Log.Info($"/\\ Value = {value} CW");
            break;
        case RotationDirection.CounterClockwise:
            value--;
            Resolver.Log.Info($"\\/ Value = {value} CCW");
            break;
    }
}

Sample project(s) available on GitHub

###Two-bit Gray Code

This rotary encoder driver works with most rotary encoders which return a two-bit Gray Code which is the minimum number of bits necessary to describe direction. Most common rotary encoders use two-bit Gray Code, so this driver should work with most common rotary encoders.

###PushButton

Some rotary encoders, such as the ones pictured above, have an integrated push button. This driver exposes that button as a PushButton via the Button property.

The following example shows how to register event handlers to print in the console when pressing and relasing the push button:

public class MeadowApp : App<F7Micro, MeadowApp>
{
    protected RotaryEncoderWithButton _rotary = null;
    protected PwmLed _led = null;
    // how much to change the brightness per rotation step. 
    // 0.05 = 20 clicks to 100%
    protected float _brightnessStepChange = 0.05F; 

    public MeadowApp()
    {
        // instantiate our peripherals
        _rotary = new RotaryEncoderWithButton(Device, Device.Pins.D07, Device.Pins.D08, Device.Pins.D06);
        _rotary.Rotated += RotaryRotated;
        _rotary.Clicked += RotaryClicked;

        _led = new PwmLed(Device.Pins.D12, TypicalForwardVoltage.Red);
    }

    protected void RotaryRotated(object sender, RotaryTurnedEventArgs e)
    {
        // if clockwise, turn it up! clamp to 1, so we don't go over.
        if (e.Direction == RotationDirection.Clockwise)
        {
            if(_led.Brightness >= 1) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness + 
                    _brightnessStepChange).Clamp(0,1));
            }
        } 
        else // otherwise, turn it down. clamp to 0 so we don't go below. 
        { 
            if (_led.Brightness <= 0) 
            {
                return;
            } 
            else 
            {
                _led.SetBrightness((_led.Brightness - 
                    _brightnessStepChange).Clamp(0,1));
            }
        }
    }

    private void RotaryClicked(object sender, EventArgs e)
    {
        if (_led.Brightness > 0) 
        {
            _led.SetBrightness(0f);
        } 
        else 
        {
            _led.SetBrightness(_lastOnBrightness);
        }
    }
}

Sample projects available on GitHub

Wiring Example

<img src="../../API_Assets/Meadow.Foundation.Sensors.Rotary.RotaryEncoderWithButton/RotaryEncoderWithButton_Fritzing.svg"