Example Code for Arduino-WS2812

Last revision 2026/01/13

This article guides users through setting up and programming WS2812 LEDs with Arduino, using the FastLED library, including hardware and software preparation, connection setup, and sample code for LED control.

Hardware Preparation

Software Preparation

Other Preparation Work

Connect the onboard WS2812 of M0 to P~8 of Arduino. We use open-source FastLED library, and set it to pin 8.

Sample Code

Set 1 WS2812 LED on pin8 to constant red with brightness 128.

#include "FastLED.h"            // FastLED library will be used in this example

#define NUM_LEDS 1            //Number of LED 
#define DATA_PIN 8              // Arduino Signal control output pin 
#define LED_TYPE WS2812         // Type of LED
#define COLOR_ORDER GRB         // The order of red, green, blue of RGB LEDs

uint8_t max_bright = 128;       // LED brightness control variable, available value 0 ~ 255. The larger the value, the brighter the LED strip.  

CRGB leds[NUM_LEDS];            // Create leds strip

void setup() { 
  LEDS.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);  // Init leds strip
  FastLED.setBrightness(max_bright);                            // Set leds strip brightness
}

void loop() { 
  leds[0] = CRGB::Red;          // Set the first LED of the LED strip to red, leds[0] is the first LED, leds[1] is the second LED 
  FastLED.show();                // Update LED color 
  delay(50);                     // Wait 500ms 
}

Result

The onboard WS2812 RGB LED (connected to pin 8) lights up red.

Was this article helpful?

TOP