Getting started with IoT Core and C# on the Raspberry Pi

david kyle headshot

By: David Kyle
Principal Consultant, Data Exploitation

2nd August 2017

Most people reading this are no doubt familiar with the Raspberry Pi. It was originally designed for the education market but its combination of price, size, and IO (Input/Output) capability has made it a huge success for both the home hobbyist and professional seeking to create a prototype.

It’s a physically small single board machine with a set of IO pins that can be used to control real world functionality; that includes everything from lights, to central heating systems, and anything else you might be considering for your IoT (Internet of Things) project.

I’ve spent a good part of my career working with the Microsoft.NET framework and I think it’s a great platform. I’ve also spent a lot of time working with embedded firmware, and that tends to be C code. Unfortunately, and if you’re a C# expert unfamiliar with C, then the transition to the embedded world can be a jarring experience. To make matters worse, the Raspberry Pi was originally based on a Linux distribution which is another hurdle to the C# developer.

Of course it’s possible to learn new languages, but maybe you would just prefer to use the tools you already know and this is where “Windows IoT core” comes in. Windows IoT core is a free version of Windows 10 that runs on the Raspberry Pi, and lets you jump straight into embedded IoT using your existing C# skills. You’ll get all the benefits of Visual Studio and C# with the added bonus of control of the Raspberry Pi IO pins.

Step 1: Install IoT Dashboard

For this blog, I decided it would be fun to create a beginner’s guide that’s as much aimed at the hobbyist as it is the C# professional. So let’s get started by installing some of the tools you will need. Unlike a traditional PC, the Raspberry Pi doesn’t include a hard disk, and instead uses an SD memory card on which you will need to install IoT core using a Microsoft 10 application called “Windows 10 IoT core Dashboard”. You can get this by following this link (or just Google “IoT Dashboard”).

Once you’ve got IoT Dashboard installed, you need to choose the “Set up a new device” option, and then select “Download and Install”. Make sure you have the empty SD card in your Windows 10 desktop machine, choose the options I’ve shown (pictured), and select a password of your choice. IoT Dashboard will then fetch the IoT core image from Microsoft and write it to your flash card.

Please note: It is crucially important to have a completely clean and empty SD card; otherwise IoT Dashboard will report back errors without any indication of the cause. A great tool you can use is called “HDD LLF Low Level Format” and can be downloaded by following this link.

Running the HDD LLF Low Level Format tool will remove all partitions and data from your memory card. Just make sure that before you click “format this device” that you have definitely selected the correct hard disk. The tool will be just as happy to format your windows desktop hard disk if you select the wrong option (and you really don’t want to do this!).

Step 2: Start your Raspberry Pi

With that done, take the memory card out of your PC, insert it into the Raspberry Pi, connect power to the USB port, an Ethernet cable to your router, and the Raspberry Pi should boot. For power, I used a Samsung USB phone charger rated at 2 Amps. If you also connect a monitor via an HDMI lead you will see the familiar Windows boot sequence.

During boot up you will be asked a few setup questions, and if you have a keyboard and mouse plugged into the Raspberry Pi you can answer them. Otherwise, just ignore them and they will time out. When boot up is complete, you should get to the screen you can see on the left, but unfortunately that didn’t happen to me.




Step 3: Fix the Boot Error!


The first time I booted my Raspberry Pi everything worked as it should. However, after I re-imaged the memory card with the IoT Dashboard tool, I was presented with the blue screen (shown): “Your PC ran into a problem and needs to restart. We’re just collecting some error info, and then we’ll restart for you”. The Raspberry Pi then rebooted, returned to the same error screen and looped endlessly. This proved to be quite a frustration with lots of internet forum speculation as to the cause and usually erroneously blaming the flash card.

The way to fix this is to always use the HDD-LLF-Low-Level-Format tool before you use IoT Dashboard to image the memory card. If you do this, then everything should work fine. The format tool makes sure you have erased any partitions or data that might not be visible in Windows 10 but will stop the Raspberry Pi.

Step 4: Connect to and manage the Raspberry Pi

With the Raspberry Pi running you can now connect to it. Double check your Ethernet lead is connected, and then start up the IoT Dashboard application on your Windows desktop. Navigate to the “My Devices” option, and if everything has worked you will see your Raspberry Pi together with its IP address. Right click your Raspberry Pi, and select the “Open In Device Portal”. This will open a web site that’s hosted by your Raspberry Pi, and the first thing you will need to do is to enter the web site credentials. These are “Administrator”, and the password you entered when you first used IoT Device Portal to create your Raspberry Pi image.

The management portal lets you configure your raspberry Pi, change its network setup, install applications, and view its status.

Earlier we connected a monitor to the Raspberry Pi, but you can also use a Windows 10 app to view the Raspberry Pi display from your PC. (If you do this you won’t need to tie up your monitor). To make this work, make sure you enable the “Windows IoT Remote Server” option as shown in the picture, and then return to the IoT device portal, right click on your Raspberry Pi, and then choose “Launch IoT Remote Client”. If it’s the first time you’ve done this you will be taken to the Windows App store to install the application. If you find you get a blank screen when you start the application, double check you have followed these instructions correctly.

Step 5: Get Visual Studio ready

Before you can start creating code, you’re going to need to download and install Visual Studio. If you are reading this blog and know C# you’ve probably already done this. You can find the free Visual Studio Community 2017 edition from the Microsoft web site.

Next, and once you’ve got Visual Studio running, you need to get the project templates for a Windows IoT core project. To do this, Google “Windows IoT Core Project Templates for VS 2017”, and then follow the links to download “WindowsIoTCoreTemplatesDev15.vsix”. Run the template installer and when this is done (it takes a while!) you will find some new project templates in Visual Studio.

Step 6: Create and compile some code

We’re going to create the simplest code possible, and this is a “Windows IoT Core” application that is very much like a windows service on a desktop PC. It runs in the background, and starts as soon as the Raspberry Pi boots up. We’ll write the application so that it flashes an LED on and off. So, start Visual Studio, create a Windows IoT Core application, and then replace the application code with the below contents. (If you already know C#, then this is the easy bit!)

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using Windows.ApplicationModel.Background;
using Windows.Devices.Gpio;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
namespace BackgroundApplication1
{

public sealed class StartupTask : IBackgroundTask

{

public void Run(IBackgroundTaskInstance taskInstance)

{

GpioController gpioController = GpioController.GetDefault();

GpioPin pin = gpioController.OpenPin(26);

pin.SetDriveMode(GpioPinDriveMode.Output);

while (1 == 1)

{

pin.Write(GpioPinValue.High);

Task.Delay(5).Wait();

pin.Write(GpioPinValue.Low);

Task.Delay(5).Wait();

}

}

}

}

Step 7: Deploy and debug your code

Now we’ve got the code created you need to compile it like you do any other Visual Studio Project. You can then deploy it to the Raspberry Pi.

If you’ve not written embedded code before, then cross compilation will be a new concept to you. But it’s very simple. We are using Visual Studio to generate the compiled code, but rather than running it on the PC, we are going to use Visual Studio to transfer it to the Raspberry Pi, and then debug the code running on the Raspberry Pi using Visual Studio that is running on your desktop PC.

The first thing to do is select the “Remote Machine” option in Visual Studio (take a look at the diagram above) and this will launch the dialogue options shown. Set the address to the IP address of your Raspberry Pi, and set the Authentication Mode to “Universal (Unencrypted Protocol”). Next, right click on your project, and select “Deploy”. This will install your project onto the Raspberry Pi. Alternatively, you can click the green “Go” triangle to start your application, and you will be able to debug in Visual Studio just like any other application. At least that’s the theory ……

…. I experienced a lot of problems with Visual Studio refusing to talk to the Raspberry Pi, working one moment, and then failing the next. If this happens to you, then connect to the Raspberry Pi web portal and then navigate to the Processes\Details option. This shows all of the processes running on the Raspberry Pi, and if there are any called “MSVSMON” then kill these instances and reboot the Raspberry Pi. At least for me, this fixed all Visual Studio connection issues.

You will also discover that there is an option in the web portal called “Debug Settings” with a “Start” button to enable Visual Studio remote debugger. In my case, this needed to remain off for Visual Studio debugging to work (i.e. clicking the option stopped the visual studio debugger – perhaps a little confusing and counter intuitive!).

Once you have visual studio running, and you have deployed your application to the Raspberry Pi, you should be able to see it in the web management portal under the “Apps Manager” option. You can use this to start and stop an application; configure it to start automatically at boot up, and to uninstall the application.

Step 8: Beginners tips and words of warning

The code in the example is very simple, and all it does is use C# to open a GPIO pin, set the pin to be an output, and then enter an eternal loop to switch the pin on and off. You can easily connect this to an LED to prove that it’s working.

If you’re an absolute beginner, I’d recommend you buy a ‘bread board’ and some connecting leads for your experiments. It’s also very important that you understand the current limits of the Raspberry Pi GPIO pins. The pins can only supply enough electrical energy to drive an LED, and if you try and draw any more than this (for example to operate a relay) you will almost certainly damage the Raspberry Pi. To be safe, take a look at the Raspberry Pi website to find the maximum current draw, and make sure you don’t exceed this (If you “Google Raspberry Pi relay driver” you will be able to find out how to solve the problem. You can either build your own driver circuits, or alternatively buy modules designed for the purpose).

You also need to know which physical header pins on the Raspberry Pi correspond to which pin numbers you access in your code. There is a very helpful document on the Microsoft web site that gives you the pin mappings, but for convenience I’ve also summarised the information in the table at the end of this blog. In our diagram above we have the LED connected to ground on pin 39 (bottom left pin), and GPIO26 on pin 37 (second from bottom left pin). If your LED doesn’t flash, make sure you have connected it the correct way round!

Step 9: Build your IoT project!

So hopefully you found this a useful read, and hopefully it will get you past any of the initial setup hurdles in getting you started with your C# IoT project. Finally, and if you’re a skilled IoT developer, we are always looking for good engineers, and if you’re an IoT start up looking to take your prototype to the next level, our team of IoT engineers would love to talk!

Save

Save

GPIO# Power-on Pull Alternate Functions Header Pin
2 PullUp I2C1 SDA 3
3 PullUp I2C1 SCL 5
4 PullUp 7
5 PullUp 29
6 PullUp 31
7 PullUp SPI0 CS1 26
8 PullUp SPI0 CS0 24
9 PullDown SPI0 MISO 21
10 PullDown SPI0 MOSI 19
11 PullDown SPI0 SCLK 23
12 PullDown 32
13 PullDown 33
16 PullDown SPI1 CS0 36
17 PullDown 11
18 PullDown 12
19 PullDown SPI1 MISO 35
20 PullDown SPI1 MOSI 38
21 PullDown SPI1 SCLK 40
22 PullDown 15
23 PullDown 16
24 PullDown 18
25 PullDown 22
26 PullDown 37
27 PullDown 13
35 PullUp Red Power LED
47 PullUp Green Activity LED
GND 9,25,39,6,14,20,30,34
5V Power 2,4
3.3V Power 1

Save