In this tutorial, I'll show you what an RGB LED is and how it works. We'll use a NodeMCU ESP8266 to control an RGB LED, creating different colors by varying the voltage supplied to the LED's red, green, and blue components.
What is an RGB LED?
An RGB LED is a combination of three LEDs: Red, Green, and Blue. These three colors can be combined to create almost any color by varying the voltage supplied to each LED.
Types of RGB LEDs
An RGB LED has four pin interfaces:
- 3 Pins for Red, Green, and Blue
- 1 Common Pin for all three LEDs
RGB LEDs can be of two types:
- Common Anode: The Anode (+) pin is common.
- Common Cathode: The Cathode (-/GND) pin is common.
Hardware Required
- NodeMCU ESP8266 - Link to Amazon
- Breadboard - Link to Amazon
- RGB LED - Link to Amazon
- Resistor (1k Ohm) - Link to Amazon
- Jumper Wires (Male to Male) - Link to Amazon
- Micro USB Cable - Link to Amazon
- Computer with Arduino IDE installed - Link to Arduino IDE download
Circuit Diagram (Schematic)
Common Anode Layout
Programming
Here's the code for controlling the RGB LED with the NodeMCU using the Arduino IDE:
// Mechatronicslabrpi.blogspot.com Tutorial
// Hardware: NodeMCU
int RED = 5;
int GREEN = 4;
int BLUE = 0;
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}
void loop() {
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
delay(1000);
digitalWrite(RED, LOW);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, LOW);
delay(1000);
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, HIGH);
delay(1000);
}
Steps to Upload the Code
- Open Arduino IDE.
- Copy and paste the above code into the IDE.
- Connect your NodeMCU to your computer using the Micro USB cable.
- Select the correct board (NodeMCU 1.0) and port from the Tools menu.
- Click on the upload button.
Testing the Setup
Once the code is uploaded, the RGB LED will cycle through red, green, and blue colors, with each color staying on for one second.
No comments