Introduction:
Welcome to my Led Strip tutorial. This tutorial uses the Neopixel library by Adafruit. It is part of the Platy++ project, developed with Chin Yee Lee and Neil Narayan. It was originally done in an Arduino Uno. Here is what you are going to need:
- 01 Arduino UNO Board
- 01 Breadboard
- 01 330Ω Resistor
- 04 Jump Wires
- 01 RGBW Addressable LED Strip WS2812B for Arduino
If you landed here at this tutorial, chances are that you got everything but the LED strip. Perhaps you got already got your LED strip and cannot control it as you should. Do not worry, I learned it the hard way, so you don’t have to.
If you do have the same LED Strip from this tutorial, the code and instructions will already match your needs. But if you don’t, IT IS REALLY IMPORTANT to set the right parameters for your LED strip in the Arduino IDE. We will get there, but first things first, how do you wire this project?
As you surely noticed if the picture and diagram, most LED strips come with the wiring for both directly in your Arduino, as well as in an alternative source of power. Please notice the importance of using a 330Ω resistor, instead of wiring the signal directly to the Arduino Pin. This workaround enables a cleaner signal transmission.
Here is a picture of my installation, to make things clearer:
Installing the NeoPixel Library and setting up your Led strip:
There are two ways of installing the NeoPixel library:
- In your Arduino IDE, go to Tools > Manage Libraries (or Ctrl+Shift+I):
Search for the Adafruit NeoPixel and install it:
2. Alternatively, you can go to https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-installation and click on the green banner that says Download Adafruit_Neopixel for Arduino.
- IMPORTANT: In this case, you have to extract the zip file and rename the folder. The zip file is named Adafruit_NeoPixel-master.zip, so it will create a folder named Adafruit_NeoPixel-master once extracted. Right click on the folder and select rename, and then remove the “-master” part of the name (The folder’s name should be just Adafruit_NeoPixel).
- Copy the entire folder inside the Arduino Libraries Folder (Normally, it is on C: > Program Files (or Program Files (x86), if you are using Windows 10) > Arduino > libraries.
- Restart the Arduino IDE before you use it.
OK, now that you got your library installed, let’s go over some initial setup IMPORTANT TIPS, before the real fun begins:
Make sure to learn which kind of LED you are using. Notice on the description of the strip we are using that this is an RGB White (or RGBW) LED, and it uses a WS2812B control. You can learn further about this by taking a look at the datasheet.
The reason why I mention it is because you are going to need to specify those parameters when you declare your variables. And inputting the correct data will make your installation work properly. Please read the comments on the code to ensure you got everything right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
/* * This tutorial is part of the course CPLN 571 - Sensing the City, at the University of Pennsylvania, taught by Dr. Allison Lassiter. It is also a part * of the final project of the class. It is based on the NeoPixel Library by AdaFruit. * * In order to run this tutorial, you will need to install the AdaFruit NeoPixel library. */ #include <Adafruit_NeoPixel.h> // include the Neopixel Library in this Sketch #define PIN 6 // This is the Arduino Pin controlling the LEDstrip. #define NUMPIXELS 60 // Here, you are informing how many LEDs you have on your strip. // You can also control only a part of the existing LEDs, if you wish. // This strip has 60 LEDs, so I am informing this number. /* * Remember that I keep annoying you whith capital IMPORTANT warnings? This next line is where it really matters. */ Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800); // Here, you are specifying your strip, // Let's go over the details: /* * The command Adafruit_NeoPixel pixels assigns (inside the parenthesis, separated by commas (,)): * 1) the number of pixels you control, We have created the variable NUMPIXELS, so we can change this number more easily according to our need * 2) the PIN on the Arduino Board that sends the signal. In this case, we use PIN 06, as declared globally above * 3) The Type of LED flag. In this case, we have a RGB White LED, with 800Khz bitstream. You can check this out on the datasheet we linked above. * * Please notice that your LEDs won't work if you do not get this line right. So, here are some tips for more recent fixtures: * * NEO_KHZ800 will be common in most NeoPixel products with WS2812 LEDs. If you check the datasheet, of your strip, it should be under * data speed, or something similar. * NEO_KHZ400 will be preent in WS2811 LEDs. Again, check the datasheet to be sure. * * NEO_GRB will be common in NeoPixel products * NEO_RGB will be common in Flora Pixel products. * * REMEMBER, if yor LED is a RGB White, you have to add a W at the end of this code, so they will be either NEO_RGBW or NEO_GRBW */ /* * OK, now let's start controlling the leds. The strip I am using has 60 LEDs. I want to control it in 6 groups of 10 LEDs, to make my life easier. * So, let's define the following arrays: */ int PXL1[] = {0,1,2,3,4,5,6,7,8,9}; // array controlling the first 10 LEDs. Please notice the "LED 0" is the first one, not "LED 1" int PXL2[] = {10,11,12,13,14,15,16,17,18,19}; int PXL3[] = {20,21,22,23,24,25,26,27,28,29}; int PXL4[] = {30,31,32,33,34,35,36,37,38,39}; int PXL5[] = {40,41,42,43,44,45,46,47,48,49}; int PXL6[] = {50,51,52,53,54,55,56,57,58,59}; int delayval = 50; // Here we set a delaytime void setup() { pixels.begin(); // This initializes the NeoPixel library. } void loop() { for(int i=0;i<10;i++){ // Since each array has 10 LEDs, we are going to turn them sequentially on using this index. /* * Notice that the lines below are just setting up the color of each pixel. This is not yet the command to turn them on. The * pixels.setPixelColor command is a very easy way to define the color of each pixel. The syntax is: * pixels.setPixelColor(x, pixels.Color(R,G,B)), where: * x = the pixel you want to define a color for. In this example, we are using the arrays we created for the 6 control groups, hence the PXL1[i] input. * R,G,B = the values of red, green, and blue on a RGB scale. */ pixels.setPixelColor(PXL1[i], pixels.Color(139,0,139)); // array number 1 is magenta pixels.setPixelColor(PXL2[i], pixels.Color(255,255,0)); // array number 2 is yellow pixels.setPixelColor(PXL3[i], pixels.Color(255,255,255)); // array number 3 is white pixels.setPixelColor(PXL4[i], pixels.Color(0,255,0)); // array number 4 is green pixels.setPixelColor(PXL5[i], pixels.Color(0,0,255)); // array number 5 is blue pixels.setPixelColor(PXL6[i], pixels.Color(255,0,0)); // array number 6 is red pixels.show(); // Okay, we have informed which colors we want. Now, it is time to flip the switch and let the magic happen. The pixels.show() command does that delay(delayval); // Let's add a little delay here. So we can appreciate more the dynamic lighting we can do with this simple and cheap components. } for(int i=10;i>-1;i--){ // Now, we are going to turn them off sequentially, so we can create a pulsing dynamic for each group pixels.setPixelColor(PXL1[i], pixels.Color(0,0,0)); // The 0,0,0 values means that nothing is being turned on. So we repeat it for all groups. pixels.setPixelColor(PXL2[i], pixels.Color(0,0,0)); pixels.setPixelColor(PXL3[i], pixels.Color(0,0,0)); pixels.setPixelColor(PXL4[i], pixels.Color(0,0,0)); pixels.setPixelColor(PXL5[i], pixels.Color(0,0,0)); pixels.setPixelColor(PXL6[i], pixels.Color(0,0,0)); pixels.show(); // Again, we have only defined the colors above, remember we must instruct the Arduino to show what we came up with. delay(delayval); // Another delay, to make the presentation consistent. /* * You are done. Upload the code and see if you like it. */ } } |
And here you have the result:
Thanks for reading. I hope you have enjoyed going through this tutorial.
5 replies on “TUTORIAL: HOW TO COMMAND INDIVIDUAL LEDS WITHIN AN RGB LED STRIP USING NEOPIXEL LIBRARY”
Hello, I loaded the sketch,
but it does not work like it did on the video.
ws2811 connected 5 meters long.
Power supply 12 V 2A. what am I doing wrong ?
Greeting Lutz Germany
Hello Leonardo,
Thanks for your answer.
I’ll try it like you wrote me.
The power supply for the strip, I feed separately, only the data come from the Arduino.
Greeting Lutz
Hello Lutz,
It is difficult to determine what it can be without looking at everything. I was using a 1m LED strip. A 5m strip may be drawing too much power from the Arduino. Also:
– The sketch above is using a WS2812 LED Strip, thus NEO_GRBW + NEO_KHZ800. Check the datasheet of your strip to see if that is what you are supposed to use. I am almost sure you should input NEO_KHZ400. Check for the color scheme too (GRBW / GRB / RGBW / RGB). It should be in the datasheet. Adjust it in this line of the code:
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);
– Did you adjust the arrays for the number of pixels in your strip?
I’d try that first
Hi there great bit of code.
I am trying to adapt it, so i can turn on each array at a set distance using a distance sensor.
My question is:
How do i control just one array at a time?
Hi Patrick,
I am not really sure if that will help you, but if you take a look at response #5 from this post:
http://forum.arduino.cc/index.php?topic=191269.0
That might help you. I guess another option would be to have an array within another array (I searched for that initially). But I am not really a coder. This tutorial was a DIY effort for a course I took last semester.