SQL Server and Windows 10 IoT Part 2: Configuring your board and wiring your circuit

That new hardware smell

Welcome back to my series on Windows 10 IoT and SQL Server! Back in part 1 I talked about the goals of the project, what our goal was, and the parts and pieces you need to get started. Some of you might have actually gone ahead and ordered your hardware already, or maybe you’re still waiting. Either way, I’m going to go ahead and talk about the board and how you can install Windows 10 IoT, wire your circuit, and run a test program to make everything is correctly set up.

Note: This is a long blog post and there are lots of steps. I wanted to break this up into two parts, but I don’t think you’d want to wire this up and then wait for the next post to actually test your work. I apologize for condensing so much in here, and if you have questions, some of them (especially how the test code works) will be answered in the next post and I’m always available to contact or tweet to if you have any issues.

First though, let’s take a look at your board. The Raspberry Pi 3 is a nifty little piece of technology and if you’ve never worked with one before, you might be surprised just how small it is considering all the different things it can do.

13825-04

Above you can see the “top” of the board. I’m using quotes because really, any side can be the “top” but this side of the board has 98% of the features you’ll be caring about for this tutorial. If you’re a regular user of computers you should probably notice a lot of the ports along the sides right away. To the right, you have 4 USB ports, and next to those you’ll have a network connection. Contuining along the side, you have an audio connector in standard 3.5mm headphone gauge, and next to that is an HDMI output. Not much farther along you have the 5V power connector in micro USB form factor. Of those, we’re only really going to concern ourselves with the power connector and network connection. That’s because we’re going to make a headless device. A headless device means we’re not going to connect a display.

A very important part of the board, though, can be seen across the top of the picture above. The series of 40 pins, arranged in two rows of 20, is called the general purpose input-output connector, or GPIO. These pins are accessible via software libraries that can control their state, and each pin is referenced by a number. Generally, each pin has one of three functions: power, in the form of 3.3V or 5V, ground, or interface pins. When you’re working with the board and circuits, you’ll be using at least two of, if not all 3 of, the pin types. We’ll be going into detail on which pins to connect to later.

Finally, the only other relevant part of the board is on the other side not pictured: there’s a small microSD slot. That’s where our software image and storage goes.

microsdpislot

And speaking of that, it’s time to install our operating system now.

Installing Windows 10 IoT

Installing Windows 10 IoT is a four-step process, and it’s outlined really well on the Windows 10 IoT site. In fact, you should probably head over there and follow those instructions. Some tips as you progress, though:

  • Make sure you pick Raspberry Pi 3 as your hardware in the first step,
  • Don’t bother installing with NOOBS. Just choose “Install onto my blank microSD card” and,
  • Make sure you’re not selecting the preview build

During the process, you’ll be asked to download the Windows 10 IoT dashboard. This is your first hard stop if you didn’t meet all the requirements back in part 1 of this blog series. If you can’t get the application to run, make sure you check out the requirements. Once you finish downloading and installing the IoT dashboard, go ahead and run it. When you open it, click on “Set up a new device” and you should see something like this:

iotdashboard

You’ll want to note that the device type should be set to Raspberry Pi 2 & 3, and your OS build should be IoT Core (not the preview build!).  If you haven’t inserted your microSD card into your PC yet, it’ll prompt you to. Then select the drive letter that matches it in the drop down. Finally, name your device (I went with backupbutton, but you can choose whatever you want) and type in the Administrator password. You’ll need this password for later steps, so write it down! Then, accept the license terms and click “Download and Install.”

The process will go ahead and flash your microSD card and should take a couple of minutes. When it finishes, take the card and plug it into the slot on your Pi. Then, connect your network cable and then the power to the device and let it boot up. If everything works correctly and it gets an IP address via DHCP, if you click on “My devices” you should see your device name in the list. Click on the pencil (or is that a pen?) icon in the settings column next to it. You’ll get a settings screen where you can change the name, but at the bottom of the screen is a link that reads something like “Open Windows Device Portal in a browser.” You should get a new web browser window to open, and you’ll be prompted for a username (Administrator) and the password you set earlier. Once you type it in, you should see something like this:

iotdeviceportal

 

Congratulations! You’ve successfully installed Windows 10 IoT on your device. Feel free to click around on the navigation panel to the left to look at some of the things you can do with it. You may want to change the time zone, or look at what processes are running. Explore this space. Now with that out of the way, it’s time to actually DO something with it.

Wiring the device

Now that we have an up and running system board, let’s start by wiring it up. Shut down your board by using the web portal and clicking “Power” and then “Shut down.” This will stop the device processes but it won’t power it off. Go ahead and disconnect the power. Before we get started, make sure you have everything you need for this next step (see the previous post for your shopping list).

I should also pause for a second and talk about wiring hobby boards like this. Good news first: you won’t electrocute yourself on it. I mean, if you do something really dumb like try to wire it underwater or eat it or something then maybe you could but you shouldn’t ever receive a shock while working with a board like this, even plugged in. The bad news is that even though you won’t damage yourself, you could very well damage the board if you just randomly plug things in. Here’s a hard and fast rule: if you’re not an electronics expert or an electrical engineer, leave it to experts to tell you where and how to wire. I’m not calling myself an expert here, but I have sort of a basic understanding of how to wire these things up. The point I’m attempting at making is: if you want to really learn and understand circuit design, there are lots of great resources of where to get started. And it’s quite a rabbit hole to go down, but it’s well worth your time if you want to learn more.

Below is a basic frtizing diagram of how the circuit is wired. One thing to note: the pins on your Pi board are male, but your breadboard is female. Unless you buy F->M jumper wires, you won’t be able to make these connections. In the shopping list I provided, I mentioned buying female jumper wires and single row header pins. You’ll need a header strip (or two, depending how you break them… they’re designed to be broken off) to make all these connections. Note the orientation of the board relative to the breadboard; this will make sure you connect the right pins.

iot_backupbutton_frtiz

For step by step instructions, try these:

  1. If you have a 4 pin button, “jump” the center gap on the breadboard. Press the button firmly in place so that all four are in the slots on the bread board. If you have a two-pin button, keep them on the same side of the board.
  2. Take one of your single header strips and break it so that you have 3 pins showing. Plug those into the holes above one side of the button.
  3. With the remaining headers, break off a pretty generous strip. I used one a length of 12 pins.
  4. When you plug in your LEDs, you’ll notice one pin is longer than the other. You want to make sure the longer pin is closer to the resistor, and the shorter end is near the wire that goes to the GPIO pins. This is called an active low configuration.

What does this actually do?

If you’re not too familiar with circuits,  then you might just wonder what the heck you wired up. Let’s walk through it. What we just did was complete a series of circuits on our breadboard. A breadboard is a great prototyping tool because it lets connect wires and devices (like LEDs and switches) without having to solder anything. There’s 2 main sections to any breadboard: the terminal strips in the “middle” of the board that run in “columns” and the bus strips which run perpendicular on the outside of the board. Bus strips are traditionally used to control power and grounds and the terminal strips are where your components will go.

Let’s look a the blue LED as an example. We first connect a wire (the blue wire) to a GPIO pin on the Pi board. We plug that into a header strip on the board. That means every pin in the same column as that wire is now “active” with that connection (look at the green highlighted holes above and below it). We then put one end of the blue LED into a hole one in the same column and bridge the center gap in the breadboard and plug the longer end into a hole on the other side. Now the rest of the column is now connected (again, note the green highlights). Then we connect one end of the 470 Ohm resistor in a hole in the same column, and then connect the other end to the positive bus at the bottom. The positive bus is connected to the 3.3V pin on the GPIO board via the teal wire. Look at the bottom row of the breadboard in our diagram above. You’ll notice that the entire row is highlighted green. That means all the other LEDs terminate to the bus, so each one gets powered separately from the same row, even though they are wired to different GPIO pins.

The exception here is the button. We’re not connecting this to any voltage pins. Instead, we are connecting one end to GPIO pin 5 (white wire), and the other to a ground pin (black wire). That’s because we’re going to make this a little mini-circuit that is normally a “broken” but gets completed when we press the button.

Got all that? Even if you don’t understand it just yet, try your best to wire your device the same way I did. If you have any questions, just reach to me via twitter or in an email or in the comments and I’ll do my best to help you. Once you complete your wiring, go ahead and reconnect your power supply to boot your device. Try pressing the button.

Nothing happens! Did we mess up? Well, the truth is, we don’t know.. yet. That’s because all the GPIO pins are “off” to start. To interface with them, we need to first upload a program to our Pi board to enable the pins and test our functionality. To do that, we first need to set up Visual Studio.

Setting up Visual Studio

To begin writing code for your device, you’ll first need to install Visual Studio. You can use any edition you want, as long as it’s a flavor of 2015. Enterprise, Ultimate, or Community all work. For our example, I am going to use the Community edition. If you already have Visual Studio 2015 installed, feel free to skip to the next step if you know you have all the required components installed.

You can download the community version of Visual Studio here: http://go.microsoft.com/fwlink/?LinkID=534599 Once you get the installer, leave the default options it gives you selected, but also make sure you select “Microsoft Web Developer Tools” under “Windows and Web Development” in the feature selection. If you do a custom install, make sure you select “Universal Windows App Development Tools” and all the sub-selections.

Update Visual Studio

Next we need to make sure Visual Studio has all it’s updates and the required IoT project templates are installed. After you launch Visual Studio, close any dialog boxes that pop up, and then open the Tools -> Extensions and Updates. Then click “Updates” on the left-hand side. If you don’t see anything named “Update 3” then you’re good to go. Otherwise, install update 3 and let Visual Studio restart.

Once that’s done, you need the templates for IoT and UWP. From the Extensions and Updates dialog again, click on “Online” on the left hand side. Then, in the search box type in “Windows IoT Core Project Templates” and you should get some results back. You’ll want the “Windows IoT Core Project Templates” and you can get them by clicking “Install.” Once those installed, restart VS2015 again.

Enable developer mode

Finally, you need to allow your PC to interact with your IoT device. From your start menu, click on “Settings.” Click on “Update and Security” and then click on “For Developers” on the left-hand side. Change the setting from “Windows Store Apps” to “Developer mode.” Note: when you’re not working with your device, you should probably play it safe and change this back to the default setting.

Create a Universal Windows App solution

If you made it this far, we just have two more steps to go, and that’s to create a new solution and then (finally!) deploy our test code. To create an app for your device, you’ll do the following:

  1. In Visual Studio, click on File -> New -> Project…
  2. In the New Project, expand the installed templates drop down, then Windows, and click on “Universal.” Choose “Blank App (Universal Windows)”
  3. In the project name, type “IoTTestApplication”
  4. Click OK
  5. You should get a dialog box pop up asking what the target and minimum versions for your app are. Just click OK.
  6. Once the project creates, you should be given a new window with the file “App.xaml.cs” open. The first thing you should do is at the top of your screen, next to the drop down that says “Debug” change the value of the other drop down from “x86” (or x64) to “ARM.”
  7. Close the “App.xaml.cs.” file by clicking the “x” next to the name.
  8. On the right-hand side of your screen, in the solution explorer, expand “references.”
  9. We need to add a new reference to the IoT libraries. Right click on “references” label, and click “Add reference…”
  10. In Reference Manager window, on the left-hand side click “Universal Windows” to expand it, and then click “Extensions.”
  11. You should now see a list of references in the main part of the dialog box. Find the entry labeled “Windows IoT Extensions for the UWP” and click it. You should see a blank checkbox show up next to it. Make sure you click the check box, and then click OK.
  12. You should now see a reference for Windows IoT Extensions for the UWP in your references folder in your solution.

Our environment setup is now complete!

Test Code

Now that we have our solution, we can create our test app. In your solution explorer, find the file that says “MainPage.xaml” and click the small arrow next to it to expand it. There should be a file named “MainPage.xaml.cs” nested below it. Double click that file to open it.

We’ll go into detail about the code later but for now, I’m going to give you some test code to run. In this file, copy and paste the following listing into your code window:

using System;
using Windows.Devices.Gpio;
using Windows.UI.Xaml.Controls;

namespace IoTTestApplication
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();
            InitGPIO();
        }

        private void InitGPIO()
        {
            var gpio = GpioController.GetDefault();

            buttonPin = gpio.OpenPin(Button_PIN);
            BlueLEDPin = gpio.OpenPin(BlueLED_PIN);
            YellowLEDPin = gpio.OpenPin(YellowLED_PIN);
            RedLEDPin = gpio.OpenPin(RedLED_PIN);

            BlueLEDPin.Write(GpioPinValue.Low);
            BlueLEDPin.SetDriveMode(GpioPinDriveMode.Output);
            YellowLEDPin.Write(GpioPinValue.High);
            YellowLEDPin.SetDriveMode(GpioPinDriveMode.Output);
            RedLEDPin.Write(GpioPinValue.High);
            RedLEDPin.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;
        }

        private void buttonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
        {
            if (e.Edge == GpioPinEdge.FallingEdge)
            {
                if (BlueLEDPin.Read() == GpioPinValue.Low)
                {
                    BlueLEDPin.Write(GpioPinValue.High);
                    YellowLEDPin.Write(GpioPinValue.Low);
                    RedLEDPin.Write(GpioPinValue.High);
                } else if (YellowLEDPin.Read() == GpioPinValue.Low)
                {
                    BlueLEDPin.Write(GpioPinValue.High);
                    YellowLEDPin.Write(GpioPinValue.High);
                    RedLEDPin.Write(GpioPinValue.Low);
                } else if (RedLEDPin.Read() == GpioPinValue.Low)
                {
                    BlueLEDPin.Write(GpioPinValue.Low);
                    YellowLEDPin.Write(GpioPinValue.High);
                    RedLEDPin.Write(GpioPinValue.High);
                }
            }
        }

        private const int BlueLED_PIN = 6;
        private const int YellowLED_PIN = 22;
        private const int RedLED_PIN = 27;
        private const int Button_PIN = 5;
        private GpioPin BlueLEDPin;
        private GpioPin YellowLEDPin;
        private GpioPin RedLEDPin;
        private GpioPin buttonPin;
    }
}

To make sure everything works, click the “build” menu, then click “build solution.” It might take a few seconds, but if you don’t see any any build errors, you’re good to go. If you do see build errors… well, welcome to software development! Start drinking heavily. I kid. If you see any build errors, most of the time the exceptions are pretty self-explanitory. You might have missed a step above, or you might have misspelled something (or more likely, I have a type in my blog post). Understanding Visual Studio is a skill unto itself, and if you have access to developers who understand it, you could (and probably should) talk to them if you have any issues. Failing that, Google is also your friend.

Deploy your app

Now’s the moment of truth: we’re going to build our solution and deploy our application to our device. First, from the file menu in Visual Studio, make sure you save everything: your code you just copy and pasted, your solution… everything. In your visual studio window, look for the two drop downs we used earlier. Next to it should be a green “play” button that says “local machine.” Hit that drop-down and click “Remote machine.” You should get a dialog box that looks like this:

remoteconnectiondevice

You should see your backupbutton device listed, along with it’s IP address. Click it, then click the “Select” button. The green play button should now say “Remote Machine.” Click it to begin your deployment. It may take a few minutes, but if it’s successful, two things should happen: one, the blue LED should light up on your breadboard and two, in your IoT control panel you should see the application running:

remoteapplicationsiot

Go ahead and try pressing your button: it should cycle through your LED lights. If they don’t light up, check your connections. Make sure your resistors and LEDs are inserted all the way into your breadboard, and double-check the pin connections on your Pi board and make sure they match what was in the diagram above. Still stuck? Drop me a line, I’ll do my best to help.

Congratulations!

You did it! Stop a minute and catch your breath, I know that was a lot of steps. And you might be a little confused just what the heck we did, and how the code above works. We’ll get into that in the next blog post, but for now, celebrate. You’re one (very big) step closer to a SQL Server IoT device. Check back soon and follow me on twitter for the final piece of the puzzle, where we’ll configure our web service to talk to SQL Server, and our device to talk to our service.

2 thoughts on “SQL Server and Windows 10 IoT Part 2: Configuring your board and wiring your circuit

  1. Pingback: Wiring A Raspberry Pi 3 – Curated SQL

  2. Pingback: SQL Server and Windows 10 IoT Part 3: From Button Press to Service to Backup - Port 1433

Comments are closed.