{"id":279,"date":"2015-02-25T22:17:01","date_gmt":"2015-02-26T06:17:01","guid":{"rendered":"http:\/\/thenscaler.com\/?page_id=279"},"modified":"2015-10-24T17:58:36","modified_gmt":"2015-10-25T00:58:36","slug":"an-introduction-to-arduino-addressable-rgb-leds","status":"publish","type":"page","link":"https:\/\/thenscaler.com\/?page_id=279","title":{"rendered":"An Introduction to Arduino &#038; Addressable RGB LEDs"},"content":{"rendered":"<p style=\"padding-left: 30px;\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/3-leds.gif\"><img loading=\"lazy\" decoding=\"async\" class=\" size-thumbnail wp-image-281 alignleft\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/3-leds-150x150.gif\" alt=\"3-leds\" width=\"150\" height=\"150\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/3-leds-150x150.gif 150w, https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/3-leds-50x50.gif 50w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>LEDs are the model railroader&#8217;s best friend.\u00a0 Low power consumption and a wide variety of colors, sizes\u00a0and styles make them the go-to lighting source for modeling in the 21st century. Even when you are looking for a distinctly &#8220;incandescent&#8221; look, LEDs can do the\u00a0 job better than a miniature incandescent bulb. In addition to single color LEDs, bi-color packages\u00a0include two LEDs with distinct colors (red &amp; green, for example), allowing the led to &#8220;change color,&#8221; useful as\u00a0a panel indicator or signal light.<\/p>\n<p>RGB LEDs are a three-led package that are intended to function like a display &#8220;pixel&#8221; and produce any one of 16 million possible colors. Theoretically, at least, RGB LEDs can meet almost any lighting need.<\/p>\n<div id=\"attachment_282\" style=\"width: 160px\" class=\"wp-caption alignright\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/rgb-led.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-282\" class=\"size-thumbnail wp-image-282\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/rgb-led-150x150.jpg\" alt=\"Typical &quot;common cathode&quot; RGB LED\" width=\"150\" height=\"150\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/rgb-led-150x150.jpg 150w, https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/rgb-led-300x300.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/rgb-led-50x50.jpg 50w, https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/rgb-led.jpg 600w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><p id=\"caption-attachment-282\" class=\"wp-caption-text\">Typical &#8220;common cathode&#8221; RGB LED<\/p><\/div>\n<p>RGB LEDs require a microcontroller, such as an Arduino, to modulate the red, green and blue\u00a0channels respectively\u00a0via PWM. So while the upside of RGB LEDs is the huge range of colors they can produce, the downside is that a single RGB LED requires 3 pins on your Arduino. Ouch!<\/p>\n<p><strong>Addressable LEDS<\/strong><\/p>\n<p>A particularly elegant solution to the pin problem is to use &#8220;addressable&#8221; RGB LEDs.\u00a0 Addressable RGB LEDs (ALEDS from here on) are a special package that includes an\u00a0embedded controller chip\u2014I use the ones with the WS2812 chip because they operate at +5v. There are other WS28XX versions that do the same thing, with different specs, so different versions may not be compatible with each other.<\/p>\n<p>The controller chip takes over the job of modulating the LEDs to produce the color set by an attached microcontroller.\u00a0Data is communicated serially over one line (using only one Arduino pin), cutting your pin commitment down to what it would be with an ordinary, mono-color LED.<\/p>\n<p>It gets better\u00a0\u2014 the WS2812 also functions as a shift register, responding to its part of the data stream, and shifting the rest on to the next unit in the chain.\u00a0 Thus,\u00a0a single data line (and still only one pin on an Arduino)\u00a0can control\u00a0multiple addressable LEDs wired in series.<\/p>\n<p>ALEDS comes in a number of package styles, including both <a href=\"http:\/\/www.adafruit.com\/products\/1379\" target=\"_blank\">SMD\u00a0<\/a>and <a href=\"https:\/\/www.sparkfun.com\/products\/12999\" target=\"_blank\">PTH <\/a>types.\u00a0Whatever package\u00a0style,\u00a0multiple\u00a0ALEDS are wired together in series like so:<\/p>\n<div id=\"attachment_286\" style=\"width: 516px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/ws2812-wiring-schmatic.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-286\" class=\"size-large wp-image-286\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/ws2812-wiring-schmatic-1024x474.jpg\" alt=\"Schematic of WS2812 LEDS Wired in Series\" width=\"506\" height=\"234\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/ws2812-wiring-schmatic-1024x474.jpg 1024w, https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/ws2812-wiring-schmatic-300x139.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/ws2812-wiring-schmatic.jpg 1227w\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/a><p id=\"caption-attachment-286\" class=\"wp-caption-text\">Schematic of WS2812 LEDS Wired in Series<\/p><\/div>\n<p>Only three connections are required to power and control the entire chain: VDD (+5v), GND and DI (Data In). Each ALED connects to VDD and GND, and has\u00a0DI to receive data plus DO (Data Out) to send to the next unit (if any).<\/p>\n<p><strong>LED Strips<\/strong><\/p>\n<p>A particularly useful implementation of SMD style ALEDs is LED strips &#8211; available on reels in lengths of 1 to 5 meters. LED densities vary from 32 to 144 LEDS\/meter (the higher the density, the smaller the LEDs themselves). I find the 60\/meter type most broadly\u00a0useful and easily obtained for about\u00a0<a href=\"https:\/\/www.sparkfun.com\/products\/12025\" target=\"_blank\">$20\/meter\u00a0from Sparkfun in\u00a0 5 meter roll<\/a> or a little less through <a href=\"http:\/\/amzn.to\/1KxGSwU\" target=\"_blank\">Amazon though only in 1 meter lengths so far<\/a>, or Chinese export sources, though specifications for those can be all over the map.<\/p>\n<div id=\"attachment_288\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/addressable-RGB-LED-Strip.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-288\" class=\"wp-image-288 size-full\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/addressable-RGB-LED-Strip.jpg\" alt=\"addressable-RGB-LED-Strip\" width=\"600\" height=\"244\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/addressable-RGB-LED-Strip.jpg 600w, https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/addressable-RGB-LED-Strip-300x122.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-288\" class=\"wp-caption-text\">5 meter Strip of Addressable LEDS<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>To control the entire strip, you connect to the first LED. The strip from Sparkfun comes with\u00a0wires already soldered on to the first LED\u2014here I&#8217;ve already removed and deployed the beginning section of a 5 meter reel.<\/p>\n<p>You see, what makes these really special is you can cut them apart to use singly or in custom strips. Cut in the middle of the pads and you get this:<\/p>\n<div id=\"attachment_289\" style=\"width: 518px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/WS2812-LED.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-289\" class=\"wp-image-289 size-full\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/WS2812-LED.jpg\" alt=\"WS2812 LED\" width=\"508\" height=\"303\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/WS2812-LED.jpg 508w, https:\/\/thenscaler.com\/wp-content\/uploads\/2015\/02\/WS2812-LED-300x179.jpg 300w\" sizes=\"auto, (max-width: 508px) 100vw, 508px\" \/><\/a><p id=\"caption-attachment-289\" class=\"wp-caption-text\">A single LED assembly cut from a strip.<\/p><\/div>\n<p>The soldering pads are small, but doable if\u00a0you have a sharp point on your soldering iron; definitely easier than trying to solder an SMD chip itself. The\u00a0LED unit seems well enough protected from\u00a0heat when soldering to the pads\u00a0so long as you don&#8217;t linger with the iron too long.\u00a0To wire the data correctly, follow the arrow indicating data flow direction. Otherwise, the\u00a0+5V and GND pads are rails that can be fed from either side.<\/p>\n<p><strong>Software and Usage Guide<\/strong><\/p>\n<p>Adafruit Industries calls these little devices &#8220;NeoPixels&#8221; and has developed a great <a title=\"Adafruit Industries\" href=\"https:\/\/learn.adafruit.com\/adafruit-neopixel-uberguide\">Arduino library and usage guide<\/a>. <a title=\"Adafruit NeoPixel Library\" href=\"https:\/\/github.com\/adafruit\/Adafruit_NeoPixel\" target=\"_blank\">Download the Adafruit NeoPixel library package<\/a>. They also have a broad array of <a title=\"Adafruit NeoPixel Products\" href=\"http:\/\/www.adafruit.com\/category\/168\" target=\"_blank\">NeoPixel products<\/a> to work with, so I recommend you spend some quality time perusing their site. Sparkfun has two things Adafruit does not have &#8212; <a href=\"https:\/\/www.sparkfun.com\/products\/12999\" target=\"_blank\">clear lens PTH <\/a>types and <a href=\"https:\/\/www.sparkfun.com\/products\/12026\" target=\"_blank\">bare 60\/meter LED strips <\/a>(no silicon protective jacket).<\/p>\n<p>The WS2812 chip is sensitive to power anomalies, so there are some important rules to follow when using them:<\/p>\n<p>1. Connect a large capacitor\u2014I use 1000\u00b5f caps\u2014across +5V and GND near the first LED. This helps buffer power surges.\u00a0 I use a regulated power source, but use the CAPS too just to be sure.<\/p>\n<p>2. Place a resistor in the 300-500\u03a9 range on the data line, near the first LED.\u00a0 I use a 1\/4 watt 470\u03a9 resistor.<\/p>\n<p>3. Avoid connecting to live power; connect ground first if you must.<\/p>\n<p>4. Avoid connecting the data line to a power (or signal) source before you&#8217;ve connected and activated +5V power.\u00a0 I&#8217;ve broken this rule accidentally to no ill effect; but it makes sense so I make sure power is on before any data is sent. If the same power supply powers both the ALEDS and your Arduino, you shouldn&#8217;t have a problem.<\/p>\n<p><strong>Sketch Basics<\/strong><\/p>\n<p>The Adafruit library comes with demo programs to get started with.\u00a0 To use the ALEDS your sketch has to first include the Adafruit library:<\/p>\n<pre style=\"padding-left: 30px;\">#include &lt;Adafruit_NeoPixel.h&gt;<\/pre>\n<p>Then you create the global strip object you&#8217;ll use to control the strip.<\/p>\n<pre style=\"padding-left: 30px;\">Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMBER_OF_PIXELS, PIN, NEO_GRB + NEO_KHZ800);<\/pre>\n<p>The strip is initialized (with all pixels turned off0 in setup() this way:<\/p>\n<pre style=\"padding-left: 30px;\">strip.begin();\r\nstrip.show();<\/pre>\n<p>Color values are unsigned 32-bit values derived from RGB values supplied to the Color() method of the strip object:<\/p>\n<pre>uint32_t colorvalue = strip.Color( R, G, B);<\/pre>\n<p>where R, G &amp; B are values between 0 and 255, resulting in over 16 million possible colors. You can <a href=\"https:\/\/en.wikipedia.org\/wiki\/RGB_color_model\" target=\"_blank\">find out more about RGB color here<\/a>.<\/p>\n<p>In loop() you set pixel colors this way:<\/p>\n<pre style=\"padding-left: 30px;\">strip.setPixelColor(PIXEL_NUMBER, colorvalue);<\/pre>\n<p>where N is\u00a0 the number of LEDs in the strip, and PIXEL_NUMBER is a number between 0 and N &#8211; 1. Then call<\/p>\n<pre style=\"padding-left: 30px;\">strip.show();<\/pre>\n<p>to refresh strip and show the pixels when you are ready. Since this is a shift register device, data is sent serially, but the show cycle is parallel &#8211; all pixels change at the same moment.<\/p>\n<p>To scale the brightness of the strip as a whole, you use<\/p>\n<pre style=\"padding-left: 30px;\">strip.setBrightness(X);\r\nstrip.show();<\/pre>\n<p>where X is a number between 0 (strip off) and 255 (full brightness).<\/p>\n<p>That&#8217;s all there is to it. To see it in action, here is a video of a couple of light bars\u00a0 I&#8217;ve fabricated, running a little demo program that borrows a cool color wheel function from the\u00a0<a title=\"Adafruit Industries\" href=\"https:\/\/www.adafruit.com\/\" target=\"_blank\">Adafruit <\/a>NeoPixel &#8220;strandtest.ino&#8217; demo program.<\/p>\n<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" title=\"Addressable LEDS - a tonal demonstration\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/n1pNCTrvU4Y?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Layout Usage Ideas<\/strong><\/p>\n<p>Being able to control all the LEDS on a control panel through a single data line is a great place to start. Multiple colors give you more ways to convey information through panel LEDS. It wouldn&#8217;t occur to me now to use any other type of LED in an Arduino-based control panel.<\/p>\n<p>The smallest PTH type available appears to be 5mm, while great for control panels these will be about 24 scale inches in diameter in N Scale &#8211; a little large for most common direct lighting purposes; though they would work very well as a light source for fiber optics.<\/p>\n<p>On the other hand, in N Scale, they are great for indirect lighting applications, such as providing interior illumination and lighting effects. For hidden light sources I favor the SMD style cut from a strip. Easy to glue in place and keep hidden.<\/p>\n<p>I came across these when I was searching for a way to <a title=\"Introducing the Lassen &amp; North Coast Railroad\" href=\"https:\/\/thenscaler.com\/?p=254\">light my layout in its tight quarters<\/a>.\u00a0 The light bars in the demo are the first two\u00a0 I made to try out my idea. As you can see from the demonstration, they are capable of producing a lot light; with enough bars there will be more than enough to light my layout and provide fun lighting effects.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LEDs are the model railroader&#8217;s best friend.\u00a0 Low power consumption and a wide variety of colors, sizes\u00a0and styles make them the go-to lighting source for modeling in the 21st century. Even when you are looking for a distinctly &#8220;incandescent&#8221; look, LEDs can do the\u00a0 job better than a miniature incandescent bulb. In addition to single [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":157,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-279","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/pages\/279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=279"}],"version-history":[{"count":17,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/pages\/279\/revisions"}],"predecessor-version":[{"id":499,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/pages\/279\/revisions\/499"}],"up":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/pages\/157"}],"wp:attachment":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}