Arduino WS2812B LED Strip connection and Code
Arduino WS2812B LED Strip connection and Code
MENU
ADVANCE ELECTRONICS
Table of Contents
1. Arduino WS2812B LED Strip:
2. Features & Specifications of DC5V WS2812B 5 Meter 60 Addressable LED Strip:
3. Amazon Links:
4. WS2812B LED Strip Connection with Arduino:
5. Arduino WS2812B LED Strip Code:
6. Arduino WS2812B LED Strip Calibration Complete code:
7. Arduino WS2812B LED Strip Code for only red light:
8. Arduino WS2812B LED Strip Code for flashing light:
9. Arduino WS2812B LED Strip Complete code:
10. Applications of ws2812b Strip LEDs:
Arduino WS2812B LED Strip connection and Code- in this tutorial, we are going to be looking
at the WS2812B Addressable LED Strip, we will go through all the specs, and I will also explain
how to use it with the Arduino. DC 5V WS2812B 5 Meter, 60 Addressable LED Strip is the
coolest type of LED strips. With the WS2812B Addressable LED Strip, you can control the
brightness and the color of each LED individually, which allows you to produce amazing and
complex effects in a simple way. This Addressable LED Strip is made by WS2812B LEDs wired
in series. These Addressable LED Strips have an IC built right into the LED. This allows
communication via a one-wire interface. This means that you can control lots of LEDs using just
one digital pin of your Arduino. This kind of strips are very flexible and can be cut to any length
you want. As you can see, the strip is divided into segments, and each segment contains one RGB
LED.
Input voltage: DC 5V
LED resource: WS2812B LED (SMD 5050 RGB LED with the built-in improved version
of WS2811 IC)
Power: 60LEDS/M—-18watt/M
DC5V, 0.3watt/LED(Max)
Amazon Links:
12v Adaptor:
Arduino Uno
Arduino Nano
330-ohm resistor:
Digital Oscilloscopes
Variable Supply
Digital Multimeter
*Please Note: These are affiliate links. I may make a commission if you buy the components
through these links. I would appreciate your support in this way!
Now to build the circuit we plug in the power rails on the breadboard from the Arduino ground and
5 volt and then we plug the capacitor with a positive end on the 5 volt supply through the power
rails and the negative end with the ground. We take digital pin number 2 of the Arduino and set it
up through a 330 ohm resistor and then we take the jumper wires from the resistor and connect it to
the middle of the led strip which is Din. Now connect the 5V from the bread board to the 5V of the
led strip and connect the ground wire from the bread board to the ground of the led strip. You can
follow the same exact connections if you plan to use Arduino Uno.
So now that we have the circuit completed let’s jump into the code.
First thing you want to do is to download the FastLED library, open your library manager in the
Arduino IDE.
Once we get the library installed you want to include the FastLED header
Then we want to define the LED data pin which for me it was pin 2 but you can use any pin you
would like as long as you set up the circuit that way then we want to define the number of LEDs
we use my LED strip came with 60 but in this tutorial we are only going to be using 12 of them.
1 #include <FastLED.h>
2 #define LED_PIN 2
3 #define NUM_LEDS 12
Now let’s create an array of type CRGB and the length of that array will be the number of LEDs
you have
1 CRGB leds[NUM_LEDS];
Now let’s go to the setup function and set up the LEDs we want to use the FastLED add LEDs
method and inside angular brackets we use the type of LEDs. In our case we have ws2812 these
are control on the LED pin using the RGB calibration then in parentheses we say that those LEDs
will use the array we declared earlier and that there are 12 LEDs present which is NUM LEDs.
Next we want to set up a power fail safe by limiting the max power in volts and milliamps to 5 volt
and 500 milliamps this just makes sure that the Arduino is not drawing too much current.
1 FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
Then we want to clear the LEDs every time we upload a new code by using FastLED.clear() and
FastLED.show().
1 FastLED.clear();
2
3 FastLED.show();
So let’s calibrate the LEDs normally the order when you write the LEDs is first is red, second is
green, and third is blue. So, to actually write a single LED we say the LED array at the zeroth
element is CRGB the color of to 255 which should correspond to red. We are going to say the
second LED will correspond to green only and the third LED will correspond to blue only.
Now we say that FastLED.show() actually show the results on the LEDs and we see that our order
is a little bit off instead of having red first we have green first and to fix this we come up into the
setup function and say that instead of RGB the order is green red and blue.
Now you justhave two changes to whatever result you get from running the code below since my
order is green red blue I change the calibration and whenever I run this code the proper values are
spitting out to the LEDs.
1 #include <FastLED.h>
2 #define LED_PIN 2
3 #define NUM_LEDS 12
4
5 CRGB leds[NUM_LEDS];
6
7 void setup() {
8 FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
9 FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
10 FastLED.clear();
11 FastLED.show();
12
13 }
14
15 void loop() {
16 // RED Green Blue
17 for (int i=0; i<NUM_LEDS; i++ )
18 {
19 leds[i] = CRGB(0, 255-2*i, 20*i );
20 FastLED.setBrightness(6*i);
21 FastLED.show();
22 delay (50);
23 }
24 for (int i=NUM_LEDS; i> 0; i-- )
25 {
26 leds[i] = CRGB(20*i, 0, 255-20*i );
27 FastLED.setBrightness(60-2*i);
28 FastLED.show();
29 delay (50);
30
31 }
32 }
So now that we have the LEDs calibrated let’s turn all the lights on the strip red so that is going to
be using a for loop we are iterating through the entire array of LEDs starting at zero up until the
length of the array and we are going to say that for every LED we will change the RGB color to
red.
1 #include <FastLED.h>
2 #define LED_PIN 2
3 #define NUM_LEDS 12
4 CRGB leds[NUM_LEDS];
5 void setup() {
6 FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
7 FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
8 FastLED.clear();
9 FastLED.show();
10
11 }
12
13 void loop() {
14 // RED Green Blue
15 for (int i=0; i<NUM_LEDS; i++ )
16 leds[i] = CRGB(255, 0, 0 );
17 FastLED.show();
18 }
When we upload the code to the Arduino we could see that all the lights turned red
We can change this value around so if we want all the values to be green we change the second
value to 255 and the value should turn to green
if we want to do a mix of colors to produce let’s say purple we can change the red value to 255 and
the blue value to 255.
You can see we’re generally getting purple so let’s say we want to keep this purple colour and
make the lights flash.
So to add a flash we use the method FastLED.setBrightness() this is actually setting the level the
brightness of the lights and we want this to increment starting from zero and it increments based on
whatever iteration we are in the for loop.
1 FastLED.setBrightness(2*i);
So it’s getting brighter for every iteration we go through and to reverse the flashing effect we copy
and paste the for loop down and reverse it so we start at the number of LEDs which is 12 when we
decrease the value so the brightness starts high and it goes low as the loop progresses we add a
delay just so you can see the effect take place.
1 #include <FastLED.h>
2 #define LED_PIN 2
3 #define NUM_LEDS 12
4
5 CRGB leds[NUM_LEDS];
6
7
8 void setup() {
9 FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
10 FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
11 FastLED.clear();
12 FastLED.show();
13
14 }
15
16 void loop() {
17 // RED Green Blue
18 for (int i=0; i<NUM_LEDS; i++ )
19 {
20 leds[i] = CRGB(255, 0, 255 );
21 FastLED.setBrightness(2*i);
22 FastLED.show();
23 delay (20);
24 }
25 for (int i=NUM_LEDS; i> 0; i-- )
26 {
27 leds[i] = CRGB(255, 0, 255 );
28 FastLED.setBrightness(2*i);
29 FastLED.show();
30 delay (20);
31
32 }
33 }
When we upload the code we get this flashing effect but it’s pretty fast so let’s increase the delay
and upload the code we could see that the flash slows down a little bit.
You just had to tweak it to whatever speed you want. So our final task in this tutorial is going to be
to turn the lights from green to blue as we progress from left to right and we do that by decreasing
the green RGB value by 20 every iteration because after 12 iterations the green value will be close
to zero and then we say for the blue value we increase from zero by 20 every iteration because that
will end at 240 which is close to blue.
2 {
3 leds[i] = CRGB(0, 255-2*i, 20*i );
4 FastLED.setBrightness(6*i);
5 FastLED.show();
6 delay (50);
7 }
Now for the reverse for loop let’s start at red and transition to magenta. The red value at 255 and
gradually decrease it so the red value here will start really high and then gradually get lower
because it’s a reverse for loop that starts at 12 and goes all the way down to 1 then we want to
increase the blue value as we progress and it looks strange that we are subtracting a value to
increase it but that’s just because we start at a high number and we are subtracting it by less and
less as we go on.
#include <FastLED.h>
#define LED_PIN 2
#define NUM_LEDS 12
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
FastLED.clear();
FastLED.show();
void loop() {
// RED Green Blue
for (int i=0; i<NUM_LEDS; i++ )
{
leds[i] = CRGB(0, 255-2*i, 20*i );
FastLED.setBrightness(6*i);
FastLED.show();
delay (50);
}
for (int i=NUM_LEDS; i> 0; i-- )
{
leds[i] = CRGB(20*i, 0, 255-20*i );
FastLED.setBrightness(60-2*i);
FastLED.show();
delay (50);
}
}
When we upload the code you can see that we have changing colors with respect to time from left
to right. We transition from green to blue and from right to left we transition from red to magenta.
This tutorial is just the starting point for individually addressable LEDs you can do a lot more
with these you can tweak the delay values the brightness and the colors all to make your own
custom patterns.
led wall
Advertising board
Share this:
Like this:
Loading...
Toll Tax System using Arduino bionic arm 500W Ebike Brushless Arduino FM Radio
Arduino: Ultrasonic using flex sensor and Motor Controller wiring using TEA5767 and
Sensor with Servo micro servos explanation, RDA5807M FM Stereo
Motor Hoverboard Test Radio Modules
My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a
site engineer in an Electrical Construction Company. Currently, I am running my own YouTube
channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music *
Martial Arts * Photography * Travelling * Make Sketches and so on...
Leave a Reply
Search … Search
RECENT POSTS
November 8, 2021
ChinaPCBOne
RECENT COMMENTS
ARCHIVES
CATEGORIES
Articles (53)
ATtinny85 (1)
computer (6)
Electrical (36)
Electronics (31)
Engineering (8)
Industrial (5)
JavaScript (16)
Matlab (14)
PHP (6)
raspberry pi (45)
RC Plane (9)
RC Projects (6)
STM32 (5)
Telecommunication (11)
posts by email.
Email Address
Subscribe
Subscribe
Follow Us
Stay updated via social channels
About Us
Home Privacy Policy Disclaimer DMCA About Us Contact Us Sitemap Advertise With Us