Introduction
Third introductory article on the 'Internet of Things, written in collaboration with Piero Sbressa. After the installation of Windows 10 IoT Core found at this link, Now we will try to create an application in order to blink an LED, but stated that normally turn on or flashing an LED is not considered IoT, but in this case, serves to give an introduction and an idea of how to develop future applications or prototypes Raspberry Pi2 with Visual Studio 2015.
Electronic circuit
The material that we need can be found in the following image:
Figure 1: Requirement material
- 1 LED color you want (red, green or yellow.)
- 1 Resistance 220 Ohms.
- 2 cable connectors
- 1 Breadboard
The final connection you see in the following picture:
Figure 2: Final connection
Figure 3: Connection
Figure 4: Pin Circuit with LED
Figure 5: Circuit with LED
Below is the diagram of the electronic circuit. It states that to create a circuit or draw, are necessary for my personal experience, as well as dedicated software and hardware parts of the bases on the basics of electronics, otherwise my advice and devote themselves to the development of the code and leave to others the task earlier.
Figure 6: Electronic circuit
From the previous image, we note that we have two cables, one black and one red. The black cable representing zero volt connect me to pin GPIO5 corresponding to the pin 29 that are on the GPIO card Raspberry Pi2. The red wire on pin 3.3V PWR corresponding to pin 1. Below the connector pin GPIO thus facilitate us to connect.
Figure 7: GPIO thus facilitate us to connect
Importantly, the Led diode to work, must be properly connected, ie the katodo (because it recognizes and the shorter leg) must be connected to zero volts, the anode that is the other leg on the positive, which in this case It will be given by pin 3.3V PWR. Wrong wiring will not allow the diode LED work, you may damage the component. The resistance is important because generally a Led diode operates at a voltage of about 3.3V dc, in this case means that there is a voltage drop necessary and thus provide the correct voltage.
Setting up your PC in developer mode
Before starting to develop code, you must set the mode "developer" of our PC, otherwise it will not be possible to develop and test their applications. The procedure is very simple. We point the mouse on the icon of the notifications as shown.
Figure 8: Notifications
The next dialog box, select "All Settings".
Figure 9: select "All Settings"
We will be led to the next screen, select the command "Update and security."
Figure 10: Select the command "Update and security"
We go to the next screen select the "For developers," and soon after Developer mode.
Figure 11: Select the "For developers"
A dialog box will appear, where we have to confirm with "Yes" key to activate this mode.
Figure 12: Confirm with "Yes" key to activate this mode
Let's confirmation and we have enabled our PC development.
Figure 13: Enabled our PC development
Now we are ready to create our first project IoT.
Creating the project
Before starting with the development, we have yet to download and install the Windows library IoT Core Project Templates found at this link. In this library, you will find everything you need for the development of Raspberry Pi2 and beyond, we will see later in the article. Now open Visual Studio, and create a new project Universal Windows App, in C#.
Figure 14: Create a new project Universal Windows App
We need at this point to add a Reference to 'Windows SDK IoT exstension for the UWP, as shown below.
Figure 15: Windows SDK IoT exstension for the UWP
Now let's add in the Grid Xaml the following code:
- < StackPanel HorizontalAlignment = "Center"
- VerticalAlignment = "center" > < Ellipse x: Name = "LED"
- 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 = "26667" / > < Button Name = "AccendeSpegneLEDButton"
- Content = "Turn on LED"
- HorizontalAlignment = "Center"
- Click = "AccendeSpegneLEDButton_Click" / > < / StackPanel>
- While the code-behind we put the following code:
- / / Copyright(c) Microsoft.All rights reserved.
- using System;
- using Windows.Devices.Gpio;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Controls.Primitives;
- using Windows.UI.Xaml.Media;
- Blinky namespace {
- public sealed partial class MainPage: Page {
- private const int LED_PIN = 5;
- Private GpioPin pin;
- Private GpioPinValue pinValue;
- Private SolidColorBrush yellowBrush = new SolidColorBrush(Windows.UI.Colors.Yellow);
- Private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);
- public MainPage() {
- InitializeComponent();
- InitGPIO();
- }
- private void InitGPIO() {
- var gpio GpioController.GetDefault = ();
-
- pin = gpio.OpenPin(LED_PIN);
- pinValue = GpioPinValue.High;
- pin.Write(pinValue);
- pin.SetDriveMode(GpioPinDriveMode.Output);
- GpioStatus.Text = "GPIO pin properly initialized.";
- }
- private void AccendeSpegneLEDButton_Click(object sender, RoutedEventArgs e) {
- if (pinValue == GpioPinValue.High) = {
- pinValue GpioPinValue.Low;
- pin.Write(pinValue);
- LED.Fill = yellowBrush;
- AccendeSpegneLEDButton.Content = "Turn Off LED";
- } Else {
- pinValue = GpioPinValue.High;
- pin.Write(pinValue);
- LED.Fill = grayBrush;
- AccendeSpegneLEDButton.Content = "LED Light";
- }
- }
- }
- }
After the piece of code before test the application, you need to set a few things to see. The first being that we are developing on Raspberry Pi2, and to set the ARM compilation mode, you can perform from the menu as in this figure.
Figure 16: Set the ARM compilation mode
In reference to the previous image, you will notice that is on the run as "Remote computer". This is because we want to run the application on the card Raspberry Pi2. Select this mode, and the following screen should be placed the device name and the IP address of the Raspberry Pi2, clearing authentication, or you can change these settings by selecting the project, right-click, select "Properties" and immediately after "Debug", we will be led by the following screen.
Figure 17: Select "Properties" and immediately after "Debug"
Execution
We can now proceed with the execution of our first application. F5 key and if we did everything correctly our LED lights up when you click on the button that we have in the MainPage, as we can see from the following pictures.
Figure 18: LED Lights on
Figure 19: LED Lights off
Conclusion
In this article, we created our first application with Raspberry Pi2 and Visual Studio 2015, starting from the design, implementation of the electronic circuit, set the PC mode developer, to finish with the programming and coding for the management of the LED. In subsequent articles, we will try to create other test projects, the purpose of these mini-articles is to give an introduction on how to design and create their own project and why not be able then to make us business. Article written by
Piero Sbressa. And
Carmelo La Monica, Microsoft Contributor.