How To Control An LED Using Raspberry Pi 3, Windows IoT Core, And Visual Studio UWP

Introduction
 
This is an introduction to Raspberry Pi 3 Model B. We will explore what Raspberry Pi 3 has to offer in terms of its features and performance. I would like to introduce Raspberry Pi as the world’s most inexpensive and powerful Single-Board Computer. Ever since the launch of Raspberry Pi in 2012, we have seen several versions of it. This is the world’s cheapest microprocessor unit specially built for learners and makers. We can easily learn how software and hardware work together without worrying about damage/cost.
 
In this article, we will connect a Push Button and an LED to Raspberry Pi 3. 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 article cannot run on your desktop.
 
Requirements
Raspberry Pi GPIO Pin Diagram
 
 
 
Pi 3 Image - In-built Wireless Network (WIFI) And Bluetooth(BT) 
 
LED on and off using PushButton Circuit diagram.
 
 
Step 1
 
Open Visual Studio and go to File>>New>>Project Select Visual C#>>Windows Universal>>Blank App (Windows Universal).
 
 
 
Step 2
 
After the project creation, add the following reference packages for your projects. Open Solution Explorer, right-click to project name, and select Add >> References.
 
 
 
Now, select the following reference packages followed by the selection of your project; then click OK. 
  • Windows IoT Extensions for the UWP  
 
Step 3
 
Next, open Solution Explorer >> Project Name >> MainPage.xaml. Select the MainPage. The XAML code will appear. Just the following code. Create the layout of the Grid, StackPanel, and  Ellipse.
 
 
 
XAML Code 
  1. <Page  
  2.     x:Class="PushButton.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:PushButton"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.   
  10.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  11.         <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">  
  12.             <Ellipse x:Name="ledEllipse" Fill="LightGray" Stroke="White" Width="100" Height="100" Margin="10"/>  
  13.            <TextBlock x:Name="GpioStatus" Text="Waiting to initialize GPIO..." Margin="10,50,10,10" TextAlignment="Center" FontSize="26.667" />  
  14.         </StackPanel>  
  15.     </Grid>  
  16. </Page>   
Next, open Solution Explorer >> Project Name >> MainPage.xaml.cs. Select the MainPage.cs. The C sharp code will appear. Just the following code. 
 
C# Code
  1. using System;  
  2. using Windows.Devices.Gpio;  
  3. using Windows.UI.Core;  
  4. using Windows.UI.Xaml;  
  5. using Windows.UI.Xaml.Controls;  
  6. using Windows.UI.Xaml.Media;  
  7.   
  8. namespace PushButton  
  9. {  
  10.     public sealed partial class MainPage : Page  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.             InitGPIO();  
  16.         }  
  17.   
  18.         private void InitGPIO()  
  19.         {  
  20.             var gpio = GpioController.GetDefault();  
  21.   
  22.   
  23.             if (gpio == null)  
  24.             {  
  25.                 GpioStatus.Text = "There is no GPIO controller on this device.";  
  26.                 return;  
  27.             }  
  28.   
  29.             buttonPin = gpio.OpenPin(BUTTON_PIN);  
  30.             ledPin = gpio.OpenPin(LED_PIN);  
  31.   
  32.               
  33.             ledPin.Write(GpioPinValue.High);   
  34.             ledPin.SetDriveMode(GpioPinDriveMode.Output);  
  35.   
  36.               
  37.             if (buttonPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))  
  38.                 buttonPin.SetDriveMode(GpioPinDriveMode.InputPullUp);  
  39.             else  
  40.                 buttonPin.SetDriveMode(GpioPinDriveMode.Input);  
  41.   
  42.               
  43.             buttonPin.DebounceTimeout = TimeSpan.FromMilliseconds(50);  
  44.   
  45.               
  46.             buttonPin.ValueChanged += buttonPin_ValueChanged;  
  47.   
  48.             GpioStatus.Text = "GPIO pins initialized correctly.";  
  49.         }  
  50.   
  51.         private void buttonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)  
  52.         {  
  53.               
  54.             if (e.Edge == GpioPinEdge.FallingEdge)  
  55.             {  
  56.                 ledPinValue = (ledPinValue == GpioPinValue.Low) ?  
  57.                     GpioPinValue.High : GpioPinValue.Low;  
  58.                 ledPin.Write(ledPinValue);  
  59.             }  
  60.   
  61.               
  62.             var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {  
  63.                 if (e.Edge == GpioPinEdge.FallingEdge)  
  64.                 {  
  65.                     ledEllipse.Fill = (ledPinValue == GpioPinValue.Low) ?   
  66.                         redBrush : grayBrush;  
  67.                     GpioStatus.Text = "Button Pressed";  
  68.                 }  
  69.                 else  
  70.                 {  
  71.                     GpioStatus.Text = "Button Released";  
  72.                 }  
  73.             });  
  74.         }  
  75.   
  76.         private const int LED_PIN = 6;  
  77.         private const int BUTTON_PIN = 5;  
  78.         private GpioPin ledPin;  
  79.         private GpioPin buttonPin;  
  80.         private GpioPinValue ledPinValue = GpioPinValue.High;  
  81.         private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);  
  82.         private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);  
  83.     }  
  84. }  
Step 4
 
In this step, click on Debug >> Project Properties.
 
 
 
After that, open the Properties Box and connect the Raspberry Pi and Visual Studio to the same network and connect IP Address or Device name to the remote machine.
 
 
Step 5
 
In this step, click on the ARM and Remote Machine. 
 
 
 
Output
 
 
 
SUMMARY
 
In this article, you learned how to control an LED using Raspberry Pi 3 and Visual Studio on Universal Windows Platform.
 
If you have any questions/ feedback/ issues, please write in the comment box.

Up Next
    Ebook Download
    View all
    Learn
    View all