Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 28: RGB LED Module

In this lesson, you will learn how to control an RGB LED with Arduino. We’ll cover setting up the LED and then delve into displaying primary colors and creating a vibrant rainbow spectrum. This hands-on project is ideal for beginners, providing practical experience with output operations and color mixing in the Arduino environment.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Arduino UNO R3 or R4

BUY

RGB LED Module

-

Wiring

../_images/Lesson_28_rgb_module_circuit_uno_bb.png

Code

Code Analysis

  1. The first segment of the code declares and initializes the pins to which each color channel of the RGB LED module is connected.

    const int rledPin = 9;  // pin connected to the red color channel
    const int gledPin = 10;   // pin connected to the green color channel
    const int bledPin = 11;  // pin connected to the blue color channel
    
  2. The setup() function initializes these pins as OUTPUT. This means we are sending signals OUT from these pins to the RGB LED module.

    void setup() {
      pinMode(rledPin, OUTPUT);
      pinMode(gledPin, OUTPUT);
      pinMode(bledPin, OUTPUT);
    }
    
  3. In the loop() function, the setColor() function is called with different parameters to display different colors. The delay() function is used after setting each color to pause for 1000 milliseconds (or 1 second) before moving on to the next color.

    void loop() {
      setColor(255, 0, 0);  // Set RGB LED color to red
      delay(1000);
      setColor(0, 255, 0);  // Set RGB LED color to green
      delay(1000);
      // The rest of the color sequence...
    }
    
  4. The setColor() function uses the analogWrite() function to adjust the brightness of each color channel on the RGB LED module. The analogWrite() function employs Pulse Width Modulation (PWM) to simulate varying voltage outputs. By controlling the PWM duty cycle (the percentage of time a signal is HIGH within a fixed period), the brightness of each color channel can be controlled, allowing the mixing of various colors.

    void setColor(int R, int G, int B) {
      analogWrite(rledPin, R);  // Use PWM to control the brightness of the red color channel
      analogWrite(gledPin, G);  // Use PWM to control the brightness of the green color channel
      analogWrite(bledPin, B);  // Use PWM to control the brightness of the blue color channel
    }