Windows 10 IoT Blinky background app in VB.NET

Blinky is simple app to blink an LED that is connected to your Windows IoT Core device. The source code and documentation for this app is available in C++ or C# from the link here. But in this tutorial here we'll create this same app in VB.NET. This app does not have a UI and will run as a background service in your Winows IoT device, which can be Raspberry Pi 2 or 3, MinnowBoard Max or DragonBoard.

What you need

  1. Raspberry Pi 2 or 3 with Windows 10 IoT Core installed. See Install Windows 10 IoT on Raspberry Pi
  2. LED
  3. Resistor 220Ω (or higher upto 1KΩ)
  4. Breadboard
  5. Connector wires
  6. A development PC that has:
    • Windows 10 operating system ( Developer mode enabled )
    • Visual Studio Community 2015 Update 1
    • Windows IoT Core Project templates

Connecting the LED to Raspberry Pi

  1. Connect the negative end of the LED (shorter leg, cathode) to GPIO 5, which is pin number 29 in Raspberry Pi 2 and Raspberry Pi 3.
  2. Connect the positive end of the LED (the longer leg, anode) one end of the resistor.
  3. Connect the other end of the resistor to one of the 3.3V pins, which is pins 1 and 17 in Raspberry Pi 2 and Raspberry Pi 3.
Rasberry Pi LED Connection

Build the app in Visual Studio 2015

  1. Open Visual Studio 2015.
  2. Select File → New → Project from the main menu.
  3. In the New Project dialog select Visual Basic → Windows → Windows IoT Core in the left pane under Installed → Templates
  4. Select Background Application (IoT) from the middle pane.
  5. Enter a name and location for your app.
  6. In the Solution drop down box select create new solution and give a name for the solution.
  7. Click OK.
  8. Open StartupTask.vb from Solution Explorer and copy and paste the below source code to StartupTask class file. This source code is explained in detail in the next section.

StartupTask.vb source code

Imports Windows.ApplicationModel.Background
Imports Windows.Devices.Gpio
Imports Windows.System.Threading

Public NotInheritable Class StartupTask
    Implements IBackgroundTask

    Const LED_PIN As Short = 5 'GPIO Pin(5) = Physical Pin(29) = WiringPi Pin (21)
    Const INTERVAL As Short = 2   'Interval in seconds
    Private gpio
    Private pin As GpioPin
    Private pinValue As GpioPinValue
    Private blinktimer As ThreadPoolTimer
    Private cancelled As Boolean = False
    Private deferral As BackgroundTaskDeferral = Nothing


    Public Sub Run(taskInstance As IBackgroundTaskInstance) Implements IBackgroundTask.Run

        'Handler for cancelled background task
        AddHandler taskInstance.Canceled, New BackgroundTaskCanceledEventHandler(AddressOf onCanceled)

        'Background task continues to run even after IBackgroundTask.Run returns
        deferral = taskInstance.GetDeferral()

        'Initialize GPIO
        initGPIO()

        'Create a periodic timer
        blinktimer = ThreadPoolTimer.CreatePeriodicTimer(New TimerElapsedHandler(AddressOf blinktimer_tick), TimeSpan.FromSeconds(INTERVAL))

    End Sub

    Private Sub onCanceled(sender As IBackgroundTaskInstance, reason As BackgroundTaskCancellationReason)
        cancelled = True
    End Sub

    Sub blinktimer_tick(state As Object)

        If Not cancelled Then
           'Toggele Pin Value
            If (pinValue = GpioPinValue.High) Then
                pinValue = GpioPinValue.Low
            Else
                pinValue = GpioPinValue.High
            End If

            pin.Write(pinValue)
        Else
           'if the task is cancelled
           'Stop the timer And finish execution
            blinktimer.Cancel()
            deferral.Complete()
        End If
    End Sub

    Private Sub initGPIO()
        'Get the default GPIO controller for the system
        gpio = GpioController.GetDefault()

        If Not gpio Is Nothing Then
            'Open a connection to the GPIO Pin
            pin = gpio.openpin(LED_PIN)
            pinValue = GpioPinValue.Low

            'Write Pin value low to turn on LED
            pin.Write(pinValue)

            'Set the pin for output
            pin.SetDriveMode(GpioPinDriveMode.Output)
        End If

    End Sub

End Class


Source code walkthrough

Imports Windows.Devices.gpio

The namespace Windows.Devices.gpio contains the necessary classes that allow you to read the current status and switch on/off the GPIO pins from the app.

AddHandler taskInstance.Canceled, New BackgroundTaskCanceledEventHandler(AddressOf onCanceled)

Enable the app to recognize cancellation request that is made against your app.

deferral = taskInstance.GetDeferral()

Toggling the LED on/off is performed inside an timer which runs asynchronously. The above line tells the system not to stop timer task when the Run method is finished.

initGPIO()

This is the function that initializes the GPIO. It will get the GPIO controller for the system and opens GPIO pin 5.

blinktimer = ThreadPoolTimer.CreatePeriodicTimer(New TimerElapsedHandler(AddressOf blinktimer_tick), TimeSpan.FromSeconds(INTERVAL))

Create a periodic timer that calls the blinktimer_tick function every 2 seconds. You can adjust the blink speed by changing the constant value INTERVAL

Sub blinktimer_tick(state As Object)

Inside this function first we check if a cancel request was recieved in which case the timer is stopped and the app execution is completed. Otherwise toggle the pin value to high or low, there by changing the LED off and on respectively.

Build and Deploy your App

  1. In the Visual Studio 2015 standard toolbar select the architecture as ARM.
    Visual Studio-Architecture
  2. Select Remote Machine from the dropdown list as Target device.
    Visual Studio-Target Device
  3. From the main menu select Build → Build Solution
  4. Confirm the build succeeded with out errors.
  5. From main menu select Build → Deploy Solution.
  6. Select your device in the Remote Connections dialog. You could enter the device name or IP address or select from the Auto detected list of devices.
Visual Studio Remote Connections

Run the app on Windows IoT

  1. Open Device Portal. On Windows IoT Core Dashboard, Click My Devices from the left pane. Then right click your Windows IoT Device and select Open in Device Portal. Alternatively, if you know the IP address of your device, open a web browser and connect to that IP address on port 8080. (http://your-ip-address:8080)
  2. Select Apps fro the left side menu.
  3. Click on the start icon next to your app name.
  4. You will now see the LED blink every 2 seconds.