Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / MFC

Embedded Programming – Getting Started With the Netduino

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
8 Apr 2016CPOL7 min read 16.7K   10  
Embedded Programming – Getting Started With the Netduino

I have always had an interest in hobby electronics and, ever since I can remember, I have always taken things apart just to see how they work. In the early 1990s, the BASIC Stamp processor was available to electronic hobby enthusiasts. Since 2005, there have been some additions providing a wider selection of processors. Two interesting choices available today include the Arduino and Netdunio platforms. This post will cover the basics of getting started with the Netduino development platform.

For this introduction, it is assumed that users have a basic understanding of programming with a .NET Framework and C#. The code samples provided are based on the Netduino Plus platform. If you are using a standard Netduino, most of the code should still work; however, the project references are slightly different. If you are using a Netduino Mini, some of the code will not work as I will be using the onboard LED and the mini is not equipped with an onboard LED. However, if you are using the mini, it is very likely that you are beyond the introductory level (Hint: If you haven’t yet purchased a board, I suggest the Netduino Plus for its additional features).

Getting Setup

An excellent getting started guide is available on the Netduino website, but I will summarize the steps here.

First, you are likely going to need to download a few items:

  1. Visual Studio 2010 (C# Express edition)
  2. .NET micro framework software development kit (SDK)
  3. Netduino SDK in either 32bit or 64bit

Update February 25, 2012 – Virtual Environment

I prefer to do my programming on virtual machines/workstations (VM) and working from a VM is possible (at least with VMware workstation 8, I haven’t tried with any others). To get started, just follow the instructions on the VM. For the host, you will need to install only the Netduino SDK to target the appropriate platform: 32 bit or 64 bit. There are no issues with having a 64 bit host and a 32 bit VM.

If you have Visual Studio 2010 (VS2010) installed, you may be able to skip this step. If you don’t or your version doesn’t include C#, you’ll have to download the free version or purchase another version. For the free download, ensure you’ve downloaded the C# Express edition. After installing VS2010, install the .NET micro SDK. Finally, you can install the Netduino SDK. If you have a 32bit operating system, download and install the 32bit version of the SDK or if you have a 64bit operating system, download and install the 64bit version of the SDK.

Before you start VS2010, connect your Netduino to your computer through the USB port so that the drivers can be downloaded.

After the drivers are installed, you can start VS2010 and create a new project.

My First Program

Now that all the required software is setup, we can start with our first program. Our first application will be the de facto “Hello World” of embedded programming: the blinking LED. The Netduino Plus and standard Netduino both have an onboard LED. This will simplify things slightly since don’t have to wire up our own circuit (a subject left for later tutorials). Again, if you have a mini, there is no onboard LED, so you will have to wire in your own circuit and adjust the code accordingly.

From the Start Page, click New Project (or from the menu select File -> New -> Project…).

This will open the “New Project” window.

The projects are located in the “Installed Templates” under Visual C# -> Micro Framework

Select the project template based on the Netduino board you have purchased. For the rest of the instructions, I will be using a Netduino Plus.

Visual Studio will create the project, add references, and create the default Program.cs file.

Looking at the references in the solution, there will be two from Secret Labs. Because we have selected the Netduino Plus, a reference to the SecretLabs.NETMF.Hardware.NetduionPlus.dll has been added. If you have a different board, this reference will be different. If you have mistakenly selected the wrong project template or wanted to move a project to a different board, remove the reference and add the reference to the appropriate platform (SecretLabs.NETMF.Hardware.Netduion.dll for the standard or SecretLabs.NETMF.Hardware.NetduionMini.dll for the mini).

Looking at the default code file created, Program.cs, you can see a number of references have been added:

  1. Using statements to the .NET micro framework have been added. It’s important to note that these are not the same as for a regular .NET solution. The namespaces are specific to the micro framework which is anemic when compared to the rich domain of the standard .NET libraries.
  2. Using statements to the SPOT (Smart Personal Objects Technology) are included. These DLLs include most of the embedded and hardware related classes and interfaces.
  3. Using statements for the Netdino specific implementations have been added. As previously mentioned, this could be slightly different based on the hardware used.
  4. The template includes a stub for the main method. As it states in the comment, “Write your code here”. That might be ok for (really) small projects or our first tutorial, but it might be better to read it as “write some of your code here”.

Now we need to change some of the solution properties. Open the project’s properties (double click on the Properties folder in the solution explorer) and switch to the .NET Micro Framework tab. The emulator is selected by default and we’ll need to switch this to the Netduino device.

Under Transport, select USB.

This will change the Device setting to NetduinoPlus_NetduinoPlus (if you have another platform, this will reflect what is currently attached to the computer)

After adjusting these settings, the build process will upload the code to the Netduino board through the USB port.

Our first program is very simple, blink the onboard LED at approximately 0.3 Hz:

  1. Within the Main method, at line 15, we initialize a new Output port called onboardLed. The new method takes two parameters: pin ID and a Boolean.
    In most cases, the port relates on one of the numbered ports on the board; however, in this case, use the onboard LED.
    The Boolean value sets the initial state of the LED, either on or off (true for on; false for off). In the code, the LED is initially set to off (false).
  2. At line 17, create a loop. Generally for embedded programs a non-exiting loop is required. Within the loop resides the code that is repeated over and over again while power is supplied to the system. A possible exception to this would be if you designed your system around interrupts (or events). In cases where you use an interrupt based system, the loop may not be required.
  3. 3. Lines 18-21, contain the work for the system to complete.
    On line 18, call the write method on the output port onboardLed. The write method takes a single Boolean parameter, like the constructor, true would apply power to the port and turn on the LED while false would cut power to the LED and turn it off.
    Since the initial state is set to off, turn the LED on.
    On line 19, put the current thread (the thread that the Main method is running under) to sleep for 1500 milliseconds (or 1.5 seconds).
    Next on line 20, the write method is called on onboardLed with false to turn the LED off.
    On line 21, sleep the current thread for another 1500 milliseconds.

     

The LED will flash until the power is disconnected or the reset button (labeled as SW1 on the Netduino Plus) is pushed. If power is reapplied or the reset is pushed, the whole process starts over: onboardLed is initialized in an off state, the loop is entered, and the LED flashes on and off at approximately 0.3 Hz. This is important because if you created any variables, all information would be lost if the power is lost or the reset is pressed.

This concludes the initial post. My next instalment will include code refinement. Next, the user will move the work outside the main method and create a separate class for the LED.

Additional Information

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --