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
- <Page
- x:Class="PushButton.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:PushButton"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
-
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
- <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
- <Ellipse x:Name="ledEllipse" Fill="LightGray" Stroke="White" Width="100" Height="100" Margin="10"/>
- <TextBlock x:Name="GpioStatus" Text="Waiting to initialize GPIO..." Margin="10,50,10,10" TextAlignment="Center" FontSize="26.667" />
- </StackPanel>
- </Grid>
- </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
- using System;
- using Windows.Devices.Gpio;
- using Windows.UI.Core;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Media;
-
- namespace PushButton
- {
- public sealed partial class MainPage : Page
- {
- public MainPage()
- {
- InitializeComponent();
- InitGPIO();
- }
-
- private void InitGPIO()
- {
- var gpio = GpioController.GetDefault();
-
-
- if (gpio == null)
- {
- GpioStatus.Text = "There is no GPIO controller on this device.";
- return;
- }
-
- buttonPin = gpio.OpenPin(BUTTON_PIN);
- ledPin = gpio.OpenPin(LED_PIN);
-
-
- ledPin.Write(GpioPinValue.High);
- ledPin.SetDriveMode(GpioPinDriveMode.Output);
-
-
- if (buttonPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
- buttonPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
- else
- buttonPin.SetDriveMode(GpioPinDriveMode.Input);
-
-
- buttonPin.DebounceTimeout = TimeSpan.FromMilliseconds(50);
-
-
- buttonPin.ValueChanged += buttonPin_ValueChanged;
-
- GpioStatus.Text = "GPIO pins initialized correctly.";
- }
-
- private void buttonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
- {
-
- if (e.Edge == GpioPinEdge.FallingEdge)
- {
- ledPinValue = (ledPinValue == GpioPinValue.Low) ?
- GpioPinValue.High : GpioPinValue.Low;
- ledPin.Write(ledPinValue);
- }
-
-
- var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
- if (e.Edge == GpioPinEdge.FallingEdge)
- {
- ledEllipse.Fill = (ledPinValue == GpioPinValue.Low) ?
- redBrush : grayBrush;
- GpioStatus.Text = "Button Pressed";
- }
- else
- {
- GpioStatus.Text = "Button Released";
- }
- });
- }
-
- private const int LED_PIN = 6;
- private const int BUTTON_PIN = 5;
- private GpioPin ledPin;
- private GpioPin buttonPin;
- private GpioPinValue ledPinValue = GpioPinValue.High;
- private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);
- private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);
- }
- }
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.