Push Button Sample

In this sample, we connect a push button to your Raspberry Pi 2/MinnowBoard Max and use it to control an LED. We use GPIO interrupts to detect when the button is pressed and toggle the LED in response.

This is a headed sample, so please ensure that your device is in headed mode by running this command: setbootoption.exe headed (changing the headed/headless state will require a reboot).

Also, be aware that the GPIO APIs are only available on Windows IoT Core, so this sample cannot run on your desktop.

Components

You will need the following components :

Connect the circuit to your device

Let’s start by wiring up the components on a breadboard. Visit the corresponding Raspberry Pi 2/MinnowBoard Max sections below depending on your device.

Push Button Sample

Raspberry Pi 2

Connecting the LED
  • Connect the cathode (the shorter leg) of the LED to Pin 31 (GPIO 6) of the Raspberry Pi 2
  • Connect the anode (the longer leg) of the LED to one lead of the 330 Ω resistor
  • Connect the other end of the 330 Ω resistor to Pin 1 (3.3V) on Raspberry Pi 2
Connecting the Push Button
  • Connect one pin of the push button to Pin 29 (GPIO 5) of the Raspberry Pi 2
  • Connect the other pin of the push button to the ground

Here is the pinout of the RPi2:

Connecting the LED
  • Connect the cathode (the shorter leg) of the LED to Pin 20 (GPIO 6) of the MinnowBoard Max
  • Connect the anode (the longer leg) of the LED to one lead of the 330 Ω resistor
  • Connect the other end of the 330 Ω resistor to Pin 4 (3.3V) on MinnowBoard Max
Connecting the Push Button
  • Connect one pin of the push button to Pin 18 (GPIO 5) of the MinnowBoard Max
  • Connect the other pin of the push button to the Pin 2 (Ground)

Here is the pinout of the MBM:

Building and running the sample

  1. Download a zip of all of our samples here.
  2. Open samples-develop\PushButton\CS\PushButton.csproj in Visual Studio.
  3. If you have Raspberry Pi 2, Select ARM for the target architecture. Otherwise, for MinnowBoard Max select x86
  4. Go to Build -> Build Solution
  5. Select Remote Machine from the debug target
  6. Hit F5 to deploy and debug. Enter the IP address of your device and select None for the authentication type.

Let’s look at the code

First, we open the GpioPin resources we’ll be using. The button is connected to GPIO5 in active LOW configuration, meaning the signal will be HIGH when the button is not pressed and the signal will go LOW when the button is pressed. We’ll be using the LED, connected to GPIO6, which is connected in active LOW configuration, meaning driving the pin HIGH will turn off the LED and driving the pin LOW will turn on the LED.

buttonPin = gpio.OpenPin(BUTTON_PIN);
ledPin = gpio.OpenPin(LED_PIN);

We initialize the LED in the OFF state by first latching a HIGH value onto the pin. When we change the drive mode to Output, it will immediately drive the latched output value onto the pin. The latched output value is undefined when we initially open a pin, so we should always set the pin to a known state before changing it to an output. Remember that we connected the other end of the LED to 3.3V, so we need to drive the pin to low to have current flow into the LED.

// Initialize LED to the OFF state by first writing a HIGH value
// We write HIGH because the LED is wired in a active LOW configuration
ledPin.Write(GpioPinValue.High); 
ledPin.SetDriveMode(GpioPinDriveMode.Output);

Push Button Sample schematicNext, we set up the button pin. For the Raspberry Pi 2, we take advantage of the fact that it has built-in pull up resistors that we can activate. We use the built-in pull up resistor so that we don’t need to supply a resistor externally. Configurable pull up resistors are not available on the MinnowBoard Max, so we insert a check to make sure this drive mode is supported.

// Check if input pull-up resistors are supported
if (buttonPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
	buttonPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
else
	buttonPin.SetDriveMode(GpioPinDriveMode.Input);

Next we connect the GPIO interrupt listener. This is an event that will get called each time the pin changes state. We also set the DebounceTimeout property to 50ms to filter out spurious events caused by electrical noise. Buttons are mechanical devices and can make and break contact many times on a single button press. We don’t want to be overwhelmed with events so we filter these out.

 

For more detail: Push Button Sample


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top