The Arduino WS2812B LED strip allows for individual control of each LED’s brightness and color, providing a simple yet cool LED effect experience.
The WS2812B LED strip combines LEDs in a series, embedded with amplifying driver circuits, oscillators, and voltage programmable control chips, making it addressable and able to communicate through a single-wire interface. This means that users can control multiple LEDs simultaneously using a single digital pin on the Arduino board.
Features of the DC5V WS2812B 5-meter addressable LED strip are as follows:
Input Voltage: DC 5V
Power: 60LEDS/M -18watt/M
FPCB Width: 60LEDS/M—-10mm
Color: Full color RGB, dreamy flowing color
Length: Customizable
IC Type: Embedded
LED Type: SMD 5050
PCB Color: White
Protection: IP65 waterproof
First, connect the Arduino to the breadboard according to the circuit diagram, connect the resistor and capacitor, and then power it with 5V. The #2 digital pin of the Arduino is connected to the Din pin in the middle of the LED strip through a 330-ohm resistor and jumper wire.
Next, download the FastLED library and open it in the Arduino IDE.
Type FastLED and click install.
After successful installation, upload the FastLED header.
Then, set the LED data pin, which is 2 in this project. The number of LEDs in the LED strip can reach 60, but this project chooses 12.
#include
#define LED_PIN 2
#define NUM_LEDS 12
Now, create a CRGB array with the length equal to the number of LEDs used.
CRGB leds[NUM_LEDS];
Control the ws2812 through RGB verification, where NUM_LEDS is 12 in this case.
FastLED.addLeds(leds, NUM_LEDS);
Set the maximum voltage (5v) and maximum current (500mA) to prevent excessive current consumption by the Arduino.
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
Each time a new set of code is uploaded, refresh the LEDs using FastLED.clear() and FastLED.show().
FastLED.clear();
FastLED.show();
Calibrate the LEDs in the usual order: red – green – blue. If the actual displayed color order does not match what is written in the FastLED.show() function, modify the order of the function.
The code for calibrating the Arduino WS2812B LED strip is as follows:
#include
#define LED_PIN 2
#define NUM_LEDS 12
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
FastLED.clear();
FastLED.show();
}
void loop() {
// RED Green Blue
for (int i=0; i
{
leds = CRGB(0, 255-2*i, 20*i );
FastLED.setBrightness(6*i);
FastLED.show();
delay (50);
}
for (int i=NUM_LEDS; i> 0; i– )
{
leds = CRGB(20*i, 0, 255-20*i );
FastLED.setBrightness(60-2*i);
FastLED.show();
delay (50);
}
}
After calibration, all LEDs on the LED strip will display the function assignment, which is red.
The code for making all the LEDs on the Arduino WS2812B LED strip display red is as follows:
#include
#define LED_PIN 2
#define NUM_LEDS 12
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
FastLED.clear();
FastLED.show();
}
void loop() {
// RED Green Blue
for (int i=0; i
leds = CRGB(255, 0, 0 );
FastLED.show();
}
After uploading the code to the Arduino, all LEDs will display red.
If you change the second value in the function to 255, you can change the LED color to green:
leds = CRGB(0, 255, 0 )
If you want a mixed color effect, such as purple, set the function value to: leds = CRGB(255, 0, 255 ). After uploading the code, the strip will display purple.
If you want the strip to blink while maintaining purple, you need to use the FastLED.setBrightness() function to set the brightness level and gradually increase from 0.
The following is the blinking code for the Arduino WS2812B LED strip:
#include
#define LED_PIN 2
#define NUM_LEDS 12
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
FastLED.clear();
FastLED.show();
}
void loop() {
// RED Green Blue
for (int i=0; i
{
leds = CRGB(255, 0, 255 );
FastLED.setBrightness(2*i);
FastLED.show();
delay (20);
}
for (int i=NUM_LEDS; i> 0; i– )
{
leds = CRGB(255, 0, 255 );
FastLED.setBrightness(2*i);
FastLED.show();
delay (20);
}
}
After uploading the above code, you will achieve a blinking effect. However, this blinking is too fast, so you need to increase the delay to achieve the desired blinking effect.
Finally, let’s implement a gradient effect on the strip from left to right, transitioning from green to blue. Reduce the green RGB value by 20 each loop, and after 12 loops, this value will approach 0, turning the color blue.
for (int i=0; i
{
leds = CRGB(0, 255-2*i, 20*i );
FastLED.setBrightness(6*i);
FastLED.show();
delay (50);
}
Then start looping from red…
In this way, the Arduino WS2812B LED strip can start blinking according to our desired melody and effect, with the following code:
#include
#define LED_PIN 2
#define NUM_LEDS 12
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
FastLED.clear();
FastLED.show();
}
void loop() {
// RED Green Blue
for (int i=0; i
{
leds = CRGB(0, 255-2*i, 20*i );
FastLED.setBrightness(6*i);
FastLED.show();
delay (50);
}
for (int i=NUM_LEDS; i> 0; i– )
{
leds = CRGB(20*i, 0, 255-20*i );
FastLED.setBrightness(60-2*i);
FastLED.show();
delay (50);
}
}
Author: Hard City Allchips, Source: Breadboard Community
Link: https://mbb.eet-china.com/blog/uid-me-3975615.html
Copyright Notice: This article is original by the author, and reproduction is prohibited without permission!
Follow the Breadboard Community for daily selected electronic technology knowledge
▼

-
Disassembled a switching power supply, let’s see what’s in the circuit?
-
Step-by-step guide to analyzing over-voltage protection circuit design, did you learn the essence?
-
Can you really draw timing diagrams? No, you can’t!
-
Production of circuit board shielding cover drawings
-
DC-DC buck circuit has no output issue
-
Production of circuit board shielding cover drawings