LED on Raspberry Pi 2 with UWP

By | 22. February 2016

Hi folks,

here is a rudiment small article about:

“How can i use GPIO pins?”

Ok for sure for experts its not so much interesting but for some starters it could be helpfull…

The full code is available here:

Part 1 View:

“The same procedure as every coding”

Firstly we create an UWP project in Visual Studio 2015

Neues UWP Projekt anlegen

To use the GPIO pins in Visual Studio its necessary to use the “Windows IoT Extensions for UWP”. This extension provide us the namespaces we need to let the Raspberry Pi understand what we want.

Of course we could change the state of the led direct from code… but in this example we want that an user can interact over a visual application with the Raspberry Pi.

Due to this we create a small view with a ToggleSwitch as input control.

ToggleSwitch Implementierung

As the attentive user can see its a very small page. You only have to implement a ToggleSwitch for switching the led state.

Part 2 Raspberry Pi 2:

A possible connection is displayed on the next picture.
Steckverbindung Raspberry Pi 2

As power supply we use the 3,3V pin. So we want that our led shines and we dont want that the led blows up. Because of this we need a resistor to throttle the power down. For this we use a 68 ohm resistor.

Some side infos:

Red LED needs about: 2.1V

Yellow LED needs about: 2.2V

Green LED needs about: 3,7V

Blue LED needs about: 3,1V

White LED needs about: 3,5V

Which GPIO pin you use for the cathode is in your hand. In this example we use GPIO pin 23.

Part 3 Control the GPIO-pins:

“Last but not least…”

…. we initialize the GPIO pin with the default GPIO Controller and set the state to “High” (LED off).

If we now use the ToggleSwitch we change the state from the led….

 public sealed partial class MainPage : Page
    {
        GpioPin pin;
        GpioController gpio;       
        private readonly int LEDPIN = 23;
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void LEDSwitch_Toggled(object sender, RoutedEventArgs e)
        {
            pin.Write(LEDSwitch.IsOn ? GpioPinValue.Low : GpioPinValue.High);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            // load default GpioController
            gpio = GpioController.GetDefault();

            //set the connection to the gpio pin
            pin = gpio.OpenPin(LEDPIN);

            //set Value for the Pin to High = LED off
            pin.Write(GpioPinValue.High);

            //config the pin for output
            pin.SetDriveMode(GpioPinDriveMode.Output);
        }
    }

Thats all... Yeah your right that was not so much demanding but this is the basis where you can build on.

Leave a Reply