# Using NeoPixels with Netduino Plus 2

## Overview

Good news for Netduino users who have been eyeing the glamorous NeoPixels strips and shapes in the Adafruit shop! We have managed to get NeoPixels working on the Netduino Plus 2 (shown below with a NeoPixel 8x8)

![](https://cdn-learn.adafruit.com/assets/assets/000/010/821/medium800/microcontrollers_DSC07215_%28Large%29.jpg?1378530474)

Even though there is lots of example code for NeoPixels for AVR, Propeller and PIC chips, it's a bit of challenge getting these LEDs to work with the Netduino. With the default firmware of the Netduino (any variation, Plus or no), it is not possible to send data to NeoPixels.  
  
The reason is due to the special high-timing-specific nature of the data sent to the pixels. NeoPixels, or WS2812/WS2811, use a single wire protocol that encodes bits as pulses, the time width of the pulses determine whether each bit is a binary 1 or 0. The following is a screenshot from the WS2812 datasheet.

![](https://cdn-learn.adafruit.com/assets/assets/000/010/787/medium800/microcontrollers_neopixel_datasheet_timing.png?1378416925)

But Netduino runs .NET Micro Framework, which means two important things: your code is interpreted, and there is a garbage collector.  
  
(note: neither of these are over-all disadvantages! In fact, they exist to make your code safer, and makes coding easier)  
  
Interpreted code means that you write your code in C#, but instead of directly being compiled to ARM assembly code (and subsequently, machine code), it is first compiled to "bytecode", and the processor runs an interpreter to read the bytecode, instead of directly executing machine code. The advantage of this is that the code is safer, you can detect errors like overflows, bad memory access, and check object type info. The disadvantage is that it is much slower than directly executing machine code.  
  
(note: from Wikipedia: " **Managed code** is a term coined by Microsoft to identify computer program source code that requires and will only execute under the management of a **C** ommon **L** anguage **R** untime virtual machine (resulting in bytecode).")  
  
(note: microcontrollers do not normally run interpreted code until you put a interpreter on it, the Netduino is a STM32 microcontroller with a C# CLR interpreter, the BASIC Stamp is a PIC microcontroller with a BASIC interpreter. Arduino is an AVR microcontroller and it does NOT use an interpreter.)

To illustrate my point, I wrote a simple loop in C# that toggles a pin, and observed the time it takes on an oscilloscope:```
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace Blinky
{
    public class Program
    {
        public static void Main()
        {
            bool pinState = false;

            OutputPort x = new OutputPort(Pins.GPIO_PIN_D0, pinState);

            while (true)
            {
                pinState = !pinState;
                x.Write(pinState);
            }
        }

    }
}
```

Here is the observed signal on the pin. This is a Netduino Plus 2, which runs at 168 MHz, the other Netduino variations all run at different speed.

![](https://cdn-learn.adafruit.com/assets/assets/000/010/788/medium800/microcontrollers_fastest_loop_possible.bmp?1378417030)

It takes 17.6 microseconds to toggle that pin. Now go back to the WS2812 datasheet, and you'll realize that it is impossible to meet the timing requirements using Netduino because the signals needs to be under 1 microsecond.  
  
The other problem is the garbage collector, which runs "once in a while". The garbage collector basically prevents memory leaks when you don't explicitly free memory you don't need anymore. But if it runs while we are pulsing signals to the WS2812, it might extend a pulse and violate the timing requirements. This problem can be solved by making sure that interrupts are disabled when we communicate with the WS2812.  
  
Fortunately, .NET Micro Framework and the Netduino firmware are open source. This means it is possible to write native code functions into the firmware, and call these functions from the managed code.  
  
(note: calling functions between managed and unmanaged code is referred to as **interop** , you might have come across this term if you tried to use DLLs in C# before)  
  
Our goal is to write a function in C or C++ that will toggle a pin with strict uninterruptable timing, in order to communicate to WS2812 NeoPixels. This function will then be compiled as a part of Netduino's firmware, and we will update the Netduino with this new modified firmware. After that, we should be able to call our new function from within the C# code.

- [Next Page](https://learn.adafruit.com/using-neopixels-with-netduino/prerequisits.md)

## Featured Products

### netduino Plus 2 (.NET-programmable microcontroller)

[netduino Plus 2 (.NET-programmable microcontroller)](https://www.adafruit.com/product/1104)
Netduino is an open source electronics platform using the .NET Micro Framework. Featuring a 32-bit microcontroller and a rich development environment, it is suitable for engineers and hobbyists alike.

**The Netduino Plus 2 has Ethernet cooked in already! There is a full TCP/IP...**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1104)
[Related Guides to the Product](https://learn.adafruit.com/products/1104/guides)
### Adafruit NeoPixel NeoMatrix 8x8 - 64 RGB LED Pixel Matrix

[Adafruit NeoPixel NeoMatrix 8x8 - 64 RGB LED Pixel Matrix](https://www.adafruit.com/product/1487)
Put on your sunglasses before wiring up this LED matrix - 64 eye-blistering RGB LEDs adorn the NeoMatrix for a blast of configurable color. Arranged in an 8x8 matrix, each pixel is individually addressable. Only one microcontroller pin is required to control all the LEDs, and you get 24 bit...

In Stock
[Buy Now](https://www.adafruit.com/product/1487)
[Related Guides to the Product](https://learn.adafruit.com/products/1487/guides)
### NeoPixel Stick - 8 x 5050 RGB LED with Integrated Drivers

[NeoPixel Stick - 8 x 5050 RGB LED with Integrated Drivers](https://www.adafruit.com/product/1426)
Make your own little LED strip arrangement with this stick of NeoPixel LEDs. We crammed 8 of the tiny 5050 (5mm x 5mm) smart RGB LEDs onto a PCB with mounting holes and a chainable design. Use only one microcontroller pin to control as many as you can chain together! Each LED is addressable as...

In Stock
[Buy Now](https://www.adafruit.com/product/1426)
[Related Guides to the Product](https://learn.adafruit.com/products/1426/guides)
### Breadboard-friendly RGB Smart NeoPixel - Pack of 5

[Breadboard-friendly RGB Smart NeoPixel - Pack of 5](https://www.adafruit.com/product/1312)
This is the easiest way possible to add small, bright RGB pixels to your project. We took the same technology from our Flora NeoPixels and made them breadboard friendly, with two rows of 3 x 0.1" spaced header on each side for easy soldering, chaining and breadboarding. These ultra-bright...

In Stock
[Buy Now](https://www.adafruit.com/product/1312)
[Related Guides to the Product](https://learn.adafruit.com/products/1312/guides)
### NeoPixel Ring - 16 x 5050 RGB LED with Integrated Drivers

[NeoPixel Ring - 16 x 5050 RGB LED with Integrated Drivers](https://www.adafruit.com/product/1463)
Round and round and round they go! 16 ultra bright smart LED NeoPixels are arranged in a circle with 1.75" (44.5mm) outer diameter. The rings are 'chainable' - connect the output pin of one to the input pin of another. Use only one microcontroller pin to control as many as you can...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1463)
[Related Guides to the Product](https://learn.adafruit.com/products/1463/guides)
### Adafruit NeoPixel Digital RGB LED Strip 144 LED - 1m Black

[Adafruit NeoPixel Digital RGB LED Strip 144 LED - 1m Black](https://www.adafruit.com/product/1506)
We crammed **ALL THE NEOPIXELS** into this strip! An unbelievable 144 individually-controllable LED pixels on a flexible PCB. It's completely out of control and ready for you to blink. This strip has a black mask, and an extra heavy flex PCB.  
  
These LED strips are even more...

In Stock
[Buy Now](https://www.adafruit.com/product/1506)
[Related Guides to the Product](https://learn.adafruit.com/products/1506/guides)
### Adafruit NeoPixel Digital RGB LED Strip 144 LED - 1m White

[Adafruit NeoPixel Digital RGB LED Strip 144 LED - 1m White](https://www.adafruit.com/product/1507)
We crammed **ALL THE NEOPIXELS** into this strip! An unbelievable 144 individually-controllable LED pixels on a flexible PCB. It's completely out of control and ready for you to blink. This strip has a white mask, and an extra heavy flex PCB.  
  
These LED strips are even more...

Out of Stock
[Buy Now](https://www.adafruit.com/product/1507)
[Related Guides to the Product](https://learn.adafruit.com/products/1507/guides)
### Adafruit NeoPixel Digital RGB LED Strip - Black 30 LED 5m

[Adafruit NeoPixel Digital RGB LED Strip - Black 30 LED 5m](https://www.adafruit.com/product/1460)
You thought it couldn't get better than [our world-famous 32-LED-per-meter Digital LED strip](http://adafruit.com/products/306) but we will prove you wrong! These NeoPixel strips have 30 digitally-addressable pixel LEDs per meter and are very affordable and are only 12.5 mm...

In Stock
[Buy Now](https://www.adafruit.com/product/1460)
[Related Guides to the Product](https://learn.adafruit.com/products/1460/guides)

## Related Guides

- [Making Adabot: Part 2](https://learn.adafruit.com/making-adabot-part-2.md)
- [CheerLights](https://learn.adafruit.com/cheerlights.md)
- [NeoPixel Basketball Hoop](https://learn.adafruit.com/neopixel-mini-basketball-hoop.md)
- [Glowing WhoVille Hat](https://learn.adafruit.com/glowing-whoville-hat.md)
- [Busy Box Interruption Sign](https://learn.adafruit.com/busy-box-interruption-sign.md)
- [Light Up Paper Dragon Wall Sconce](https://learn.adafruit.com/light-up-paper-dragon-wall-sconce.md)
- [NeoPixels on Raspberry Pi](https://learn.adafruit.com/neopixels-on-raspberry-pi.md)
- [CLUE Vertical Garden Weather Visualizer](https://learn.adafruit.com/clue-vertical-garden-weather-visualizer.md)
- [Superhero Power Plant](https://learn.adafruit.com/superhero-power-plant.md)
- [Superhero Power Gauntlet](https://learn.adafruit.com/superhero-power-gauntlet.md)
- [Alohamora Bottle](https://learn.adafruit.com/alohamora-bottle.md)
- [Holiday Icicle Lights with Flair](https://learn.adafruit.com/holiday-icicle-lights-with-flair.md)
- [Flora+NeoPixel LED Skateboard Upgrade](https://learn.adafruit.com/flora-neopixel-led-skateboard-upgrade.md)
- [GEMMA Hoop Earrings](https://learn.adafruit.com/gemma-hoop-earrings.md)
- [1,500 NeoPixel LED Curtain with Raspberry Pi and Fadecandy](https://learn.adafruit.com/1500-neopixel-led-curtain-with-raspberry-pi-fadecandy.md)
