{"id":451,"date":"2018-09-05T07:00:28","date_gmt":"2018-09-05T11:00:28","guid":{"rendered":"http:\/\/www.thesmarthomehookup.com\/test_install\/?p=451"},"modified":"2023-02-08T17:03:26","modified_gmt":"2023-02-08T22:03:26","slug":"beginners-guide-to-individually-addressable-rgb-led-strips","status":"publish","type":"post","link":"http:\/\/www.thesmarthomehookup.com\/test_install\/beginners-guide-to-individually-addressable-rgb-led-strips\/","title":{"rendered":"Beginners Guide to Individually Addressable RGB LED Strips"},"content":{"rendered":"<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/WS6FI_NyRzs\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"\"><\/iframe><\/p>\n<p><strong>Individually Addressable RGB LEDS<\/strong><\/p>\n<p>Today on the hookup we\u2019re going to learn about individually addressable RGB LEDS and how to control them with a microcontroller like the ESP8266 based nodeMCU.<\/p>\n<p>RGB LEDS are super cool, and fun, and flashy, and sometimes a bit tacky, but they can also actually be useful too.&nbsp; In this video I\u2019m going cover the basics of writing programs for individually addressable RGB LEDs, by programming a simple timer for my high school classroom.&nbsp; The goal for this timer will be to count down from any custom time and gradually change color from green, to yellow, to red as the timer gets closer to zero.&nbsp; I\u2019ll use this timer in my classroom to keep track of how much time it left in the period, but the concepts I\u2019m going to talk about are applicable to any individually addressable RGB LED project.<\/p>\n<p>RGB LEDS strips come in hundreds of shapes, sizes and configurations, but they can be broken into two major categories\u2026 3 Channel RGB strips, and Individually addressable RGB strips.&nbsp; A&nbsp; 3 channel strip has 3 different ground circuits and one common voltage, each different ground channel controls a different color of light (Red, Green, or Blue).&nbsp; By changing the resistance on those channels you can change the brightness of each channel, which allows you to produce a wide range of resulting colors.&nbsp; The major downside to these strips is that you can\u2019t pick and choose which LEDS are lit, the entire strip must be on or off, and every LED will be the same color, so you can\u2019t really do any cool effects with them.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/09\/microcontroller.jpg\"><img fetchpriority=\"high\" decoding=\"async\" src=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/09\/microcontroller-300x211.jpg\" alt=\"\" width=\"300\" height=\"211\"><\/a><\/p>\n<p>By contrast, individually addressable LEDS have a tiny microcontroller on each of the LEDs to allows each one to light up with a unique color and brightness.&nbsp; The strips have a positive voltage wire, a ground, and&nbsp; a data wire.&nbsp; Each time the data reaches an led it is read and passed down the strip to the next led. first chip reads the incoming address as LED0, and then performs the instructions for LED0, it also passes the data on to the next chip after increasing the counter value by 1.&nbsp; In other words, the first LED says \u201cOkay, I\u2019m LED0, the next guy who gets this message is LED1\u201d, this message continues down the strip until there are no more LEDS left.&nbsp; The important thing for you to know here is that each LED in the strip needs to be given specific instructions with their specific address.<\/p>\n<p>Individually addressable RGB LED strips come in 5V and 12V, and each strip has some unique advantages.&nbsp; 12V LED strips are less susceptible to voltage drop and are therefore able to travel longer longer distances before the LEDS become dim and the colors become inaccurate.&nbsp; If you are planning on running your strips any significant distance you\u2019ll probably want to inject power on each end of the strip at the very least and probably somewhere in the middle as well if you\u2019re using the 5V variety.&nbsp; The upside to using a 5V strip is that you\u2019ll be able to power your NodeMCU from the same power source without using a buck converter or some other voltage regulator.<\/p>\n<p><a href=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/09\/5vnodemcu.jpg\"><img decoding=\"async\" src=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/09\/5vnodemcu-300x232.jpg\" alt=\"\" width=\"300\" height=\"232\"><\/a><\/p>\n<p>For our project today, I\u2019m going to be using 5V WS2812Bs because I\u2019m not worried about voltage drop since this project is only going to be use around 60 LEDS.<\/p>\n<p>In the Arduino IDE we\u2019re going to use a very popular library for controlling these LEDS called FastLED.<\/p>\n<p>To get started with your project you need to set up your LED strip in your sketch.&nbsp; To do this you\u2019ll need to do three things.<\/p>\n<p>Minimal FastLED Sketch:&nbsp;<a href=\"https:\/\/github.com\/thehookup\/WS2812B_RGB_Classroom_Timer\/blob\/master\/FastLED_Minimal.ino\">https:\/\/github.com\/thehookup\/WS2812B_RGB_Classroom_Timer\/blob\/master\/FastLED_Minimal.ino<\/a><\/p>\n<pre data-enlighter-language=\"cpp\">\/\/NEEDED TO MAKE NODEMCU WORK \/\/\n#define FASTLED_INTERRUPT_RETRY_COUNT 0\n#define FASTLED_ESP8266_RAW_PIN_ORDER\n\/\/ LIBRARY SECTION \/\/\n#include &lt;span&gt;&amp;lt;&lt;\/span&gt;FastLED.h&lt;span&gt;&amp;gt;&lt;\/span&gt;\n\/\/LED LAYOUT AND SETUP \/\/\n#define NUM_LEDS 60\n\/\/DECLARATIONS \/\/\nCRGB leds[NUM_LEDS];\n\/\/GLOBAL VARIABLES \/\/\nconst int ledPin = 4; \/\/marked as D2 on the board\n\/\/SETUP FUNCTIONS \/\/\nvoid setup()\n{\n   Serial.begin(115200);\n   FastLED.addLeds&amp;lt;WS2812B, ledPin, RGB&amp;gt;(leds, NUM_LEDS);\n}\n\/\/ MAIN LOOP \/\/\nvoid loop()\n{\n   leds[11] = CHSV (96, 255, 192);\n   FastLED.show();\n}<\/pre>\n<p>First you\u2019ll need to define how many LEDs you\u2019re going to have in our strip.&nbsp; This refers to the number of controllable nodes, so if you bought the kind of LED strip that has one controller for every 3 LEDs you\u2019ll need to divide your total number of LEDs by 3.&nbsp; For our WS2812B leds, each led can be controlled individually, so my 57 LEDS needs to be declared as 57 controllable nodes.<\/p>\n<p>Second, you\u2019ll need to declare your LED matrix, to do this you\u2019ll type CRGB, then give a name to your led strip, and then tell it how many leds are in your array using the variable that you defined in step 1.<\/p>\n<p>Third, you\u2019ll need to set up our LED strip in the void setup by using the fastLED.addLeds function.&nbsp; You\u2019ll tell it the type of LED strip you\u2019re are using, the data pin they are connected to, the order in which the red, green and blue leds are addressed, the name of the led matrix, and the number of LEDS it has.<\/p>\n<p>Once you have your LED strip set up you can tell a specific LED to turn on at a specific color by calling the name of the led strip, then the LED you want to light in square brackets, EQUALS and then the color you want.<\/p>\n<p>When selecting a color you have a few different options.&nbsp; The first one is the classic RGB configuration in which you can turn on each of the red, green, and blue channels to 255 levels of brightness. Using this method you can generate around 16.8 million unique colors, but the downside is that you need to manipulate 3 variables to change the color.<\/p>\n<p><a href=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/09\/rainbow.jpg\"><img decoding=\"async\" src=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/09\/rainbow-300x104.jpg\" alt=\"\" width=\"300\" height=\"104\"><\/a><\/p>\n<p>Another option that allows us to manipulate color while only changing one variable is called HSV or Hue, Saturation and Value.&nbsp; Hue refers to the base color\u2026 0 is red and then all of the colors of the spectrum are represented as you go all the way back to red again at 255, saturation refers to the vibrance of the color.&nbsp; If you choose 0, there won\u2019t be any color, only white light and 255 will be the richest most saturated color.&nbsp; Last is the value, which is essentially the brightness of the LEDs.<\/p>\n<p>If it\u2019s your first time working with these strips I highly recommend you compile and upload your project after making a single LED light up as a custom color.&nbsp; I know the first time I got my strip working I was beyond excited and it really helped me push through some of the issues that I ran into later.<\/p>\n<p>For my specific project I\u2019m making a timer that transitions from green to yellow to red, so you can see that I need to go from a hue value of about 96 down to zero and I can keep my saturation and value numbers constant, which is nice because that means I only need to work with a single variable.<\/p>\n<p>The trigger for my timer is going to be an MQTT message. &nbsp;I always use the same code to receive my MQTT values and you can see that after receiving a new payload it converts the payload into both a string and an int in separate variables so I can decide which data type I want to use for each specific function.<\/p>\n<p>I use home assistant and node-red for my home automation, so I have an easy way to send an MQTT message. &nbsp;But if you don\u2019t already have an MQTT server setup you could use io.adafruit.com which offers a really nice web interface and IFTTT integration without the need to run a separate raspberry pi at home.<\/p>\n<p>Once a new MQTT message comes in, my code is going to do a few things.&nbsp; The first thing we\u2019ll need to do is reset our ledsRemaining to the total number of LEDS in our string, which will result in the bar filling completely.&nbsp; Then&nbsp; we need to calculate how often an LED needs to disappear.<\/p>\n<p>This calculation is pretty simple, we just take the total number of minutes for the timer, and divide it by the total number of LEDs in our strip which will give us the fraction of a minute that each LED represents.&nbsp; Since the simple timer library for Arduino wants an input of milliseconds we\u2019ll also need to multiply by 60,000 to convert from minutes to milliseconds.&nbsp; We\u2019ll use this number of seconds to tell our program how often it should subtract an LED from the ledsRemaining variable.&nbsp; In the simplest situation if we had 60 leds total in our strip and we set our timer for 1 minute each LED would last for only 1 second.<\/p>\n<p>To actually light up our LEDs we could type in each LED\u2019s specific address in the matrix and tell it which color to turn, but that would take forever and is way more work than we need to do.&nbsp; Instead of doing that, we\u2019re going to light them up using a for loop.<\/p>\n<p>Basically in a for loop you declare a variable, \u201ci\u201d in this case\u2026 then you give it a condition that has to be true for the loop to continue, which means this loop will continue as long as the variable \u201ci\u201d is less than my ledsRemaining variable\u2026 and last you tell it what to do every time the loop completes which in this specific for loop I have i++, which means it will add 1 to the value of \u201ci\u201d each time the loop finishes.<\/p>\n<p>You can see that each time the loop runs it will turn on a different LED based on the value of that variable \u201c.\u201d, if the ledsRemaining variable is equal to 25 it will run 25 times and turn on LEDs 0-24.&nbsp; If my ledsRemaining variable is equal to 8 it will run 8 times and turn on LEDs 0-7.<\/p>\n<p>The last piece of the puzzle is color changing.&nbsp; As I mentioned before I\u2019m going to manipulate the hue value of my HSV color.&nbsp; When the timer is full I want to be at a value of around 96 and when it\u2019s empty I want to be at a value of 0.&nbsp; But since I only have 57 LEDs in my strip I can\u2019t just use my LEDSremaining variable.&nbsp; Instead I\u2019m going to use the map function for Arduino.&nbsp; This compares two number ranges and results in the equivalent position on a number line for the two ranges.&nbsp; I\u2019ll be changing from a range of 0-57 to a range of 0-96 in order to adjust for my color.<\/p>\n<p>There\u2019s a few other little lines of code in there to allow me to cancel a previous timer before inputting a new one, but overall the code should be pretty easy for you to comb through and figure out what\u2019s going on and modify as needed.<\/p>\n<p>Here\u2019s what the timer looks like in action:&nbsp; We can set the timer to 1 minute, and then as the LEDs disappear we could send a different timer for 5 minutes that will overwrite the previous one and reset it to the maximum number of LEDs. &nbsp;And since no project IoT project would be complete without voice control we can say \u201cAlexa, set the classroom display to 10\u201d and it will start a 10 minute timer for us.<\/p>\n<p>I hope this video will be helpful for you in making your own RGB led projects.&nbsp; If you\u2019d like to build this exact same project I\u2019ve put links in the description for the specific products that I used.&nbsp; I\u2019d also like to hear in the comments if you prefer videos where I just build a project and leave the code in the description, or if you\u2019d rather see videos like this one where I explain how the code works.<\/p>\n<p>As we get closer to Halloween and Christmas I\u2019m going to be making some videos about my house LED projects.&nbsp; Ben from Bruh Automation already made a really good video about RGB leds that focuses on using pre-made animations, so I\u2019m going to look at different things like using separate led arrays for different parts of the house, and custom tailoring your LED arrays for the specific features of your house so you can have animations that are custom for your house only.<\/p>\n<p>Thank you again to my awesome patrons on patreon for your continued support of my channel and my projects.&nbsp; I only have these video ideas planned out around 2 weeks in advance, so if you have an idea for a unique video that you\u2019d love to see me make, let me know down in the comments and if it sounds exciting and it hasn\u2019t already been done well on youtube there\u2019s a good chance that I\u2019ll make it.&nbsp; If you\u2019d like to support my channel, check out the links in the description.&nbsp; If you enjoyed this video, and you\u2019d like to see more like it please consider subscribing, and as always, thanks for watching the hookup.<\/p>\n<pre><\/pre>\n<h4>US Amazon Links ####<\/h4>\n<p>16.4ft WS2812B LEDs: <a href=\"https:\/\/amzn.to\/2Q6UU4\">https:\/\/amzn.to\/2Q6UU4<\/a>u<br \/>\nESP8266 NodeMCU: <a href=\"https:\/\/amzn.to\/2nkeahq\">https:\/\/amzn.to\/2nkeahq<\/a><br \/>\nAluminum LED Channel: <a href=\"https:\/\/amzn.to\/2Q3aJce\">https:\/\/amzn.to\/2Q3aJce<\/a><br \/>\n5 Amp 5 Volt Power Supply: <a href=\"https:\/\/amzn.to\/2wIAVAd\">https:\/\/amzn.to\/2wIAVAd<\/a><\/p>\n<h4>#### Files ####<\/h4>\n<p>Wiring Schematic: <a href=\"https:\/\/github.com\/thehookup\/WS2812B_RGB_Classroom_Timer\/blob\/master\/5vnodemcu.jpg\">https:\/\/github.com\/thehookup\/WS2812B_RGB_Classroom_Timer\/blob\/master\/5vnodemcu.jpg<\/a><br \/>\nMinimal FastLED Arduino Sketch: <a href=\"https:\/\/github.com\/thehookup\/WS2812B_RGB_Classroom_Timer\/blob\/master\/FastLED_Minimal.ino\">https:\/\/github.com\/thehookup\/WS2812B_RGB_Classroom_Timer\/blob\/master\/FastLED_Minimal.ino<\/a><br \/>\nFull Timer Sketch: <a href=\"https:\/\/github.com\/thehookup\/WS2812B_RGB_Classroom_Timer\/blob\/master\/Classroom_Period_Timer_CONFIGURE.ino\">https:\/\/github.com\/thehookup\/WS2812B_RGB_Classroom_Timer\/blob\/master\/Classroom_Period_Timer_CONFIGURE.ino<\/a><\/p>\n<h4>#### Support my channel ####<\/h4>\n<p>Patreon: <a href=\"https:\/\/www.patreon.com\/thehookup\">https:\/\/www.patreon.com\/thehookup<\/a><br \/>\nVisit my website: <a href=\"http:\/\/www.thesmarthomehookup.com\/test_install\">http:\/\/www.thesmarthomehookup.com\/test_install<\/a><br \/>\nFollow me on Twitter: <a href=\"https:\/\/twitter.com\/thehookup1\">@TheHookUp1<\/a><\/p>\n<p>Music by www.BenSound.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Individually Addressable RGB LEDS Today on the hookup we\u2019re going to learn about individually addressable RGB LEDS and how to control them with a microcontroller like the ESP8266 based nodeMCU. RGB LEDS are super cool, and fun, and flashy, and sometimes a bit tacky, but they can also actually be useful too.&nbsp; In this video [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2596,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,5],"tags":[],"class_list":["post-451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-projects","category-tutorials"],"acf":[],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/comments?post=451"}],"version-history":[{"count":11,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/451\/revisions"}],"predecessor-version":[{"id":2351,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/451\/revisions\/2351"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media\/2596"}],"wp:attachment":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media?parent=451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/categories?post=451"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/tags?post=451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}