In this sample, we will connect a Push Button and LED to Raspberry Pi 2. We will be reading the status of the Push button and control an LED using GPIO.
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.
You will need the following components :
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 Tactile 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
Building and running the sample
- Download the MS-IOT samples zip from here.
samples-develop\PushButton\CS\PushButton.csprojin Visual Studio.
ARMfor the target architecture.
- Go to
Build -> Build Solution
Remote Machinefrom the debug target
- Hit F5 to deploy and debug. Enter the IP address of your device and select
Nonefor 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.
For more detail: Push Button Sample