How to use RGB LED bulb with Arduino | RGB LED control

How to use RGB LED bulb with Arduino | RGB LED control

Hello guys, Welcome back to my SriTu Hobby blog. Today we will learn how to control an RGB LED bulb. first, let’s see what is an RGB LED bub. An RGB LED Bulb is a diode made up of three colors: red, green, and blue. We can get the desired color by adding or subtracting the three colors here. Also, we can light these three colors separately. We can usually see two pins in an LED bulb, but in this RGB LED bulb, we can see four pins. Here we can identify three pins as the anode terminal of the three colors and the other pin as the cathode terminal of the three colors. In particular, the cathode pin is longer than the other pins. Also, we can see common cathode and common anode RGB LEDs in the market. You can choose it as per your requirements.

We will now test this RGB LED bulb using an Arduino board. For this, we need three Potentiometers in addition to the RGB bulb. The potentiometer is a variable resistor. Here the resistance value is changed by twisting the knob. Through these, we can increase or decrease the brightness of the three diodes in the RGB LED bulb.

Well now let’s see what components are needed for this project. You can easily buy these components using the links below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Ok, let’s do this project step by step.

Step 1

Firstly, identify these components.

Step 2

Now connect these components. For that, use the circuit diagram below.

How to use RGB LED bulb with Arduino | RGB LED control

Step 3

Well now let’s look at the program required for this. You can see this program below.

  • The complete program of this project – Download
/*RGB LED bulb PWM with Arduino.
  Serial monitor readings.
  created by the SriTu Tech team.
  Read the code below and use it for any of your creation
*/
#define red 3
#define green 5
#define blue 6
 
void setup() {
  Serial.begin(9600);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
}
 
void loop() {
  int p1 = analogRead(A0);
  int p2 = analogRead(A1);
  int p3 = analogRead(A2);
 
  p1 = map(p1, 0, 1023, 0, 255);
  analogWrite(red, p1);
  Serial.println(p1);
  p2 = map(p2, 0, 1023, 0, 255);
  analogWrite(green, p2);
  Serial.println(p2);
  p3 = map(p3, 0, 1023, 0, 255);
  analogWrite(blue, p3);
  Serial.println(p3);
}

Code explanation

First, RGB LED bulb pins are defined. These are PWM pins.

#define red 3
#define green 5
#define blue 6

In this Void setup, these pins are set as output pins.

void setup() {
  Serial.begin(9600);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
}

In the loop function, the potentiometer values are put into three int variables. Then convert these values from 0 to 255.

void loop() {
  int p1 = analogRead(A0);
  int p2 = analogRead(A1);
  int p3 = analogRead(A2);
 
  p1 = map(p1, 0, 1023, 0, 255);
  analogWrite(red, p1);
  Serial.println(p1);
  p2 = map(p2, 0, 1023, 0, 255);
  analogWrite(green, p2);
  Serial.println(p2);
  p3 = map(p3, 0, 1023, 0, 255);
  analogWrite(blue, p3);
  Serial.println(p3);
}

Step 4

Now select the correct board and port. Finally, upload this code.

Now, you can test this project by rotating the potentiometers. The full video guide is below. So, we hope to see you in the next tutorial or project. have a good day.

How to use RGB LED bulb with Arduino | RGB LED control

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
Select your currency
USD United States (US) dollar
EUR Euro
Scroll to Top