In continuing with the NETMF for Electronics Noobs series of tutorials, today I’m going to demonstrate how to connect a button/switch to a Panda-II NETMF microprocessor and how to respond when the button is pushed. Since I need to do something when the button is pushed, I thought this would be an opportune time to also show how to connect an LED to the board and light it up also.
Nearly every NETMF basic tutorial out there starts with a very simple program that uses the on-board button to light up the on-board LED. That’s just great and is absolutely fantastic the first time you write a NETMF program. However, as soon as you start working on a real project you soon discover that the on-board button & LED have very limited uses since they can’t be exposed outside of your project enclosure. Then the question comes up – how do I add more buttons? How do I light up an LED on my project box? How do I use more than one button or LED?
I’m going to demonstrate how to do this in the most basic situation – the one that will work for 95% of all hobbyist’s needs.
Like most things, there are many ways you could wire this up. This is just one. What I did was to first plug my button into the breadboard and wire one side of the button to the ground line (very top row on the breadboard). Then, I connected a 10K ohm resistor (brown, black, orange) to the power (5V) line (2nd row on the breadboard) and used it to connect power to the other side of the button. The 10K resistor prevents us from creating a “short” which could fry the electronics by allowing too much current to be pulled through the electronics. Next, we put a 100 ohm resistor (brown, black, brown) on the power side of the button then connect the other side of the resistor to the analog 0 pin on the Panda-II. This resistor prevents too much current from getting into the board and damaging it.
Now that we have the button wired up, we’ll wire up the LED. Since only a very small amount of current flows through pins of the Panda-II board we can connect the anode/positive side (longer wire) of the LED to the D5/PWM5 pin and wire the cathode/negative (shorter) side of the LED to our ground line. That’s it for the wiring.
I do want to note here that if we were wiring the LED to an external power source that we would need to add a current limiting resistor to one side. To determine the correct size resistor, you can use an LED calculator.
Now that we have the hardware all wired up, it’s time to write some software that responds to our button being pressed by lighting the LED.
In Visual Studio, we create a new console project and add the following references:
In our Program.cs file that is automatically created, replace what is there by default with the following.
public class Program
// Tell the CPU we're going to use digital pin #5 to send a signal to the LED.
static readonly OutputPort _led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di5, false);
public static void Main()
// Tell the CPU we're going to receive an analog signal from analog pin #0.
var blueButton = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.An0, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
// The second parameter in the line above ("true") tells the CPU that we want to use the "glitch" filter.
// If you disable this line then run the program, you will notice in the debug windows that if you are not very fast
// at pushing the button that the event will actually get fired multiple times. We only want the event to fire once. GHI gives
// us this property that we can set to put a boundary around our first event and prevent redundant events from being fired.
// Adjust the number of tick
Cpu.GlitchFilterTime = new TimeSpan(TimeSpan.TicksPerSecond/2); // 1 Tick = 100 nanoseconds
// When the button is pushed it will signal an interrupt in the CPU which will in turn cause an OnInterrupt event to be thrown.
// Our BlueButton_OnInterrup function will be called when the interrupt/event is fired.
blueButton.OnInterrupt += BlueButton_OnInterrupt;
private static void BlueButton_OnInterrupt(uint port, uint state, DateTime time)
_led.Write(true); // Turn on the LED
Thread.Sleep(1000); // wait 1 second...
_led.Write(false); // Turn off the LED
Debug.Print("Blue Button Pushed! " + DateTime.Now.ToString());
I’ve chosen to document the code as comments in-line, but if anything isn’t clear please leave a comment and I’ll clear it up.
Make sure and set your project properties to deploy to the USBizi device then run it. If you’ve done everything correct then when you push the button the LED should come on for half a second then turn back off.
Now that you have one button and one LED working, get creative! You can reproduce this many times and as many different ways as your imagination will allow. To use more buttons or more LEDs, just use any of the other pins and be sure to specify in your code when you define your buttons or LEDs which pin they are connected to and you’ll be fine. Put these new skilz to work and be sure to post your results in the comments!