WAVEGO-WS2812 LED
From Waveshare Wiki
WS2812 LED
Introduction
- This chapter mainly introduces some functions to control RGB_LED on the secondary development, through the study of this chapter, you can understand how to control the color of RGB_LED and the brightness output.
Principle
- RGB_LED color trichromatic principle: color mixing way as there are red, green, and blue three lamps when their light superimposed on each other, the colors are mixed, while the brightness is equal to the sum of the two brightness, the more mixed the higher the brightness.
- Colored light can be washed out and brightened by colorless light, but only the three primary colors mixed with the same brightness are the white light.
- Check out the official schematic below:
Install Dependency Libraries
- Installing the dependency library Adafruit_NeoPixel in the Arduino IDE:
Demo Code
Demo Analysis
Initialization
- Import library functions in the format of #include <library function>, library function refers to the encapsulated function, a series of commonly used functions encapsulated, easy to call; RGB_LED control with GPIO26, the device board contains two RGB lamp beads, the default for the control of GPIO26.
#include <Adafruit_NeoPixel.h> // Import Adafruit_NeoPixel #define BRIGHTNESS 255 // Defines the maximum brightness, value range 0 - 255 #define RGB_LED 26 // Defines the pins used to control the WS2812 RGB LEDs #define NUMPIXELS 2 // Control the number of lamp beads Adafruit_NeoPixel matrix = Adafruit_NeoPixel(NUMPIXELS, RGB_LED, NEO_GRB + NEO_KHZ800); // Instantiating the matrix
- Declare the function InitRGB, call the function setBrightness to set the maximum brightness of the LED, and change the value of BRIGHTNESS to adjust the maximum brightness of the lamp bead, the adjustment range is 0-255, call the function begins to initialize the matrix function, call the show function to transfer the pixel data in the RAM to the new pixel, i.e., to update the lamp bead color.
//Initialize RGB control void InitRGB(){ matrix.setBrightness(BRIGHTNESS); // Setting the maximum brightness matrix.begin(); //Begin matrix.show(); // Every time a setting change is made, it needs to be applied using this function. }
Lamp Bead Color Control
- Input three numbers for the color brightness of the lamp bead, R is red, G is green, B is blue, and the value is 0-255.
matrix.Color(R,G,B);
- Declare colorWipe (color brush) function, the role is to set the color for all lamp beads (NUMPIXELS value). Used to set color for lamp beads. for() loop usage: for(initialize variable; condition to judge true/false, execute loop if true, jump out of the loop if false; value to be changed before/after executing loop) {what to execute in loop}.
// Call this function to set all lights (number of NUMPIXELS set above) to a certain color. // Rinput, Ginput, and Binput are used to set the brightness of the red, green, and yellow color channels, the value range is 0-255 void colorWipe(byte Rinput, byte Ginput, byte Binput) { for(uint16_t i=0; i<matrix.numPixels(); i++) { matrix.setPixelColor(i, matrix.Color(Rinput, Ginput, Binput)); } matrix.show(); }
- Declares the setSingleLED function, which is called to set the color of a particular light.
// Call this function to set the color of one of the lights // LEDnum, the number of the lamp to be controlled, ranging from 0 to NUMPIXELS. // Rinput, Ginput, and Binput are used to set the brightness of the red, green, and yellow color channels, the value range is 0-255 void setSingleLED(uint16_t LEDnum, byte Rinput, byte Ginput, byte Binput){ matrix.setPixelColor(LEDnum, matrix.Color(Rinput, Ginput, Binput)); matrix.show(); }
Full Demo
#include <Adafruit_NeoPixel.h> // Import library function Adafruit_NeoPixel #define BRIGHTNESS 255 // Defines the maximum brightness, value range 0 - 255 #define RGB_LED 26 // Defines the pins used to control the WS2812 RGB LEDs #define NUMPIXELS 2 // Control the number of lamp beads Adafruit_NeoPixel matrix = Adafruit_NeoPixel(NUMPIXELS, RGB_LED, NEO_GRB + NEO_KHZ800); // Instantiating the matrix // Initialize RGB control void InitRGB(){ matrix.setBrightness(BRIGHTNESS); // Setting the maximum brightness matrix.begin(); // Begin matrix.show(); // Every time a setting change is made, it needs to be applied using this function. } // Call this function to set all lights (the number of NUMPIXELS set above) to a certain color // Rinput, Ginput, and Binput are used to set the brightness of the red, green and yellow color channels, the value range is 0-255 void colorWipe(byte Rinput, byte Ginput, byte Binput) { for(uint16_t i=0; i<matrix.numPixels(); i++) { matrix.setPixelColor(i, matrix.Color(Rinput, Ginput, Binput)); } matrix.show(); } // Call this function to set the color of one of the lights // LEDnum, the number of the lamp to be controlled, ranging from 0 to NUMPIXELS. // Rinput, Ginput, and Binput are used to set the brightness of the red, green and yellow color channels, the value range is 0-255 void setSingleLED(uint16_t LEDnum, byte Rinput, byte Ginput, byte Binput){ matrix.setPixelColor(LEDnum, matrix.Color(Rinput, Ginput, Binput)); matrix.show(); } void setup(){ InitRGB(); // LED light initialization } void loop(){ // Bright one-second white light (all three color channels at maximum brightness 255) colorWipe(255, 255, 255); delay(1000); // Out for one second (all three color channels at minimum brightness 0) colorWipe(0, 0, 0); delay(1000); // Number 0 lights up red for one second. setSingleLED(0, 255, 0, 0); delay(1000); setSingleLED(0, 0, 0, 0); // The light numbered 1 will light up blue for one second. setSingleLED(0, 0, 0, 255); delay(1000); setSingleLED(0, 0, 0, 0); }
Upload Demo
After copying and pasting the code, connect the device to the computer with USB, open RGB_LED.ino with Arduino IDE, select the development board and port, and click Upload the demo.
Demo Running Effect
The two lamp beads of the device light up white for one second and then go out for one second, followed by the No. 0 lamp which lights up red for one second and then goes out, and the No. 1 lamp which lights up blue for one second and then goes out.