Scott Hanselman

The .NET Micro Framework - Hardware for Software People

September 08, 2010 Comment on this post [24] Posted in Hardware | Micro Framework | Open Source
Sponsored By

imageI'm definitely a software person. I took EE in school and made an LED class, then a small computer like everyone else, and I know my volts and my amps for the most part, but that's about it. The limits of my skills are somewhere around adding an LED and some resistors to leech power off a USB adapter (which I recently did while working on the Hanselcade retro arcade build).

I look at hardware guys like Clint Rutkas in awe. I mean, seriously, who builds a T-shirt cannon from scratch for fun? Amazing.

Clint sent me a "Netduino" board today. It's similar to an Arduino board, except it uses the .NET Micro Framework. Micro you say? That's techie-speak for "tiny ass framework." I spoke to Colin Miller about this earlier in the year on video at Channel 9.

Remember my SPOT watch from 2004? That's Smart Personal Objects Technology, which is marketing-speak for "tiny ass framework." That watch is six years old (and still running nicely, sitting on my desk, in fact) and ran .NET.

Fast forward to today and I find myself plugging in this Netduino board to my computer and following Pete's Hello World Tutorial and I'm looking at this namespace.

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

It's back!

Ok, putting it all together in context. The Netduino is a board that's mostly Arduino compatible and has a published schematic (PDF here) so you could make one yourself, if you wanted. The .NET Micro Framework (or TinyCLR as some folks have called it) is literally that - it's a tiny CLR that runs .NET byte code. You can write C# and it'll run on tiny CPUs with tiny amounts of memory (like 64k or something similarly smallish.) It's been with us all this time, and there is an enthusiastic community built around it.

The .NET Micro Framework 4.1 source is available, it's Open Source under the Apache 2.0 License. (Ya, the new Microsoft is freaking me out also. There's a lot of source that's quietly making its way out under increasingly liberal licenses.) There's lots of great details at Pete's blog.

Here's what a Netduino looks like:

Netduino Overhead Photo

I'm going to think of some hardware ideas that I can build with this. I also have a more capable and fancy Tahoe II with a touch-screen, accelerometer, buttons and more. If you're looking to prototype something quick, or even build a complete system with an off-the-shelf board, do check it out! Here's what a Tahoe II looks like. Remember, all these boards use C# and .NET. It's amazing writing something for hardware using a language and framework I already know how to use. It literally gets me 80% of the way there from a learning curve perspective.

TahoeII Large Development Board

There's also the GHI Electronics EMX Development system, so there's a lot of choices.GHI-00129 Large Development Board

With each of these boards (and others) you just need to get the Micro Framework 4.1, then the SDK for that specific board. It integrates into Visual Studio 2010. If you want to change the product, they are taking proposals in the .NETMF Forums.

Directly from Pete's blog:

Getting Started

What you'll need:

  • Netduino (Scott: or some other .NET Micro Framework board)
  • USB Cable (early Netduino units come with the USB cable) (Scott: Usually a micro- or mini-USB)
  • Visual Studio 2010 and the .NET Micro Framework 4.1 SDK  (you can use C# Express 2010 if you don't have Visual Studio)
  • Netduino SDK in 32 bit or 64 bit, depending on your host OS.
  • Optional: shields and starter kits to do cool things with netduino. Existing Arduino shields are compatible. A shield is just an add-on card that fits the pins on the board.

The SDK installs a device driver for talking to the Netduino. Make sure you select the one with the appropriate bitness, and that you install it before connecting the Netduino to the PC. I installed the VS2010 bits before the SDK, but it shouldn't matter.

Once you plug in the Netduino, using the USB cable, you should see the device driver get installed, and the power LED on the board light up.

Hello World with Morse Code

Now I just have the Netduino for now, so I haven't got any attachments. If I was a hardware guy, I'm sure I'd go try to take apart a toaster or remote control and declare something like "this toaster just needs a one OHM resister on pin-out 5A so I can invert the voltage and it'll toast bread over Bluetooth" but I have no idea what that means. All I can do with the Netduino out of the box to flash its LED, as Pete points out:

public static void Main() 
{
OutputPort onboardLed = new OutputPort(Pins.ONBOARD_LED, false);

while (true)
{
onboardLed.Write(true);
Thread.Sleep(500);

onboardLed.Write(false);
Thread.Sleep(500);
}
}

Let's make it fancier. How about outputting string using Morse Code? Wikipedia says a dot is 100ms long and a dash is 300ms. How hard can it be?

I could go to StackOverflow as they had a contest to see who could make the SMALLEST implementation that would take a string and output Morse Code. They have an extremely optimized (for lines of code) solution. But it's extremely silly. Certainly no more silly than me making an LED blink Morse Code as well, but I'd like to be able to actually read my code. ;)

So, here's a naive 10 minutes solution using this guys' two arrays because I'm too lazy to type up the Morse myself. I could have use a Hashtable also, but two parallel arrays was fine too. The .NET Micro Framework, being micro, doesn't have everything the full framework has. However, being open source, it has taken contributions and version 4.1 includes a Hashtable implementation.

I can even debug directly connected to the board!

netduino debugging

Here's my sad little program (it was very easy!)

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Text;
using System.Collections;

namespace NetduinoApplication1
{
public class Program
{
public static void Main()
{
OutputPort onboardLed = new OutputPort(Pins.ONBOARD_LED, false);

while (true)
{
onboardLed.Write(false);

foreach (char c in " hello scott hanselman ")
{
string morse = ConvertTextToMorse(c);
Debug.Print(c + " = " + morse);
TransmitDotOrDash(onboardLed, morse);
}

}
}

private static Char[] Letters = new Char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', ' '};

private static String[] MorseCode = new String[] {".-", "-...", "-.-.",
"-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---",
"...--", "....-", ".....", "-....", "--...", "---..", "----.", " "};

public static String ConvertTextToMorse(char c)
{
int index = -1;
index = Array.IndexOf(Letters, c);
if (index != -1)
return MorseCode[index];
return string.Empty;
}


public static void TransmitDotOrDash(OutputPort port, string dotordash)
{
foreach (char c in dotordash)
{
TransmitDotOrDash(port, c);
}
Thread.Sleep(300); //gap between letters
}

public static void TransmitDotOrDash(OutputPort port, char dotordash)
{
if (dotordash == ' ')
{
port.Write(false);
Thread.Sleep(700); //gap between words
}
else //it's something
{
port.Write(true);
if (dotordash == '.')
Thread.Sleep(100); //dot
else
Thread.Sleep(300); //dash
port.Write(false);
}
}
}
}

Here's the debug output as I flash "hello scott hanselman" from the board.

Debug Output from the Netduino Board

All it all, it really couldn't be much easier. Next I'll try to get the Tahoe II working and maybe make a game for the boys. Perhaps hook up a speaker and a proximity sensor and see if they can sneak up on it.

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
September 08, 2010 4:01
That is awesome!

Thanks for this.
September 08, 2010 4:47
That is really cool. All your array needs is a few punctuation marks and you'd actually be able to send properly formed sentences. For example, a comma is "--..--", a period is ".-.-.-" and a question mark is "..--..".

I've been tossing around an idea in my head for a program for fellow hams to use in the Alabama QSO Party radio contest. The code you posted above give me an idea for a feature. Instead of toggling an LED just have the computer toggle the DTR or RTS line on a specified com port.

--... ...-- -.. . -.- ....- --. -.. .--
September 08, 2010 10:10
I'm just reading the source code and did not test it, but shouldn't the line

Thread.Sleep(300); //gap between letters

go inside the loop?
September 08, 2010 12:55
I believe building robots will be much easier now.

September 08, 2010 13:45
Looks nice (although I gave up using Microsoft software the same moment I bought my MacBook). For tiny programming, I prefer carrying around my Ben NanoNote: A linux (OpenWRT from the box, can boot Debian) powered palmtop computer. Or handheld, whatever you prefer... Sadly, it has no connection power (no wifi, networking only via a USB connection to a host computer acting as proxy). But it is one of the cutest pieces of hardware I have ever since, although I use it for mostly nothing these days :/

Of course, it makes one of the best pocket calculators out there... I am still trying to find a good way to use it as a pocket calculator completely (partially ported YaCAS to OpenWRT), but gnuplot doesn't fit well.

Ruben
September 08, 2010 21:53
@Benjamin: No. The entire string printed by that routine is a single letter in plain text. The delay goes after all the dots and dash are sent.

September 08, 2010 22:50
Thanks for sharing this Scott!

I was actually looking into what it would take to write C# code on an Arduino this past weekend. Your post is well timed and has saved me a lot of searching!

Now it's time to build something remarkable (or at least something that turns an LED on and off)!

Thanks again!
September 08, 2010 23:53
At first I thought it was just doing I/O from your PC to this card... then I realised the code actually runs on the card! Nice.
September 08, 2010 23:56
That last comment from me. Just ditch Google from the OpenID list for the time being - it's useless.
September 09, 2010 0:30
Funny you mention that. I'm upgrading the blog now to deal with Google's stupid ass Open Id stuff.
September 09, 2010 0:46
Scott, great article! BTW, your VS font caught my eye when reading the article. Can you share the name? Thx!
September 09, 2010 1:00
That's Consolas 15.
September 09, 2010 4:14
Gee - I always wondered what the SPOT watches ran. I have two of them - one still running on my wrist every day. IIRC, those watches came out before the MF was announced - or right about the same time.
September 09, 2010 7:21
The Netduino is really pretty slick. And it's hardware compatible with the majority of the Arduino shields out there. And, with a bit of work, you can usually port a fair bit of the Arduino code to C# too.

The Netduino forums are here, and there are several blogs starting to cover Netduino projects now too.
September 10, 2010 6:39
Article was very timely. I had ordered my netduino for play 15 minuts before checking on this blog post! I will definitely use Scott's code as something else to play with until I get some components!
September 11, 2010 10:01
Wow this is so cool. Can you build a communicator watch yet? I think we're almost there! =)
September 11, 2010 11:17
Wow, I'm gunna buy that, looks like fun!
September 12, 2010 3:56
I wish there was support for the Propeller from Parallax.com.
Would like to see the TPL ported to MF for that one :)
September 13, 2010 14:13
Interesting. Always been curious about low level code, all these years dealing with SQL express, vba etc and most of us can barely get an led to flash. Imagine the possibilities, tweet a special code and feed your fish from 1000 miles away. Awesome.
September 13, 2010 18:34
Hi Scott,

Is there a reason you did not use a Dictionary in your Morse Code?
No Dictionary in the Micro Framework or just a quick hack code example?

Markus
September 14, 2010 1:59
Yes, no dictionary or IList that I could find. Version 4.1 added a hashtable, but this seemed easier.
September 15, 2010 23:39
Very cool
September 17, 2010 8:39
Nice! Love this post. It makes me feel one step closer to Tony Stark. Who doesn't love that guy. Nerd Chic.
September 22, 2010 23:30
@Ben Coffman

Ironman, FTW!

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.