Control LED in C# .NET using Netduino

Did you know that you can use Netduino to turn on an LED when you press a button? Well, It is true, you can do this! Leaving the joke aside, let me show how you can achieve this.

The button in the middle of the board (Pins.ONBOARD_BTN) is just another port that you can read using the .Net MF libraries. This simple example loops and reads the status of the button. If it’s down it returns true, Up returns false. Running the program and pressing the button just toggles the LED on and off.

Source Code

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace LED_Control_Using_Switch
{
    public class Program
    {
        public static void Main()
        {
            OutputPort onBoardLED = new OutputPort(Pins.ONBOARD_LED, false);
            InputPort inputPort = new InputPort(Pins.ONBOARD_BTN, false, Port.ResistorMode.Disabled);

            while(true)
            {
                if(inputPort.Read())
                {
                    onBoardLED.Write(true);
                }
                else
                {
                    onBoardLED.Write(false);
                }
            }
        }
    }
}

It would interesting to knock up a little app that recorded a pattern of button presses. After recording, it loops, waiting for you to press the button in the same sequence. If you do, it will light up the LED. You could imagine this connected to some sensor measuring a secret knock-knock password or a keyboard with which you play some musical notes. If you get it right, it unlocks something like a box for instance. It might also be a nice way for sending morse code.

Related Article :- Getting Started with Netduino. Learn IoT using C# .NET

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *