top of page

Safari browser sometimes has issues displaying images...

I.e: *you have to click on the images to see them...

For a better browsing experience on Brainy-Bits

Please use Chrome, Edge or Firefox browser.

Writer's pictureBrainy-Bits

Build an RGB Matrix using some WS2812 modules



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.

 

PARTS USED

WS2812 RGB Stick


Arduino UNO


10K Potentiometer


These are Amazon affiliate links...

They don't cost you anything and it helps me keep the lights on

if you buy something on Amazon. Thank you!

 

CONNECTIONS




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.

2,240 views0 comments

Recent Posts

See All

Comments


All my content is and will always be Free.

If you feel that my Videos / Tutorials are helping, and you would like to contribute...

 You can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

bottom of page