Arduino RGB LED Strip Controller

3,756

21

7

About: Mechanical Engineering Student

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!

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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!

Lights Contest 2017

Participated in the
Lights Contest 2017

Be the First to Share

    Recommendations

    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    7 Discussions

    0
    None
    MarkieG2

    8 months ago

    would it be plausible to use a 5 volt RGB LED strip in place of the 12 Volt and power it straight from the arduino pins rather than an external power source. Ideally I would like to power it through a computer usb port. I think the brightness may suffer, but I am not sure. I just got my arduino today and am learning on the fly.

    1 reply
    0
    None
    TiggelenMarkieG2

    Reply 7 months ago

    An Arduino has a maximum current per GPIO port of only 40 mA, so driving the LED's directly from the Arduino will burn it. Using current from USB ports is also limited, because the USB spec says that, depending on which USB standard is applied, the maximum current is 400 - 1500 mA. Me personally, I wouldn't risk burning a laptop/PC/powerbank/etc. by using USB for the power supply.

    0
    None
    TiggelenRiisgaard

    Reply 1 year 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.