Build an RGB Matrix using some WS2812 modules

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

OVERVIEW

When you want to connect a lot of LEDs to a microcontroller like an Arduino UNO, using WS2812 is a great way to go. 

The WS2812 and the WS2812B consist of  a 5050 LED and a WS2811 IC into one small package.

The integrated WS2811 IC makes it possible to control each LED individually using only 1 pin.

The WS2812 and the newer WS2812B package version are essentially then same except that the B version uses 4 pins instead of the WS2812 6 pins to connect to a PCB and has bigger traces to be able to handle higher currents to run safely on a PCB.

For the purpose of this tutorial, we are using the RGB sticks which have 8 WS2812 LEDs on them, but you could use any WS2812(B) modules you want.

We will use the great FastLED library, which was written specifically to control these modules and is optimized for speed using a regular Arduino UNO.



SCHEMATIC

Arduino RGB Matrix using WS2812 module

The great thing about these WS2812(B) modules is that we only need one digital pin on the UNO to control all of them.

 

Pin 2 is connected to the DIN (Digital In) of the first module.

VCC and GND are connected to the breadboard.

The DOUT, GND and VCC of the modules are connected to DIN, GND and VCC of the next module.

 

The output leg of the Potentiometer (10K) is connected to A1 (analog pin 1).

The VCC and GND of the UNO are connected to the breadboard.

 

*note: A good practice in general for WS2812 based products to prevent damage is adding a capacitor of 10μF or higher between the ground and VCC and a resistor of 100Ω to 1KΩ between the microcontroller and the signal pin is recommended.  

If your are using an external power supply to power the LEDs you should use a capacitor and a resistor.  

In this tutorial we are driving the LEDs directly from the UNO because the power requirement is not very high, so we decided not to use those, but if you want to be extra careful you can connect those if you want.



THE CODE

In this tutorial we will do two things:

 

First we will use the Potentiometer to light up the LEDs one by one, and change the color has more of them are being lit.

Next we will read the color value of each LEDs from arrays and make a simple animation.

 

Don’t forget to check out our Tutorial video for more information.

 

/* Start of Code */

#include "FastLED.h"

// How many leds are connected?
#define NUM_LEDS 32

// Define the Pins
#define DATA_PIN 2

// Define the array of leds
CRGB leds[NUM_LEDS];

// Put color values in arrays
long invader1a[] =
{
0x008000, 0x000000, 0x000000,0x000000,0x000000,0x000000,0x000000, 0x008000,
0x008000, 0xFFFF00, 0x0000FF, 0xFFFF00, 0xFFFF00, 0x0000FF, 0xFFFF00, 0x008000,
0x008000, 0x000000, 0xFFFF00, 0x800080, 0x800080, 0xFFFF00, 0x000000, 0x008000,
0x000000, 0x000000, 0x000000, 0xFF0000, 0xFF0000, 0x000000, 0x000000, 0x000000
};

long invader1b[] =
{
0x000000, 0x000000, 0x0000FF, 0xFFFF00, 0xFFFF00, 0x0000FF, 0x000000, 0x000000,
0x000000, 0x008000, 0xFFFF00, 0x800080, 0x800080, 0xFFFF00, 0x008000, 0x000000,
0x008000, 0x000000, 0x000000, 0xFFFF00, 0xFFFF00, 0x000000, 0x000000, 0x008000,
0x000000, 0x008000, 0x000000, 0xFF0000, 0xFF0000, 0x000000, 0x008000, 0x000000
};


void setup() { 
FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS);
}

void loop() { 

int val = analogRead(1);
if (val < 1000) {

// Map the pot values to 0 - Number of Leds
int numLedsToLight = map(val, 0, 950, 0, NUM_LEDS);

// Clear the existing led values
FastLED.clear();

// Change led colors
for(int led = 0; led < numLedsToLight; led++) { 
if(led < 12)leds[led] = CRGB::Green;
if(led >=12 & led < 24)leds[led] = CRGB::Orange;
if(led >=24)leds[led] = CRGB::Red;
}

FastLED.setBrightness(50);
FastLED.show();
}
else {

// Loop for the Matrix example

FastLED.clear();
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = invader1a[i];
}
FastLED.setBrightness(50);
FastLED.show();
delay(500);

for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = invader1b[i];
}

FastLED.setBrightness(50);
FastLED.show();
delay(500);

}
}

/* End of Code */


TUTORIAL VIDEO



DOWNLOAD

Copy the above Sketch code in your Arduino IDE software to program your Arduino.

 

You can download the FastLED library here and extract it to your Arduino IDE Library folder.

For more information on the FastLED library, go to their website here.

3 Comments

Maurice - Feb 01, 2017

When I copy and compile the code I get:Arduino: 1.6.7 (Windows 10), Board: “Arduino/Genuino Uno”

WARNING: Spurious .github folder in ‘Adafruit NeoPixel’ library
WARNING: Spurious .github folder in ‘DHT sensor library’ library
In file included from C:\Users\mcraf\OneDrive\Documents\Arduino\brainyled\brainyled.ino:1:0:

C:\Program Files (x86)\Arduino\libraries\FastLED-3.1.3/FastLED.h:17:21: note: #pragma message: FastLED version 3.001.003

pragma message “FastLED version 3.001.003” ^

C:\Users\mcraf\OneDrive\Documents\Arduino\brainyled\brainyled.ino: In function ‘void setup()’:

brainyled:31: error: missing template arguments before ‘,’ token

FastLED.addLeds(NEOPIXEL, DATA_PIN)(leds, NUM_LEDS); ^

C:\Users\mcraf\OneDrive\Documents\Arduino\brainyled\brainyled.ino: In function ‘void loop()’:

C:\Users\mcraf\OneDrive\Documents\Arduino\brainyled\brainyled.ino:48:15: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]

if (led >= 12 & led < 24)leds[led] = CRGB::Orange; ^

exit status 1
missing template arguments before ‘,’ token

This report would have more information with “Show verbose output during compilation” enabled in File > Preferences.
Brainy-Bits - Feb 01, 2017

Maurice:
There was an error in the code on the tutorial page.

I have refreshed the code on this page, so go ahead and copy paste the code again it should work now!

Thanks

Melissa - Feb 01, 2017

Hi thanks for your tutorial=) I found your post and it is nearly what I am looking for. :D:D

My question, can I put in 3 potentiometer knobs that each have their own animation. Animation ON animation OFF. So I dont have to scroll through 1-3 animations to get to what I want? And if having potetiometer knobs will “confuse” the code if they are turned all on at the same time by mistake, maybe another knob would be better and just a fade in fade out on the animation? Trying to get around this problem in my head but I’m so new to it all that it is very confusing understanding to code and wire such a thing. Hope you have a tiny tip for me on my way :)
Melissa

Got Questions? Click here and post them in the Brainy-Bits Forum to get help!

Leave a comment

Comments have to be approved before showing up