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 the 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="https://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  4.     xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"    
  5.     xmlns:local="using:PushButton"    
  6.     xmlns:d="https://schemas.microsoft.com/expression/blend/2008"    
  7.     xmlns:mc="https://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. namespace PushButton   
  8. {  
  9.  public sealed partial class MainPage: Page   
  10.  {  
  11.   public MainPage()   
  12.   {  
  13.    InitializeComponent();  
  14.    InitGPIO();  
  15.   }  
  16.   private void InitGPIO()   
  17.   {  
  18.    var gpio = GpioController.GetDefault();  
  19.    if (gpio == null)   
  20.    {  
  21.     GpioStatus.Text = "There is no GPIO controller on this device.";  
  22.     return;  
  23.    }  
  24.    buttonPin = gpio.OpenPin(BUTTON_PIN);  
  25.    ledPin = gpio.OpenPin(LED_PIN);  
  26.    ledPin.Write(GpioPinValue.High);  
  27.    ledPin.SetDriveMode(GpioPinDriveMode.Output);  
  28.      
  29.    if (buttonPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))  
  30.     buttonPin.SetDriveMode(GpioPinDriveMode.InputPullUp);  
  31.    else  
  32.     buttonPin.SetDriveMode(GpioPinDriveMode.Input);  
  33.       
  34.    buttonPin.DebounceTimeout = TimeSpan.FromMilliseconds(50);  
  35.    buttonPin.ValueChanged += buttonPin_ValueChanged;  
  36.    GpioStatus.Text = "GPIO pins initialized correctly.";  
  37.   }  
  38.   private void buttonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)   
  39.   {  
  40.    if (e.Edge == GpioPinEdge.FallingEdge)   
  41.    {  
  42.     ledPinValue = (ledPinValue == GpioPinValue.Low) ? GpioPinValue.High : GpioPinValue.Low;  
  43.     ledPin.Write(ledPinValue);  
  44.    }  
  45.    var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>   
  46.    {  
  47.     if (e.Edge == GpioPinEdge.FallingEdge)   
  48.     {  
  49.      ledEllipse.Fill = (ledPinValue == GpioPinValue.Low) ? redBrush : grayBrush;  
  50.      GpioStatus.Text = "Button Pressed";  
  51.     }   
  52.     else   
  53.     {  
  54.      GpioStatus.Text = "Button Released";  
  55.     }  
  56.    });  
  57.   }  
  58.   private const int LED_PIN = 6;  
  59.   private const int BUTTON_PIN = 5;  
  60.   private GpioPin ledPin;  
  61.   private GpioPin buttonPin;  
  62.   private GpioPinValue ledPinValue = GpioPinValue.High;  
  63.   private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);  
  64.   private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);  
  65.  }  

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 the 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 the Universal Windows Platform.
 
If you have any questions/ feedback/ issues, please write in the comment box.