Control WS2812 and NeoPixel with Arduino

FunduinoPublished on

Einzelne Pixel, Helligkeitsstufen und Farben am LED-Ring steuern

WS2812- und SK6812-LEDs, auch NeoPixel genannt, werden vor allem in LED-Streifen und großen Displays verwendet. Das Besondere daran ist, dass die einzelnen LEDs miteinander vernetzt sind. Jede LED lässt sich einzeln ansteuern, obwohl nur drei Kabel nötig sind: die Spannungsversorgung mit Plus und Minus sowie eine Datenleitung. Über diese Datenleitung steuerst du mit einem Mikrocontroller – in diesem Fall natürlich aus der Arduino-Reihe – alle LEDs an.

Eine gute Library zu den WS2812- und SK6812-LEDs gibt es von Adafruit. Dort werden die LEDs als „NeoPixel“ bezeichnet. Die Library kann alle Versionen der genannten LEDs ansteuern: sowohl die LEDs mit den drei Farben Rot, Grün und Blau (RGB) als auch die Versionen mit integrierter weißer LED (RGBW). Außerdem bietet Adafruit eine sehr umfangreiche Informationsseite und einige Sketche rund um das Thema NeoPixel an.

In unseren Anleitungen zu diesem Thema starten wir nicht sofort mit tollen Regenbogeneffekten, sondern mit der Ansteuerung einzelner Pixel in unterschiedlichen Helligkeitsstufen. Vielfarbige Regenbogeneffekte entstehen durch sogenannte Schleifen, die wiederum auf andere Schleifen für die Farbänderung zurückgreifen. Um diese Inhalte zu entzerren, beginnen wir möglichst unkompliziert.

Hinweis: Damit die Sketche in dieser Anleitung funktionieren, musst du vorab in der Arduino-Software die Library „Adafruit NeoPixel“ installieren. Du findest sie über die Funktion „Bibliothek einbinden“ in der Arduino-Software.

Round LED ring with WS2812 LEDs, also known as NeoPixels
LED ring with WS2812 LEDs
Square LED matrix with 8x8 WS2812 LEDs
LED matrix with 8x8 WS2812 LEDs
1 / 2

You need this

Master-LED Ring (WS2812, vergleichbar mit Neopixel) - F23108699 - Funduino WS2812 LED ring - RGB addressable, 5 V, 3 to 60 pixels — 40 Pixel / Yes 15,39 € *Item no. F23108699About the product — WS2812 LED ring - RGB addressable, 5 V, 3 to 60 pixels — 40 Pixel / Yes
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 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 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

The most important command in the following sketches is pixels.setPixelColor(x, pixels.Color(0,255,0)); It contains two pieces of information in parentheses.

The first is a number - shown here as “x” - and specifies the number of the LED that should light up. The second piece of information pixels.Color(0,255,0) contains both the color and the brightness. The parentheses contain three numbers, each separated by a comma: the first number stands for red, the second for green, and the third for blue.

You can choose values between 0 and 255 for the numbers. The smaller the number, the dimmer the corresponding color will shine. At a value of 0, the LED stays completely dark; at a value of 255, it shines at maximum brightness. If you activate two or three colors at the same time, they mix to create a new color - in the last example below, all three colored LEDs light up together, and the light appears white.

In our example, we use a ring with 40 WS2812 LEDs. However, you can follow this guide with any other WS2812 or NeoPixel module. The only important thing is to specify the total number of LEDs in the respective sketch - in the line #define NUMPIXELS 40. The number indicates how many LEDs are present.

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

Depending on whether your module has RGB LEDs or RGBW LEDs with an additional white LED, specify this in the line where you name the NeoPixel:

With RGBW LEDs, you can control all four LEDs in the following command. To do this, add a fourth number in the color parentheses:

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

How to wire the LED ring or LED matrix to the Arduino UNO:

  • DI (Digital Input) on the ring / matrix – pin D9 on the Arduino UNO
  • GND on the ring / matrix – GND on the Arduino UNO
  • VCC on the ring / matrix – 5V on the Arduino UNO
Circuit diagram: LED ring with DI connected to pin D9, GND to GND, and VCC to 5V on the Arduino UNO
NeoPixel ring on the Arduino UNO

Make an LED move around in a circle

In this sketch, an LED moves in a circle: The LEDs are activated in sequence while the previous LED is switched off. The variable “i” counts from 0 to 39 and then starts again at 0.

The result should look like the following image.

The numbers 39 and 40 in the loop belong to a ring with 40 LEDs. If you use a module with a different number of LEDs, you need to adjust these two numbers as well as NUMPIXELS.

LED ring with a single green LED
This is what the result looks like: a green LED moves 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

For this sketch, we’re using a ring with 40 WS2812 LEDs again. This time, five LEDs light up one after another, each slightly dimmer than the previous one - from 255 down to 150, 50, 10, and 1. The sketch then turns all five LEDs off and starts over.

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

WS2812 LEDs are also available in a wired form. These LEDs are programmed in exactly the same way as the flat versions. The only tricky part with this variant is the wiring, as you have to connect each LED to the power supply as well as to the data input and data output. A sketch labeling the connections is helpful.

In the setup below, we have mounted three wired WS2812 LEDs on a breadboard. Each LED is powered separately via GND and 5V. Connect the data input of the first LED to pin 6 on the Arduino UNO. Connect the data output of the first LED to the data input of the second LED, and the data output of the second LED to the data input of the third LED. Leave the data output of the third LED unconnected.

The following sketch lights up the three LEDs one after another in different colors.

Diagram of a through-hole WS2812 LED with labels for the four connections
Connections of a through-hole WS2812 LED
Wiring diagram with three through-hole WS2812 LEDs, data line daisy-chained from pin 6 of the Arduino UNO
Three wired WS2812 LEDs on a breadboard
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!

Weitere Beispiele für farbenfrohe Lichteffekte findest du in den Beispielen, die zusammen mit der NeoPixel-Library heruntergeladen werden.

Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.