← Back to Exercise 8

Step 2 — led-stribe

Open Wokwi project Tutorial
NeoPixel — Arduino UNO basic example 📋 Show
NeoPixel — Arduino UNO basic example
#include <Adafruit_NeoPixel.h> // Include NeoPixel library

// Create a strip with 8 LEDs on pin 6 (UNO)
Adafruit_NeoPixel strip(8, 6, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();                     // Initialize NeoPixel
  uint32_t color = strip.Color(255, 0, 0); // RGB color (red)
  strip.setPixelColor(0, color);     // Set first pixel to red
  strip.setBrightness(255);          // Max brightness (0..255)
  strip.show();                      // Send data to strip
}

void loop() {
  // No animation in this minimal example
}
for command — set first 4 LEDs 📋 Show
for command — example
#include <Adafruit_NeoPixel.h> // Include the NeoPixel library

// Create a NeoPixel strip named "nauha" with 8 LEDs on pin 6 (UNO)
Adafruit_NeoPixel nauha(8, 6, NEO_GRB + NEO_KHZ800);

void setup() {
  nauha.begin();                          // Initialize the strip
  uint32_t color = nauha.Color(255, 0, 0); // Build an RGB color: red
  nauha.setBrightness(255);               // Max brightness (range 0..255)

  // Light the first 4 LEDs (indices 0..3) using a for loop
  for (int i = 0; i <= 3; i++) {
    nauha.setPixelColor(i, color);        // Set pixel i to the chosen color
  }

  nauha.show();                           // Send the buffered colors to the strip
}

void loop() {
  // Nothing to do here — static example
}

Some ready-made effects

Open LED effects
Arduino UNO examples — 8 LEDs on pin 6 📋 Show
Static Red (all LEDs)
#include <Adafruit_NeoPixel.h>   // Include the NeoPixel library

#define PIN 6                      // Data pin connected to the LED strip
#define NUM_LEDS 8                 // Number of LEDs on the strip

// Create the strip instance: 6 LEDs, on pin 6, GRB pixel order, 800 kHz timing
Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();                   // Initialize the strip object
  strip.setBrightness(255);        // Global brightness (0..255)

  // Set all LEDs to red and send the data to the strip
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, strip.Color(255, 0, 0));
  }
  strip.show();
}

void loop() {
  // Intentionally empty: static color example
}
Blink (non-blocking)
Knight Rider (non-blocking)
#include <Adafruit_NeoPixel.h>   // NeoPixel control library

#define PIN 6                      // LED strip data pin
#define NUM_LEDS 8                 // Number of LEDs

Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

// Knight Rider animation state
int position = 0;                  // Current LED index
int direction = 1;                 // 1 = forward, -1 = backward
unsigned long lastStep = 0;        // Last animation step time
const unsigned long speedMs = 60;  // Step interval (smaller = faster)

// Helper: clear all pixels quickly
void clearAll() {
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, 0);
  }
}

void setup() {
  strip.begin();
  strip.setBrightness(255);
  strip.show();
}

void loop() {
  unsigned long now = millis();
  if (now - lastStep >= speedMs) {       // Time for the next frame
    lastStep = now;
    clearAll();

    // Main red dot at current position
    strip.setPixelColor(position, strip.Color(255, 0, 0));

    // Simple red trail on both sides (dimmed)
    if (position - 1 >= 0)       strip.setPixelColor(position - 1, strip.Color(80, 0, 0));
    if (position + 1 < NUM_LEDS) strip.setPixelColor(position + 1, strip.Color(80, 0, 0));

    strip.show();

    // Move the dot and bounce at the ends
    position += direction;
    if (position == 0 || position == NUM_LEDS - 1) {
      direction = -direction;
    }
  }
}
Random Color (non-blocking)
#include <Adafruit_NeoPixel.h>   // NeoPixel control library

#define PIN 6                      // LED strip data pin
#define NUM_LEDS 8                 // Number of LEDs

Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

// Periodically pick a new random color for all LEDs (non-blocking)
unsigned long lastChange = 0;            // Time of last color change
const unsigned long changeEveryMs = 200; // Change frequency in ms

// Helper: set all pixels
void setAll(uint32_t c) {
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, c);
  }
}

void setup() {
  strip.begin();
  strip.setBrightness(255);
  randomSeed(analogRead(A0));            // Seed RNG for different sequences
}

void loop() {
  unsigned long now = millis();
  if (now - lastChange >= changeEveryMs) { // Time for a new color?
    lastChange = now;
    uint8_t r = random(256);
    uint8_t g = random(256);
    uint8_t b = random(256);
    setAll(strip.Color(r, g, b));
    strip.show();
  }
}
Fade In/Out (non-blocking)
#include <Adafruit_NeoPixel.h>   // NeoPixel control library

#define PIN 6                      // LED strip data pin
#define NUM_LEDS 8                 // Number of LEDs

Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

// Smoothly fade brightness up and down (non-blocking)
unsigned long lastStep = 0;        // Last update timestamp
const unsigned long stepMs = 20;   // Time between brightness updates
int level = 0;                     // Current brightness level (0..255)
int dir = 1;                       // +1 increasing, -1 decreasing

void setAll(uint32_t c) {
  for (int i = 0; i < NUM_LEDS; i++) {
    strip.setPixelColor(i, c);
  }
}

void setup() {
  strip.begin();
  strip.setBrightness(255);
}

void loop() {
  unsigned long now = millis();
  if (now - lastStep >= stepMs) {  // Update on schedule
    lastStep = now;
    level += dir;                   // Step brightness
    if (level >= 255 || level <= 0) {
      dir = -dir;                   // Reverse at the ends
      level = constrain(level, 0, 255);
    }
    setAll(strip.Color(level, level, level));
    strip.show();
  }
}
Warm breathing — cozy white/yellow glow
#include <Adafruit_NeoPixel.h>   // NeoPixel control library

#define PIN 6                      // LED strip data pin
#define NUM_LEDS 8                 // Number of LEDs

Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

unsigned long lastStep = 0;
const unsigned long stepMs = 20;   // Smooth updates
int level = 0;                     // 0..255
int dir = 1;                       // Fade up/down

void setAll(uint8_t r, uint8_t g, uint8_t b) {
  for (int i = 0; i < NUM_LEDS; i++) strip.setPixelColor(i, strip.Color(r, g, b));
}

void setup() {
  strip.begin();
  strip.setBrightness(255);
}

void loop() {
  unsigned long now = millis();
  if (now - lastStep >= stepMs) {
    lastStep = now;
    level += dir;
    if (level >= 255 || level <= 0) { dir = -dir; level = constrain(level, 0, 255); }
    // Warm white: more red, some green, little blue
    setAll(level, level * 0.45, level * 0.05);
  }
  strip.show();
}
Rainbow (non-blocking)
#include <Adafruit_NeoPixel.h>   // NeoPixel control library

#define PIN 6                      // LED strip data pin
#define NUM_LEDS 8                 // Number of LEDs

Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

// Cycle a rainbow across the LEDs (non-blocking)
unsigned long lastStep = 0;        // Last hue shift timestamp
const unsigned long stepMs = 60;   // Time between hue shifts
uint16_t offset = 0;               // Moving offset through the color wheel

// Map a 0..255 position onto an RGB rainbow color
uint32_t wheel(byte pos) {
  pos = 255 - pos;
  if (pos < 85) {
    return strip.Color(255 - pos * 3, 0, pos * 3);
  }
  if (pos < 170) {
    pos -= 85;
    return strip.Color(0, pos * 3, 255 - pos * 3);
  }
  pos -= 170;
  return strip.Color(pos * 3, 255 - pos * 3, 0);
}

void setup() {
  strip.begin();
  strip.setBrightness(255);
}

void loop() {
  unsigned long now = millis();
  if (now - lastStep >= stepMs) {  // Time to advance the rainbow
    lastStep = now;
    for (int i = 0; i < NUM_LEDS; i++) {
      // Evenly space hues across the strip, then add a moving offset
      uint8_t hue = (i * (256 / NUM_LEDS) + offset) & 255;
      strip.setPixelColor(i, wheel(hue));
    }
    strip.show();
    offset++;                       // Slowly rotate the color wheel
  }
}