Use of RGB LEDs

This scenario presents how to handle the brightness control of the tri-coloured LEDs. Both LEDs are electrically bound and cannot be controlled independently. Those LEDs have 3 colour channels, controlled independently: R (Red), G (Green) and B (Blue). Mixing of those colours creates other ones, such as pink and violet. Each R G B channel can be controlled with a separate GPIO to switch it on or off or control brightness using a PWM signal, as presented in this tutorial.

Step 1

Define the necessary pins and functions:

/* RGB LEDS */
#define RED_PIN   9
#define GREEN_PIN 10
#define BLUE_PIN  11
void setRGB(int red, int green, int blue);

Step 2

Define pins as output:

void setup() {
  Serial.begin(152000);

  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

Step 3

Add a function at the end of the code:

void setRGB(int red, int green, int blue) {
  analogWrite(RED_PIN, green);
  analogWrite(GREEN_PIN, blue);
  analogWrite(BLUE_PIN, red);
}

Step 4

Write some code for the LED's (circle through red, green, blue light):

void loop() {
  setRGB(255, 0, 0);
  delay(1000);
  setRGB(0, 255, 0);
  delay(1000);
  setRGB(0, 0, 255);
  delay(1000);
}
en/iot-open/practical/hardware/rtu/esp32/scenarios/rgb-led.txt · Last modified: 2025/07/30 09:29 by kivilands6
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0