{"id":231,"date":"2014-01-12T13:53:11","date_gmt":"2014-01-12T12:53:11","guid":{"rendered":"http:\/\/jcoder.me\/blog\/?p=231"},"modified":"2014-01-16T19:37:48","modified_gmt":"2014-01-16T18:37:48","slug":"netduino-netmf-ws2812-rgb-led","status":"publish","type":"post","link":"https:\/\/jcoder.me\/blog\/2014\/01\/12\/netduino-netmf-ws2812-rgb-led\/","title":{"rendered":"Netduino Plus 2 (NETMF) and WS2812 RGB LEDs"},"content":{"rendered":"<p>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 <a href=\"http:\/\/learn.adafruit.com\/adafruit-neopixel-uberguide\" target=\"_blank\">Adafruit NeoPixel \u00dcberguide<\/a>.)<\/p>\n<p>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.<\/p>\n<p>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\u03bcs to 0.8\u03bcs and some websites found out that the GPIO pins have a latency of 17\u03bcs). As I nearly bricked a Netduino before, I didn&#8217;t want to dive into custom firmwares at this point.<\/p>\n<p>So I found a different way to send the control codes to the LEDs at the needed speed as the following photo proves:<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/jcoder.me\/blog\/wp-content\/uploads\/2014\/01\/Netduino-with-WS2812-LEDs.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-230\" alt=\"Netduino with WS2812 LEDs\" src=\"http:\/\/jcoder.me\/blog\/wp-content\/uploads\/2014\/01\/Netduino-with-WS2812-LEDs.jpg\" width=\"640\" height=\"426\" srcset=\"https:\/\/jcoder.me\/blog\/wp-content\/uploads\/2014\/01\/Netduino-with-WS2812-LEDs.jpg 800w, https:\/\/jcoder.me\/blog\/wp-content\/uploads\/2014\/01\/Netduino-with-WS2812-LEDs-300x199.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p><strong>How does it work?<\/strong><\/p>\n<p>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\u03bcs (I haven&#8217;t evaluated this, so maybe I&#8217;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.<\/p>\n<p><strong>SPI<\/strong> (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.<\/p>\n<p>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&#8217;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.<\/p>\n<p>I&#8217;ve written two classes and uploaded them to github, so if you have a Netduino Plus 2 (or similar microcontroller) and don&#8217;t want to use a custom firmware to control WS2812 RGB LEDs, then maybe you want to give my code a try.<\/p>\n<p>Code on github: <a href=\"https:\/\/github.com\/jcoder\/NetMFNeoPixelSPI\">https:\/\/github.com\/jcoder\/NetMFNeoPixelSPI<\/a><\/p>\n<p><strong>Usage example:<\/strong><\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nNeoPixelSPI neoPixel = new NeoPixelSPI(Pins.GPIO_PIN_D10, SPI.SPI_module.SPI1);\nPixel pixelRed = new Pixel(255, 0, 0);\nPixel pixelGreen = new Pixel(0, 255, 0);\nPixel pixelBlue = new Pixel(0, 0, 255);\nPixel pixelWhite = new Pixel(255, 255, 255);\nPixel&#x5B;] block1 = new Pixel&#x5B;] { pixelRed, pixelWhite };\nPixel&#x5B;] block2 = new Pixel&#x5B;] { pixelBlue, pixelGreen };\nint waittime = 1000;\nwhile (true)\n{\n neoPixel.ShowPixels(block1);\n Thread.Sleep(waittime);\n neoPixel.ShowPixels(block2);\n Thread.Sleep(waittime);\n}<\/pre>\n<p>The library code is not optimized (yet), and I didn&#8217;t write a helper class for easier handling of long LED stripes (because I don&#8217;t own one), so feel free to extend the code. I&#8217;m not quite sure if this approach is really a good one, but it worked for me.<\/p>\n<p>Also feel free to leave a comment below if you have any remarks, ideas, etc.<\/p>\n<p><em>As always: use this information and the code on your own risk. ;)<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/jcoder.me\/blog\/2014\/01\/12\/netduino-netmf-ws2812-rgb-led\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":230,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6,4],"tags":[8,37,51,52,49,50],"class_list":["post-231","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","category-netduino","category-netmf","tag-c","tag-led","tag-netduino","tag-netmf","tag-rgb","tag-ws2812"],"_links":{"self":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/comments?post=231"}],"version-history":[{"count":7,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/231\/revisions"}],"predecessor-version":[{"id":239,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/posts\/231\/revisions\/239"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/media\/230"}],"wp:attachment":[{"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/media?parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/categories?post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jcoder.me\/blog\/wp-json\/wp\/v2\/tags?post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}