markneustadt.com

Netduino Rotary Encoder Demo

In this demonstration, I’ve hooked up a rotary encoder to the Netduino with three LEDs. As you turn the knob on the rotary encoder, it increases or decreases a value in the program. Then, based on the value, it will light up or turn off the LEDs.

RotaryEncoder

Download the Fritzing RotaryEncoder.fzz schematic

You’ll need to download the RotaryEncoder library here.
Download the project files here

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using Toolbox.NETMF.Hardware;

namespace RotaryEncoderDemo
{
    public class Program
    {

        static OutputPort red = new OutputPort(Pins.GPIO_PIN_D7, false);
        static OutputPort ylw = new OutputPort(Pins.GPIO_PIN_D6, false);
        static OutputPort grn = new OutputPort(Pins.GPIO_PIN_D5, false);

        static int currentValue = 0;

        public static void Main()
        {
            // Initializes a new rotary encoder object
            RotaryEncoder Knob = new RotaryEncoder(Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1);

            // Bounds the event to the rotary encoder
            Knob.Rotated += new NativeEventHandler(Knob_Rotated);

            red.Write(true);
            Thread.Sleep(250);
            red.Write(false);
            ylw.Write(true);
            Thread.Sleep(250);
            ylw.Write(false);
            grn.Write(true);
            Thread.Sleep(250);
            grn.Write(false);

            // Wait infinitely
            Thread.Sleep(Timeout.Infinite);
        }

        /// <summary>
        /// The value has been changed
        /// </summary>
        /// <param name="Unused">Not used</param>
        /// <param name="Value">The new value</param>
        /// <param name="Time">Time of the event</param>
        static void Knob_Rotated(uint Unused, uint Value, DateTime Time)
        {
            //Debug.Print(Value == 1 ? "Clockwise" : "Counter clockwise");

            if (Value == 1)
            {
                currentValue++;
            }
            else
            {
                currentValue--;
            }
            Debug.Print("Current Value = " + currentValue.ToString());
            switch (currentValue)
            {
                case 1:
                    red.Write(true);
                    break;
                case 2:
                    ylw.Write(true);
                    break;
                case 3:
                    grn.Write(true);
                    break;
                default:
                    currentValue = 0;
                    red.Write(false);
                    ylw.Write(false);
                    grn.Write(false);
                    break;
            }

        }

    }
}

RotaryEncoder_01 RotaryEncoder_02 RotaryEncoder_03 RotaryEncoder_04 RotaryEncoder_05 RotaryEncoder_06 RotaryEncoder_07

Scroll To Top