Taillieu.Info

More Than a Hobby..

Set up a Raspberry Pi 2

Set up a Raspberry Pi 2

Get your computer ready for the Windows Developer Program for IoT, set up your Raspberry Pi 2 and build your first Windows IoT Core app.

1. Set up your Raspberry Pi 2


2. Write your first app: Hello, Blinky!


In this tutorial, we'll create a simple LED blinking app and connect a LED to your Windows 10 IoT Core device.

This is a headed sample. To better understand what headed mode is and how to configure your device to be headed, follow the instructions here.

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

Load the project in Visual Studio

You can find the source code for this sample by downloading a zip of all of our samples here and navigating to the samples-develop\Blinky. The sample code is available in either C++ or C#, however the documentation here only details the C# variant. Make a copy of the folder on your disk and open the project from Visual Studio.

Connect the LED to your Windows IoT device

You'll need a few components:

A 220 Ω resistor

A breadboard and a couple of connector wires

An LED (any color you like)

Wiring your Raspberry Pi 2

  • Connect the shorter leg of the LED to GPIO 5 (pin 29 on the expansion header) on the RPi2.
  • Connect the longer leg of the LED to the resistor.
  • Connect the other end of the resistor to one of the 3.3V pins on the RPi2.
  • Note that the polarity of the LED is important. (This configuration is commonly known as Active Low)

Here is the pinout of the RPi2:

Here is an example of what your breadboard might look like with the circuit assembled:

*Image made with Fritzing*

Deploy Your App

  • With the application open in Visual Studio, set the architecture in the toolbar dropdown. If you're building for MinnowBoard Max, select x86. If you're building for Raspberry Pi 2 or the DragonBoard, select ARM.
 
  • Next, in the Visual Studio toolbar, click on the Local Machine dropdown and selectRemote Machine
  • At this point, Visual Studio will present the Remote Connections dialog. If you previously used Powershell to set a unique name for your device, you can enter it here (in this example, we're using my-device). Otherwise, use the IP address of your Windows IoT Core device. After entering the device name/IP select Universal (Unencrypted Protocol)Authentication Mode, then click Select.
  • You can verify or modify these values by navigating to the project properties (selectProperties in the Solution Explorer) and choosing the Debug tab on the left:

When everything is set up, you should be able to press F5 from Visual Studio. If there are any missing packages that you did not install during setup, Visual Studio may prompt you to acquire those now. The Blinky app will deploy and start on the Windows IoT device, and you should see the LED blink in sync with the simulation on the screen.

Congratulations! You controlled one of the GPIO pins on your Windows IoT device.

Let's look at the code

The code for this sample is pretty simple. We use a timer, and each time the 'Tick' event is called, we flip the state of the LED.

Timer Code

Here is how you set up the timer in C#:

            public MainPage()
            {
                // ...

                timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromMilliseconds(500);
                timer.Tick += Timer_Tick;
                InitGPIO();
                if (pin != null)
                {
                    timer.Start();
                }

                // ...
            }

            private void Timer_Tick(object sender, object e)
            {
                if (pinValue == GpioPinValue.High)
                {
                    pinValue = GpioPinValue.Low;
                    pin.Write(pinValue);
                    LED.Fill = redBrush;
                }
                else
                {
                    pinValue = GpioPinValue.High;
                    pin.Write(pinValue);
                    LED.Fill = grayBrush;
                }
            }
        

Initialize the GPIO Pin

To drive the GPIO pin, first we need to initialize it. Here is the C# code (notice how we leverage the new WinRT classes in the Windows.Devices.Gpio namespace):

Let's break this down a little

  • First, we use GpioController.GetDefault() to get the GPIO controller.
  • If the device does not have a GPIO controller, this function will return null.
  • Then we attempt to open the pin by calling GpioController.OpenPin() with the LED_PINvalue.
  • Once we have the pin, we set it to be off (High) by default using the GpioPin.Write()function.
  • We also set the pin to run in output mode using the GpioPin.SetDriveMode() function.
            using Windows.Devices.Gpio;

            private void InitGPIO()
            {
                var gpio = GpioController.GetDefault();

                // Show an error if there is no GPIO controller
                if (gpio == null)
                {
                    pin = null;
                    GpioStatus.Text = "There is no GPIO controller on this device.";
                    return;
                }

                pin = gpio.OpenPin(LED_PIN);
                pinValue = GpioPinValue.High;
                pin.Write(pinValue);
                pin.SetDriveMode(GpioPinDriveMode.Output);

                GpioStatus.Text = "GPIO pin initialized correctly.";

            }
        

Modify the state of the GPIO pin

Once we have access to the GpioOutputPin instance, it's trivial to change the state of the pin to turn the LED on or off.

To turn the LED on, simply write the value GpioPinValue.Low to the pin:

            pin.Write(GpioPinValue.Low);
        

and of course, write GpioPinValue.High to turn the LED off:

            pin.Write(GpioPinValue.High);
        
Remember that we connected the other end of the LED to the 3.3 Volts power supply, so we need to drive the pin to low to have current flow into the LED.