Project showcase
Color Mixing Addressable LEDs

Color Mixing Addressable LEDs © GPL3+

This lesson aims to show how to control an addressable WS2812 LEDs strip in order to obtain various colors using 3 potentiometers.

  • 5,347 views
  • 1 comment
  • 7 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
WS2811 Addressable LED strip (1m, 60 LEDs)
×1
10 10K trimmer
×1

About this project

1. Download the library

In order to run this sketch on your Arduino board you have to download the Adafruit NeoPixel library.

2. How does it work?

The sketch is very simple: there are 3 potentiometers attached to three different analog inputs (A0, A1 and A2). Each one control the amount of color (Red Green and Blue respectively) in 1 meter of addressable WS2812 LEDs.

For sake of simplicity in the following layout only one LED is reported.

Attention: Please make sure to supply the strip using an external 5V power supply as reported in the layout!

3. Code

Here the code! Upload it and start to play with colors!

/*
Color addressable LEDs control using Adafruit NeoPixel library
 Parts required:
 1m strip of addressable LEDs http://store.arduino.cc/products/C000083
 Created 28 November 2014
 by Arturo Guadalupi <a.guadalupi@arduino.cc>
 This example code is part of the public domain
 */
#include <Adafruit_NeoPixel.h>
const int NUMPIXELS = 60; //number of LEDs in 1m
const int LEDsPin = 9;    // LEDs connected to digital pin 9
const int redPotPin = A0;      // pin to control red
const int greenPotPin = A1;   // pin to control green
const int bluePotPin = A2;   // pin to control blue
int redValue = 0; // value to write to the red LED
int greenValue = 0; // value to write to the green LED
int blueValue = 0; // value to write to the blue LED
int redPotValue = 0; // variable to hold the value from the red pot
int greenPotValue = 0; // variable to hold the value from the green pot
int bluePotValue = 0; // variable to hold the value from the blue pot
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // set the digital pin as output
  pinMode(LEDsPin, OUTPUT);
}
void loop() {
  // Read the pots first:
  // read the value from the red pot control:
  redPotValue = analogRead(redPotPin);
  // give the ADC a moment to settle
  delay(5);
  // read the value from the green pot control:
  greenPotValue = analogRead(greenPotPin);
  // give the ADC a moment to settle
  delay(5);
  // read the value from the blue pot control:
  bluePotValue = analogRead(bluePotPin);
  // print out the values to the serial monitor
  Serial.print("raw sensor Values \t red: ");
  Serial.print(redPotValue);
  Serial.print("\t green: ");
  Serial.print(greenPotValue);
  Serial.print("\t Blue: ");
  Serial.println(bluePotValue);
  /*
  In order to use the values from the pots for the LEDs,
  you need to do some math. The ADC provides a 10-bit number,
  but analogWrite() uses 8 bits. You'll want to divide your
  sensor readings by 4 to keep them in range of the output.
  */
  redValue = map(redPotValue, 0, 1023, 0, 255);
  greenValue = map(greenPotValue, 0, 1023, 0, 255);
  blueValue = map(bluePotValue, 0, 1023, 0, 255);;
  //  print out the mapped values
  Serial.print("Mapped sensor Values \t red: ");
  Serial.print(redValue);
  Serial.print("\t green: ");
  Serial.print(greenValue);
  Serial.print("\t Blue: ");
  Serial.println(blueValue);
  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  for (int i = 0; i < NUMPIXELS; i++) {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(redValue, greenValue, blueValue));
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(50); // Delay for a period of time (in milliseconds).
  }
}

Code

Code snippet #1Arduino
/*
Color addressable LEDs control using Adafruit NeoPixel library

 Parts required:
 1m strip of addressable LEDs http://store.arduino.cc/products/C000083
 
 This example code is part of the public domain
 */

#include <Adafruit_NeoPixel.h>

const int NUMPIXELS = 60; //number of LEDs in 1m
const int LEDsPin = 9;    // LEDs connected to digital pin 9

const int redPotPin = A0;      // pin to control red
const int greenPotPin = A1;   // pin to control green
const int bluePotPin = A2;   // pin to control blue

int redValue = 0; // value to write to the red LED
int greenValue = 0; // value to write to the green LED
int blueValue = 0; // value to write to the blue LED

int redPotValue = 0; // variable to hold the value from the red pot
int greenPotValue = 0; // variable to hold the value from the green pot
int bluePotValue = 0; // variable to hold the value from the blue pot

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);

  // set the digital pin as output
  pinMode(LEDsPin, OUTPUT);
}

void loop() {
  // Read the pots first:

  // read the value from the red pot control:
  redPotValue = analogRead(redPotPin);
  // give the ADC a moment to settle
  delay(5);
  // read the value from the green pot control:
  greenPotValue = analogRead(greenPotPin);
  // give the ADC a moment to settle
  delay(5);
  // read the value from the blue pot control:
  bluePotValue = analogRead(bluePotPin);

  // print out the values to the serial monitor
  Serial.print("raw sensor Values \t red: ");
  Serial.print(redPotValue);
  Serial.print("\t green: ");
  Serial.print(greenPotValue);
  Serial.print("\t Blue: ");
  Serial.println(bluePotValue);

  /*
  In order to use the values from the pots for the LEDs,
  you need to do some math. The ADC provides a 10-bit number,
  but analogWrite() uses 8 bits. You'll want to divide your
  sensor readings by 4 to keep them in range of the output.
  */
  redValue = map(redPotValue, 0, 1023, 0, 255);
  greenValue = map(greenPotValue, 0, 1023, 0, 255);
  blueValue = map(bluePotValue, 0, 1023, 0, 255);;

  //  print out the mapped values
  Serial.print("Mapped sensor Values \t red: ");
  Serial.print(redValue);
  Serial.print("\t green: ");
  Serial.print(greenValue);
  Serial.print("\t Blue: ");
  Serial.println(blueValue);

  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  for (int i = 0; i < NUMPIXELS; i++) {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(redValue, greenValue, blueValue));
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(50); // Delay for a period of time (in milliseconds).
  }
}
Github
https://github.com/adafruit/Adafruit_NeoPixel/archive/master.zip

Schematics

Schematic 1
750x750 54789ba7ace5d5 47589761 zjnkhsplhq

Comments

Similar projects you might like

Arduino Color Mixer

Project tutorial by Ahmed Hamdy

  • 14,781 views
  • 6 comments
  • 59 respects

Color Sensor Prototype: Detect Color Name from Objects

Project tutorial by moretticb

  • 3,024 views
  • 4 comments
  • 23 respects

Using SMS messages to control LED color

Project tutorial by SteiniBrodda

  • 6,973 views
  • 0 comments
  • 6 respects

LCD Thermometer With LEDs And Alarm Signal

Project showcase by GioMac

  • 4,493 views
  • 5 comments
  • 25 respects

tinyGlow Smart LEDs

by Alex Glow

  • 3,799 views
  • 3 comments
  • 24 respects
Add projectSign up / Login