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 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
Arduino UNO R3 or R4 |
|
- |
Wiring
Code
Code Analysis
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
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); }
In the
loop()function, thesetColor()function is called with different parameters to display different colors. Thedelay()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... }
The
setColor()function uses theanalogWrite()function to adjust the brightness of each color channel on the RGB LED module. TheanalogWrite()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 }