Once you have the strip wired up, it is easy to control the color of the strip by using PWM output, for Arduino you can use analogWrite() on pins 3, 5, 6, 9, 10 or 11 (for classic Arduinos using the Atmega328 or 168). An analogWrite(pin, 0) will turn that LED off, analogWrite(pin, 127) will turn it on half-way and analogWrite(pin, 255) will turn it on full blast. Here is some example code that performs a simple color-swirl.

If you want to use other pins, check out this page on analogWrite() to know which models support analogWrite() on which pins
// color swirl! connect an RGB LED to the PWM pins as indicated
// in the #defines
// public domain, enjoy!
 
#define REDPIN 5
#define GREENPIN 6
#define BLUEPIN 3
 
#define FADESPEED 5     // make this higher to slow down
 
void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}
 
 
void loop() {
  int r, g, b;
 
  // fade from blue to violet
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from violet to red
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from red to yellow
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
  // fade from yellow to green
  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  // fade from green to teal
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
}

This guide was first published on Nov 26, 2012. It was last updated on Mar 14, 2024.

This page (Arduino Code) was last updated on Nov 26, 2012.

Text editor powered by tinymce.