Push Button RGB LED Color Change Project with Arduino

Share:
In this Arduino project, we will control an RGB LED using Push Button to change color interfacing with Arduino Board. Basically, The RGB LED will cycle through the colors red, green, blue, purple and white when we press the "Push Button". Let's make it.

Push Button RGB LED Color Change Project

Components List

The following components are needed for this project.
  1. RGB LED Module (or 1x RGB LED, 3x 150R Resistor).
  2. Arduino Board.
  3. Push Button Module (or 1x Push Button, 1x 1K Resistor).
  4. Breadboard.
  5. Jumper Wires.
I used KY-016 RGB LED Module and Push Button Module in this project. Don't worry, you can easily create these modules in your home.

RGB LED Module Pinout
KY-016 RGB LED Module Pinout.
Push Button Module Pinout
Push Button Module Pinout.

Circuit Schematic

Now let us interface RGB LED and Push Button with Arduino. So the circuit diagram and connection are given below. You can follow the same.


Source Code/Program

The source code/program for interfacing RGB LED and Push Button with Arduino is given below. Copy this code and upload it to Arduino Board.

#define button 11
#define redLED 8
#define greenLED 9
#define blueLED 10
int state = 0;
int old = 0;
int buttonPoll = 0;

void setup() {
 pinMode(button,INPUT);
 //RGB LED set as output
 pinMode(redLED,OUTPUT);
 pinMode(greenLED,OUTPUT);
 pinMode(blueLED,OUTPUT);

 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
}

void loop() {
 buttonPoll = digitalRead(button);
 if(buttonPoll == 1) {
 delay(50);
 buttonPoll = digitalRead(button);
 if(buttonPoll == 0) {
 state = old + 1;
 }
 }
 else {
 delay(100);
}

switch (state) {
 case 1: //Red color
 digitalWrite(redLED,HIGH);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
 old = state;
 break;
 case 2: //Green color
 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,HIGH);
 digitalWrite(blueLED,LOW);
 old = state;
 break;
 case 3: //Blue color
 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,HIGH);
 old = state; 
 break;
 case 4: //Purple color
 digitalWrite(redLED,HIGH);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,HIGH);
 old = state; 
 break;
 case 5: //White color
 digitalWrite(redLED,HIGH);
 digitalWrite(greenLED,HIGH);
 digitalWrite(blueLED,HIGH);
 old = state; 
 break;
 default:
 digitalWrite(redLED,LOW);
 digitalWrite(greenLED,LOW);
 digitalWrite(blueLED,LOW);
 old = 0;
 break;
 }
}

Watch the video! - Testing this project

7 comments:

  1. This is a good subject to talk about. Sometimes I fav stuff like this on Redit. This article probably wont do well with that crowd. I will be sure to submit something else though. led engine

    ReplyDelete
  2. bad projec t i t dint werk wen i ded i t

    ReplyDelete
    Replies
    1. Please, check the connections and try again.

      Delete
  3. How to change the code to make the LEDs flash?

    ReplyDelete
  4. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future... ecopac led driver

    ReplyDelete
  5. I have made your circuit that works perfectly, but would like to add one more button press to select a fade red to green to blue, but don't know how to add it in to the menu, please can you help date 10-06-22 bob

    ReplyDelete
  6. what is the old variable for?

    ReplyDelete