Category Archives: Netduino

Netduino Plus 2 (NETMF) and WS2812 RGB LEDs

Some weeks ago I got some Adafruit NeoPixel, a compact RGB LED module. Each LED module has a WS2812 chip that only needs three wires: power, ground and data. The data protocol is self-clocking, at a rate of 800KHz. The LED modules can be chained to build longer LED stripes. (For more details see the Adafruit NeoPixel Überguide.)

Adafruit provides an Arduino library on github to control the LEDs, but there is no direct support for Microsoft .NET Microframework (NETMF) controllers like the Netduino Plus 2 that I was going to use.

Actually, there is an advanced guide to use NETMF controllers with the WS2812 LEDs, but it requires a custom firmware, because it seems that the GPIO pins of most NETMF controllers are not fast enough to switch the signal at the needed rate (there is a timing needed in the range of 0.4μs to 0.8μs and some websites found out that the GPIO pins have a latency of 17μs). As I nearly bricked a Netduino before, I didn’t want to dive into custom firmwares at this point.

So I found a different way to send the control codes to the LEDs at the needed speed as the following photo proves:

Netduino with WS2812 LEDs

How does it work?

I thought that there must be a faster way to get signals off a Netduino Plus 2 because it has an Ethernet port and SD card slot, and I suppose they need to be adressed faster than 17μs (I haven’t evaluated this, so maybe I’m wrong with that, but it led me in the right direction). Looking at the ports that the Netduino provides my attention was drawn towards the SPI interface.

SPI (Serial Peripheral Interface) is a four-wire master-slave interface for full-duplex communication between chipsets. It has a clock wire (SCLK), output (MOSI) and input (MISO). Normally it can be used at a frequency up to some MHz, so the ports must be really fast.

My assumtion was, that I could only use the output (MOSI) pin, configure the frequency accordingly and send the needed control bytes on that wire to the LEDs. I tried some settings and data packets, but first without any success, but luckily I wasn’t the first one to try this, so I found some code snippets for different microcontrollers, that pointed me into the right direction, e.g. that a timing of 6.666MHz is recommended.

I’ve written two classes and uploaded them to github, so if you have a Netduino Plus 2 (or similar microcontroller) and don’t want to use a custom firmware to control WS2812 RGB LEDs, then maybe you want to give my code a try.

Code on github: https://github.com/jcoder/NetMFNeoPixelSPI

Usage example:

NeoPixelSPI neoPixel = new NeoPixelSPI(Pins.GPIO_PIN_D10, SPI.SPI_module.SPI1);
Pixel pixelRed = new Pixel(255, 0, 0);
Pixel pixelGreen = new Pixel(0, 255, 0);
Pixel pixelBlue = new Pixel(0, 0, 255);
Pixel pixelWhite = new Pixel(255, 255, 255);
Pixel[] block1 = new Pixel[] { pixelRed, pixelWhite };
Pixel[] block2 = new Pixel[] { pixelBlue, pixelGreen };
int waittime = 1000;
while (true)
{
 neoPixel.ShowPixels(block1);
 Thread.Sleep(waittime);
 neoPixel.ShowPixels(block2);
 Thread.Sleep(waittime);
}

The library code is not optimized (yet), and I didn’t write a helper class for easier handling of long LED stripes (because I don’t own one), so feel free to extend the code. I’m not quite sure if this approach is really a good one, but it worked for me.

Also feel free to leave a comment below if you have any remarks, ideas, etc.

As always: use this information and the code on your own risk. ;)

Leaving the FEZ comfort zone – say hello to the breadboard

I’ve been very busy over the last weeks, but now I’ve taken some time to get deeper into the “core electronics part”, which simply means that I got a breadboard and some electronic components like resistors, transistors, LEDs and a lot of wires.

The first step was to get a normal LED on the breadboard blinking by connecting it directly to the Digital IO ports on the FEZ Panda board. Sounds easy but I did not want to break anything so after carefully reading some postings about the topic I connected die Digital IO port to a 220 Ohm resistor which was connected to the LED and then back to Ground. Using the OutputPort class on the given IO port the LED was blinking. That easy :)

As it was not as complicated as I thought it would be, I remembered the 2×24 LCD panel that I got from a friend. I wasn’t sure how it exactly works, but as I found out it has a HD44780 controller on-board that accepts 4-bit commands using additional wires for signals and power. And there is even a very useful C# class at the GHI Electronics website, nearly ready to use.

And guess what? After connecting the wires and including the pre-defined class I got the LCD working! Awesome!

I actually had to de-wire everything to put it back in the boxes for storage, but it’s not as hard as I thought to put everything together, so manybe I’ll use the LCD in some of the next projects.

And the next part of equipment is waiting: I just soldered the needed connectors to a 4×7 segment LED element, so stay tuned ;)

Netduino Scripted Lights

Playing around with a FEZ Panda II is a lot of fun, especially when it comes to LED lights (and when it’s getting dark outside). Sure, you can make them simply blink, or maybe progam a more complex light-switching pattern, but you always end up with a lot of code to address the LEDs and wait some time between changing their state.

To make it easier to work with an LED, I’ve written a small wrapper class some time ago as described in this posting, but I really wanted to come around having to write a lot of (non-reusable) code just to make some LEDs blink.

My current solution: a very simple, char-based scripting module to control the LEDs. This is how it works:

  • You define some simple script like "R2 r2" or "GH4 YZ4 a2"
  • Based on the ILights interface, you create an instance of LEDLights
  • An outer loop defines how often the sequence should be executed
  • The inner loop iterates over the sequence and passes each char to the mini engine
  • The lights start blinking, or whatever you told them to do

Actually, some of these steps could be encapsulated and the pin configuration for the LEDs is currently hard-coded, but for the beginning I wanted to keep the things as easy as possible.

See it in action

How does the script work?

In short: an uppercase letter switches the LED on, a lowercase letter switches it off, and a numerical letter pauses between two actions. I recently got some additional LEDs so I added support for 6 LEDs (please look up the pin configuration in the source code).

  • r / R : red LED no. 1
  • s / S : red LED no. 2
  • y / Y : yellow LED no. 1
  • z / Z : yellow LED no. 2
  • g / G : green LED no. 1
  • h / H : green LED no. 2
  • a / A : all LEDs at once
  • 1, 2, 3, …, 9 : pause for 500ms, 1000ms, 1500ms, …, 4500ms

All other characters are currently ignored, so you can add spaces to separate the parts of the script which makes it easier to read the script sequence.

Some examples

R2 r2 – the red LEDs is switched on, then waits for 1000ms, then  the LED is switched off and the script waits again for 1000ms

R1Y1G4 r1y1g4 – the red, yellow and green LED is switched with short delay, then switched off again

zY2 yZ2 – the two yellow LEDs blink alternately

Tipps, tricks and ideas

When defining a sequence make sure that you add pauses, because otherwise the LEDs fill flicker based on the Netduino’s maximum speed.
It’s easy to forget the pause at the end of the script, which makes the sequence look strange when it gets repeated.
Of course, you can switch off an LED at the beginning of the script, even if it was not switched off before. This is very useful if the sequence has a complex overall pattern and an LED must stay switched on at the end of the script.

Some ideas to extend this project:

  • Store more than one sequence in the program and add an IR-receiver or simply a button to switch the sequences
  • Read the sequences from an SD card
  • Connect the Netduino to the network and add either a webserver to the program to enter the sequence or fetch it from a website (or even from Twitter)

Source Code

For the LED class please look at this posting.

public interface ILights
{
    void LightCommand(char c);
}

public class LEDLights : ILights
{
    private int baseDelay = 500;

    private LED led_R = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di1);
    private LED led_Y = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di0);
    private LED led_G = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di8);
    private LED led_S = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di5);
    private LED led_Z = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di3);
    private LED led_H = new LED(GHIElectronics.NETMF.FEZ.FEZ_Pin.Digital.Di2);

    public void LightCommand(char c)
    {
        switch (c)
        {
            case 'r': led_R.Off(); break;
            case 'R': led_R.On(); break;
            case 'y': led_Y.Off(); break;
            case 'Y': led_Y.On(); break;
            case 'g': led_G.Off(); break;
            case 'G': led_G.On(); break;
            case 's': led_S.Off(); break;
            case 'S': led_S.On(); break;
            case 'z': led_Z.Off(); break;
            case 'Z': led_Z.On(); break;
            case 'h': led_H.Off(); break;
            case 'H': led_H.On(); break;
            case 'a': led_R.Off(); led_Y.Off(); led_G.Off(); led_S.Off(); led_Z.Off(); led_H.Off(); break;
            case 'A': led_R.On(); led_Y.On(); led_G.On(); led_S.On(); led_Z.On(); led_H.On(); break;
            case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': Sleep(c); break;
        }
    }

    protected void Sleep(char c)
    {
        int num = c - '1' + 1;
        int delay = num * baseDelay;
        Thread.Sleep(delay);
    }
}

public class Program
{
    public static void Main()
    {
        string sequence = "R2 r2;
        // process sequence
        ILights lights = new LEDLights();
        // switch off all lights at the beginning
        lights.LightCommand('a');
        // outer loop
        bool looping = true;
        while (looping)
        {
            for (int i = 0; i < sequence.Length; i++)
            {
                char c = sequence[i];
                lights.LightCommand(c);
            }
        }
    }
}

Networking with the FEZ Panda II

First of all: using the ethernet connection with the FEZ Panda II works well if you follow the steps from GHIElectronic’s eBook “FEZ Internet of Things”. You just set up MAC, IP, netmask, gateway and DNS server settings and you are ready to go.

But, there is actually a drawback: although the FEZ Panda II uses the .NET Microframework 4.x (NETMF), it does not fully support the System.Net namespace due to resource limitations of the device!

This means that all code using System.Net from the NETMF will not work, giving you a System.NotSupportedException. One disadvantage of this is, that most 3rd party NETMF projects using networking will not work as well.

The workaround to this problem is to use the network classes provided by GHIElectronics. In detail, it helps to add the following references to your project:

  • GHIElectronics.NETMF.Net
  • GHIElectronics.NETMF.W5100
  • GHIElectronics.NETMF.W5100.Http

Those references provide all the classes used to successfully establish network connections via TCP or UDP. As said above, the examples in the eBook are very useful, so I’ll not go into detail here ;)

Now that I basically got the network connection working I’m going to think about some example usages and over the course of the next weeks I’ll share my observations (and maybe some code too) here.

Choosing a private MAC address

When doing some research for connection my Netduino to the local network (using the Connect Shield) I stumbled upon the fact that I not only need to set the IP adress, network mask and gateway, but also the device’s MAC address – something that you normally don’t need to do because the vendor of the network device burned a globally unique MAC address into the device.

While it should not be a serious problem to randomly generate a MAC address (which might interfer with a real device’s MAC address somewhere on the planet) I wanted to do it “the right way”. There are many hints on the internet how to choose a private MAC address, mainly for use in virtual computers and many information about the pattern how MAC addresses for vendors are built.

So, to cut a long story short, I’ve come to the conclusion that it can be considered safe to use a MAC address starting with 01 followed by a random combination. I’ll give it a try and if it doesn’t work, then I’ll do some more in-depth research on the topic ;)

Next: external power supply and SD card

After taking a short break with Netduino hacking I’ll start over on the next days to test an external power supply that I’ve bought some days ago. It has an adjustable volatge range from 3 to 12 V and comes with a variaty of connectors. This external power supply is intended to support projects to be run independently of an USB power source of a computer.

I also got two micro SD cards which hopefully will work with the Netduino and which will allow to store a decent amount of runtime data.

Over the course of the next days I’ll try to get the two things mentioned above up and running and as always I’ll share the results of my tests with you.

Update: the power supply works pretty well.

New piece of hardware: an LCD

Guess what I found at my desk when I came to the office today?

Saying thank you to my coworker Oliver for long-time renting me this 2×24 LCD with a HD44780 controller. I’m afraid that it’ll take some weeks to get it up and running, because currently I don’t have any breadboards, cables or things like that.