Arduino RGB LED Strip Controller

About: Mechanical Engineering Student

Intro: Arduino RGB LED Strip Controller

Often 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 like a color wheel.

This project seems to be a perfect application for a rotary encoder. This is a device that converts the motion of its shaft to a digital output. When the shaft is turned, the encoder sends out a signal (pulse) which can be measured by an Arduino. For more on rotary encoders, you can watch this video which explains it more in-depth.

In this Instructable I will show you how to make an Arduino RGB LED strip controller by using a rotary encoder. This Instructable covers the construction of the circuit on a breadboard. You could however produce your own PCB in order to create an Arduino shield!

Step 1: Parts

For the RGB LED strip controller you will need the following materials:

  • 1x Arduino Nano
  • 3x IRLB8721PBF, any N-channel logic level MOSFET will do as long as it is rated at a minimum of 12V and the current your LED strip consumes.
  • 1x Rotary encoder
  • 1x 12V 2A power supply, the current the power supply has to deliver may depend on the length of the used LED strip.
  • 16x Male to male jumper wires
  • 1x Solderless breadboard, any breadboard will do as long as it is sufficiently large.

Step 2: Circuit

Connect the Arduino to the 12V and GND rail of the breadboard. Then connect the other parts as follows:


Rotary encoder

Pin A - D4

Pin B - D3

GND - GND


MOSFET Red

Gate - GND

Drain - LED strip red wire

Source - D11


MOSFET Green
Gate - GND

Drain - LED strip green wire

Source - D9


MOSFET Blue
Gate - GND

Drain - LED strip blue wire

Source - D6

Step 3: Code

// Arduino PWM pins
int redPin = 11;
int greenPin = 6;
int bluePin = 9;

// Arduino encoder pins
int encoderPinA = 3;
int encoderPinB = 4;

// Color variables
int colorVal;
int redVal;
int greenVal;
int blueVal;

// Encoder variables
int encoderPos;
int encoderPinACurrent;
int encoderPinALast = HIGH;

// Other
int counter;

void setup(){ 
  pinMode(encoderPinA,INPUT_PULLUP);
  pinMode(encoderPinB,INPUT_PULLUP);
} 

void loop(){
  readEncoder();
  encoder2rgb(counter);

  analogWrite(redPin, redVal);
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin, blueVal);
}

int readEncoder(){
  encoderPinACurrent = digitalRead(encoderPinA);

  if ((encoderPinALast == LOW) && (encoderPinACurrent == HIGH)){
    if (digitalRead(encoderPinB) == LOW){
      encoderPos = encoderPos - 1;
    } 
    else {
      encoderPos = encoderPos + 1;
    }
  }

  encoderPinALast = encoderPinACurrent;
  counter = encoderPos*8;

  if (counter < 0){
    counter = 1535;
  }
  else if (counter > 1535){
    counter = 0;
  }  

  return counter;
}

int encoder2rgb(int counterVal){
  // Red to yellow
  if (counterVal <= 255){
    colorVal = counterVal;

    redVal = 255;
    greenVal = colorVal;
    blueVal = 0;
  }

  // Yellow to green
  else if (counterVal <= 511){
    colorVal = counterVal - 256;

    redVal = 255 - colorVal;
    greenVal = 255;
    blueVal = 0;
  }

  // Green to cyan
  else if (counterVal <= 767){
    colorVal = counterVal - 512;

    redVal = 0;
    greenVal = 255;
    blueVal = colorVal;
  }

  // Cyan to blue
  else if (counterVal <= 1023){
    colorVal = counterVal - 768;

    redVal = 0;
    greenVal = 255 - colorVal;
    blueVal = 255;
  }

  // Blue to magenta
  else if (counterVal <= 1279){
    colorVal = counterVal - 1024;

    redVal = colorVal;
    greenVal = 0;
    blueVal = 255;
  }

  // Magenta to red
  else{
    colorVal = counterVal - 1280;

    redVal = 255;
    greenVal = 0;
    blueVal = 255 - colorVal;
  }

  return redVal, greenVal, 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

    • Tiny Home Contest

      Tiny Home Contest
    • Metalworking Contest

      Metalworking Contest
    • Audio Contest 2018

      Audio Contest 2018

    5 Discussions

    0
    None
    TiggelenRiisgaard

    Reply 6 months ago

    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.