Control WS2812 and NeoPixel with Arduino
FunduinoPublished on
Einzelne Pixel, Helligkeitsstufen und Farben am LED-Ring steuern
You need this
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Breadboard cables male-female - 40 pieces, 20 cm
1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm
All 3 parts together 28,25 € *Ready to ship immediately · Delivery time: 1-3 business days
* All prices include statutory VAT, plus shipping costs.
Understand the setPixelColor command
Color values for RGB LEDs
pixels.setPixelColor(x, pixels.Color(255,0,0)); // Red pixels.setPixelColor(x, pixels.Color(0,255,0)); // Green pixels.setPixelColor(x, pixels.Color(0,0,255)); // Blue pixels.setPixelColor(x, pixels.Color(255,255,0)); // Yellow pixels.setPixelColor(x, pixels.Color(0,255,255)); // Turquoise pixels.setPixelColor(x, pixels.Color(255,255,255)); // White
RGB LEDs or RGBW LEDs
Specify RGB or RGBW LEDs
// Verwendung von RGB-LEDs: Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); // Verwendung von RGBW-LEDs: Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRBW + NEO_KHZ800);
Color values for RGBW LEDs
pixels.setPixelColor(x, pixels.Color(255,0,0,0)); // Red pixels.setPixelColor(x, pixels.Color(0,255,0,0)); // Green pixels.setPixelColor(x, pixels.Color(0,0,255,0)); // Blue pixels.setPixelColor(x, pixels.Color(255,255,0,0)); // Yellow pixels.setPixelColor(x, pixels.Color(0,255,255,0)); // Turquoise pixels.setPixelColor(x, pixels.Color(255,255,255,0)); // White by mixing the three colored LEDs pixels.setPixelColor(x, pixels.Color(0,0,0,255)); // White by controlling the separate white LED directly
Wire the LED ring
Make an LED move around in a circle
Program code - An LED moves in a circle
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
int i = 0;
#define PIN 9 // Specifies which digital pin the WS2812 LEDs or NeoPixels are connected to
#define NUMPIXELS 40 // Specifies the number of connected WS2812 LEDs or NeoPixels
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int pause = 100; // 100 milliseconds pause before the next LED is controlled.
void setup()
{
pixels.begin(); // Initialize the NeoPixels
}
void loop()
{
pixels.setPixelColor(i, pixels.Color(0,150,0)); // Pixel lights up green
pixels.setPixelColor(i-1, pixels.Color(0,0,0)); // The previous pixel is turned off
pixels.show(); // Apply the pixel settings
if (i==0) pixels.setPixelColor(39, pixels.Color(0,0,0)); // For pixel "0", the previous pixel (39) must be turned off.
pixels.show(); // Apply the pixel settings
delay(pause);
i = i+1; // The variable "i" is incremented by one. The new value of "i" is then the next LED in the LED ring
if (i==40) i=0; // When the variable reaches 40, it is reset to "0", since the LEDs are numbered only from 0 to 39.
}Control multiple LEDs in sequence
Program code – Five LEDs in sequence at decreasing brightness
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 9 // Specifies which digital pin the WS2812 LEDs or NeoPixels are connected to
#define NUMPIXELS 40 // Specifies the number of connected WS2812 LEDs or NeoPixels
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int pause = 100; // 100 millisecond pause before activating the next LED.
void setup()
{
pixels.begin(); // Initialize the NeoPixels
}
void loop()
{
pixels.setPixelColor(0, pixels.Color(0,255,0)); // The first pixel (number 0) lights up green
pixels.show(); // Update the pixels
delay(pause); // Pause; nothing changes during this time.
pixels.setPixelColor(1, pixels.Color(0,150,0)); // The second pixel (number 1) lights up a little dimmer
pixels.show(); // Update the pixels
delay(pause); // Pause; nothing changes during this time.
pixels.setPixelColor(2, pixels.Color(0,50,0)); // The third pixel (number 2) lights up even dimmer
pixels.show(); // Update the pixels
delay(pause); // Pause; nothing changes during this time.
pixels.setPixelColor(3, pixels.Color(0,10,0)); // The fourth pixel (number 3) lights up only faintly
pixels.show(); // Update the pixels
delay(pause); // Pause; nothing changes during this time.
pixels.setPixelColor(4, pixels.Color(0,1,0)); // The fifth pixel (number 4) only glows faintly
pixels.show(); // Update the pixels
delay(pause); // Pause; nothing changes during this time.
// Reset all pixel colors to level "0" (off)
pixels.setPixelColor(0, pixels.Color(0,0,0));
pixels.setPixelColor(1, pixels.Color(0,0,0));
pixels.setPixelColor(2, pixels.Color(0,0,0));
pixels.setPixelColor(3, pixels.Color(0,0,0));
pixels.setPixelColor(4, pixels.Color(0,0,0));
pixels.show(); // Update the pixels
delay(pause); // Pause; the LEDs stay off during this time
}Wiring and controlling wired WS2812 LEDs
1 / 2
Program code – Three through-hole WS2812 LEDs
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define PIN 6 // Data line pin
#define NUMPIXELS 3 // Number of WS2812 LEDs or NeoPixels
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
pixels.begin(); // Initialize the LEDs
}
void loop() {
pixels.setPixelColor(0, pixels.Color(0, 150, 0));
pixels.show(); // Send the updated data to the WS2812 LEDs
delay(500);
pixels.clear(); // Turn off all LEDs
pixels.setPixelColor(1, pixels.Color(150, 0, 0));
pixels.show(); // Send the updated data to the WS2812 LEDs
delay(500);
pixels.clear(); // Turn off all LEDs
pixels.setPixelColor(2, pixels.Color(0, 0, 150));
pixels.show(); // Send the updated data to the WS2812 LEDs
delay(500);
pixels.clear(); // Turn off all LEDs
}Done!
Rate the instructions