Arduino RGB LED Strip Controller

Published

Introduction: Arduino RGB LED Strip Controller

About: Mechanical Engineering Student

In this Instructable I will show you how to make an Arduino RGB LED strip controller with a rotary encoder. Usually, when people want to control their RGB LED strip with an Arduino, three potentiometers are used to mix the red, green and blue colors. This works and could be perfectly fine for your needs, but I wanted to make something more intuitive, something more like a color wheel. So I searched the internet and found something called a rotary encoder. This is a turning knob that registers in which way the shaft is turned. This way you can attach some variable which increases when the encoder is turned one way, and decreases when it is turned the other way. For more on rotary encoders, you can watch this video which explains it more in-depth.

This Instructable covers the construction of the circuit on a breadboard. You could, however, produce your own PCB with a service like Seeed or do it yourself, in order to create your own Arduino shield!

Step 1: Parts

I bought nearly all the necessary parts at Banggood.com, but they can also be found at eBay, Adafruit, SparkFun etc. For the LED controller you will need the following materials:

Step 2: Circuit

Connect the components and wires as shown in the diagram. Connect the 12V power supply to the positive and ground rail of the breadboard. Make sure you check polarities! Then you can connect the positive wire of the LED strip to the brown wire, and the red, green and blue wires of the LED strip to the corresponding wires on the breadboard.


Arduino

Vin - 12V

GND - gnd


Rotary encoder (see image for pinout)

Pin A - D4

Pin B - D3

Pin C - gnd


MOSFET Red (see image for pinout)

Gate - gnd

Drain - LED strip red wire

Source - D11


MOSFET Green (see image for pinout)
Gate - gnd

Drain - LED strip green wire

Source - D9


MOSFET Blue (see image for pinout)
Gate - gnd

Drain - LED strip blue wire

Source - D6

Step 3: Code

int redPin = 11;
int greenPin = 6; 
int bluePin = 9;

int colorVal;

int redVal;
int greenVal;
int blueVal;

int encoder0PinA = 4;
int encoder0PinB = 3;
int encoder0Pos = 0;
int encoder0PinALast = HIGH;
int n = LOW;

void setup() 
{ 
 pinMode (encoder0PinA,INPUT_PULLUP);
 pinMode (encoder0PinB,INPUT_PULLUP);
} 

void loop() 
{
  // Rotary encoder
  // INPUT: rotary encoder position
  // OUTPUT: encoder0Pos [0-1535]
  
  n = digitalRead(encoder0PinA);
 
  if ((encoder0PinALast == LOW) && (n == HIGH)) 
  {
    if (digitalRead(encoder0PinB) == LOW) 
    {
      encoder0Pos = encoder0Pos - 8;
    } 
    else 
    {
      encoder0Pos = encoder0Pos + 8;
    }
    
    if (encoder0Pos < 0) 
    {
      encoder0Pos = 1535;
    }
    else if (encoder0Pos > 1535) 
    {
      encoder0Pos = 0;
    }
  } 

  encoder0PinALast = n;

  // Led strip
  // INPUT: encoder0Pos [0-1535]
  // OUTPUT: redVal [0-255], greenVal [0-255], blueVal [0-255]

  // Red to yellow
  if (encoder0Pos <= 255)
  {
    colorVal = encoder0Pos;             
    redVal = 255;
    greenVal = colorVal;
    blueVal = 0;
  }

  // Yellow to green
  else if (encoder0Pos <= 511)
  {
    colorVal = encoder0Pos - 256;
    redVal = 255 - colorVal;
    greenVal = 255;
    blueVal = 0;
  }

  // Green to cyan
  else if (encoder0Pos <= 767)
  {
    colorVal = encoder0Pos - 512;
    redVal = 0;
    greenVal = 255;
    blueVal = colorVal;
  }

  // Cyan to blue
  else if (encoder0Pos <= 1023)
  {
    colorVal = encoder0Pos - 768;
    redVal = 0;
    greenVal = 255 - colorVal;
    blueVal = 255;
  }

  // Blue to magenta
  else if (encoder0Pos <= 1279)
  {
    colorVal = encoder0Pos - 1024;
    redVal = colorVal;
    greenVal = 0;
    blueVal = 255;
  }

  // Magenta to red
  else
  {
    colorVal = encoder0Pos - 1280;
    redVal = 255;
    greenVal = 0;
    blueVal = 255 - colorVal;
  }

  // Set LED strip color
  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin, blueVal);}

Step 4: Result

After uploading the code, you should be able to control the RGB LED strip by turning the rotary encoder clockwise or counterclockwise. Enjoy!

Share

    Recommendations

    • Oil Contest

      Oil Contest
    • Casting Contest

      Casting Contest
    • Clocks Contest

      Clocks Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    5 Comments

    To be exact, I used this LED strip from BangGood. However, I would not recommend it as it is not a RGB-LED strip. Red, green and blue LED's are placed next to each other with a few centimeters between them. This way you don't have a smooth color. I would, for example, recommend you to buy this LED strip, which is a true RGB-LED strip.

    Thanks! Glad you like it