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:

  1. Common Anode: The Anode (+) pin is common.
  2. Common Cathode: The Cathode (-/GND) pin is common.

Hardware Required

Circuit Diagram (Schematic)

Common Anode:-Anode (+) pin is common.




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

  1. Open Arduino IDE.
  2. Copy and paste the above code into the IDE.
  3. Connect your NodeMCU to your computer using the Micro USB cable.
  4. Select the correct board (NodeMCU 1.0) and port from the Tools menu.
  5. 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

Theme images by Dizzo. Powered by Blogger.