Arduino Uno R4 Minima #2: RGB LED and how to generate different Colors
Table of Contents
Arduino Uno R4 RGB Led:
Arduino Uno R4 Minima RGB LED and how to generate different Colors- I have already done the unboxing of the SunFounder’s Ultimate Sensor Kit and explained in detail which sensors and boards are included in this kit. We will use all the sensors one by one.
I have already written quite a detailed article on the Arduino Uno R4 Minima board, where I have explained the technical specifications, pinout, how to add the Arduino Uno R4 Minima board in the Arduino IDE, and how to upload a program. Not just that, for a comprehensive explanation, I have also compared the Arduino Uno R4 Minima board with the Arduino Uno R3. So, I would recommend reading my article on the Arduino Uno R4 Minima board.
Anyway, from the Sensor kit we are going to select;
- Arduino Uno R4 Minima board, If you have the latest Arduino Uno R4 Minima board, that’s great. If you don’t have an Arduino Uno R4, you can also use an Arduino Uno R3 or an Arduino Nano.
- A breadboard which allows you to use components without any soldering. Breadboard is one of the must have tools for testing your prototype projects.
- RGB LED Module, you can use this in different projects. Let’s say when the sensor value is normal, you can simply turn on the Green LED, when the sensor value starts to increase, you can turn on the Blue Led, and when the sensor value exceeds a threshold value then you can turn on the Red LED. You can also use it in security projects and for testing the output states.
- A Pushbutton, A push button with Arduino allows beginners to create interactive projects like games or alarms, facilitating hands-on learning and improving coding skills by introducing elements of user input and control.
- Jumper Wires: some male-to-male and male-to-female jumper wires. With the help of jumper wires, we can interface different sensors and modules with Arduino.
- A 9V battery to power up the Arduino, this is optional because in today’s examples, we are only using RGB LEDs and a push button, so we can power up the Arduino Uno R4 board from a Laptop or PC as well.
Amazon Links:
SunFounder Ultimate Sensor Kit
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
RGB Module:
This RGB LED module has a total of 4 pins, clearly labeled as -, R, G, and B. On this RGB LED module, the cathode legs of the LEDs are connected together. Therefore, we will connect the – pin with GND (Ground), and the R, G, B pins are connected with the Anode legs of the LEDs. So, we will connect these 3 pins with the IO (Input/Output) pins of the Arduino.
To control a common cathode RGB LED module, you can adjust the brightness and color by changing the voltage on the R, G, and B pins. Each pin controls the intensity of its respective color red, green, or blue. By providing a variable voltage through pulse-width modulation (PWM) on the Arduino’s IO pins, you can mix these colors in various intensities to create a wide range of colors. The common cathode (- pin), which is connected to the GND, acts as a shared ground for all three LEDs in the module. Remember, to protect the LED, it’s important to use a current-limiting resistor with each of the R, G, and B pins. But this RGB Led module already has current limiting resistors; so there is no need to add any resistors.
Basic Structure:
RGB LEDs are constructed using multiple semiconductor materials that emit light at different wavelengths. Each of the three primary colors (red, green, and blue) is generated by a separate semiconductor within the LED. The combination of these three colors in varying intensities produces a wide range of hues.
Operating Principle:
The operation of an RGB LED is based on the principles of electroluminescence. When a voltage is applied across the semiconductor materials, electrons and holes recombine, releasing energy in the form of photons. The specific materials used determine the wavelength of the emitted light, resulting in red, green, or blue colors.
Applications:
Decorative Lighting:
- RGB LEDs are widely used in decorative lighting applications, such as ambient lighting in homes, offices, and entertainment spaces.
Entertainment Systems:
- They are commonly found in LED strips, gaming peripherals, and audiovisual setups to create dynamic lighting effects synchronized with content.
Signage and Displays:
- RGB LEDs are employed in digital signage, large displays, and information boards for vibrant and attention-grabbing visuals.
Automotive Lighting:
- In automotive applications, RGB LEDs are used for interior and exterior lighting, providing customizable lighting options.
Pushbutton Module:
Principle
The button module operates based on the fundamental principle of a switch, which is an essential electrical component capable of opening or closing a circuit.
Now, let’s delve into the internal structure of a button. The schematic representation of a button in circuits is commonly denoted by the symbol on the right below.
Examining the internal configuration, pin 1 is electrically connected to pin 2, while pin 3 is linked to pin 4. When the button is pressed, these four pins become interconnected, effectively closing the circuit. This closure allows the flow of electric current through the circuit, enabling the designated functionality associated with the button’s operation.
Furthermore, the design of button modules can vary, incorporating features like spring mechanisms for tactile feedback or debounce circuits to eliminate unwanted electrical noise during button actuation. This adaptability makes button modules suitable for diverse applications, ranging from simple push-button switches to sophisticated touch-sensitive controls.
The symbol used to represent a button in circuits serves as a standardized visual reference, facilitating seamless integration into electronic schematics and designs. Understanding the internal workings of buttons is crucial for engineers and designers, as it enables them to tailor the functionality of buttons to specific requirements in electronic systems, such as those found in consumer electronics, industrial automation, and interactive interfaces.
This Pushbutton module has a total of 3 pins; the signal pin, Voltage pin, and Gnd. You can see there is a resistor, so you don’t need to worry about adding a pulling-up resistor.
These two modules are breadboard-friendly and interfacing them with the Arduino Uno is quite easy. The design of these modules allow them to be easily inserted into a breadboard, facilitating a simple and quick connection without the need for soldering. This feature makes them ideal for prototyping and experimenting with different circuits and configurations.
To connect the RGB LED module to the Arduino Uno simply connect the – pin to the Arduino Gnd. Connect the R, G, B pins to the Arduino PWM pins 9, 10, and 11.
Connect the Signal pin to the Arduino I/O (Input/Output) pin, because the Arduino will detect the button press with the help of this pin. This allows the Arduino to read the state of the button; when the button is pressed, the state changes, which the Arduino can detect and use to trigger events or actions in your code. Next, connect the middle pin to the Arduino’s 5V supply. This provides the necessary power to the button. Finally, connect the – pin to the Arduino’s GND (Ground).
You can follow this circuit diagram.
Although we have connected both the modules, but first we will start with the RGB LED module. I will run some basic programs on it, such as how to control these RGB LEDs individually, and how to mix these LEDs to generate different colors. So, let’s go ahead and start with our first example.
RGB LED Blinking Program 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
const int R_Pin = 9; // Red LED is connected to the Arduino PWM Pin 9 const int G_Pin = 10; // Green LED is connected to the Arduino PWM Pin 10 const int B_Pin = 11; // Blue LED is connected to the Arduino PWM Pin 11 void setup() { pinMode(R_Pin, OUTPUT); pinMode(G_Pin, OUTPUT); pinMode(B_Pin, OUTPUT); } void loop() { // Turn off all the three LEDs digitalWrite(R_Pin, LOW); digitalWrite(G_Pin, LOW); digitalWrite(B_Pin, LOW); delay(1000); // one second delay: 1000 milli seconds are equal to 1 second. digitalWrite(R_Pin, HIGH); delay(1000); digitalWrite(R_Pin, LOW); delay(1000); digitalWrite(G_Pin, HIGH); delay(1000); digitalWrite(G_Pin, LOW); delay(1000); digitalWrite(B_Pin, HIGH); delay(1000); digitalWrite(B_Pin, LOW); delay(1000); } |
We need to properly define the pins. It’s important to name them accurately so that anyone reading the program can immediately understand their purpose.
As you can see, I have properly defined all three pins of the RGB Module. Anyone reading this program will immediately know that I have connected the Red, Green, and Blue LEDs to the Arduino’s PWM pins 9, 10, and 11. You can also define these pins without using the const keyword. However, there are certain advantages to using the const keyword. For example;
The const keyword tells the compiler that the value of R_Pin is constant and should not change throughout the program. This means that once R_Pin is set to 9, it cannot be altered anywhere else in the code. This is particularly useful for pin assignments in Arduino and other controller boards, as the pin connections are typically fixed once the hardware setup is completed.
By declaring R_Pin as a constant, it prevents accidental modification of this variable in the code. If any part of the program attempts to change the value of R_Pin, the compiler will generate an error. This helps in maintaining the integrity of the code, especially in larger or more complex programs.
Using const makes the code more readable and understandable. It clearly communicates to anyone reading the code that this variable represents a fixed value (in this case, a pin number) that should not change. This is helpful for both the original programmer and anyone else who might work with or modify the code in the future.
After properly defining the pins, the next step is to inform the compiler about the roles of each pin – distinguishing which ones will function as inputs and which as outputs. This crucial step takes place in the setup() function, a fundamental part of every Arduino program. The setup() function is essentially where we lay down the groundwork, setting up the initial conditions and configurations for our Arduino project. It runs only once when the program starts, making it the perfect place to initialize pin modes.
In our case, as the LED is an output device, we utilize the pinMode() function to set it as OUTPUT. This tells the Arduino that these pins will be used to control the LEDs.
In Arduino programming, the loop() function is one of the core aspects of any sketch. It runs continuously after the setup() function has completed its initial run. This is where the main part of your code executes and is the heart of most Arduino programs. It’s where the dynamic actions of your code take place, responding to sensor inputs, controlling LEDs, motors, and other components.
To turn ON or turn OFF any pin on the Arduino we use the digitalWrite() function. It takes two arguments as the input, the pin and the state HIGH or LOW. You can see I have turned OFF all the LEDs. Then there is a delay of 1 second. 1000 milliseconds are equal to 1 second.
Then I am turning ON and turn OFF each LED with a 1 second delay.
Practical Demo:
In a program like the above one, if you want to change the delay time, you will have to manually change the delay at all places, which can be quite annoying, especially if the code is lengthy. To easily change the delay time, we can define a separate variable and then use it in the delay function. To explain this point, I have slightly modified this program.
RGB LED Blinking Program 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
const int R_Pin = 9; // Red LED is connected to the Arduino PWM Pin 9 const int G_Pin = 10; // Green LED is connected to the Arduino PWM Pin 10 const int B_Pin = 11; // Blue LED is connected to the Arduino PWM Pin 11 int MyDelay = 50; void setup() { pinMode(R_Pin, OUTPUT); pinMode(G_Pin, OUTPUT); pinMode(B_Pin, OUTPUT); } void loop() { // Turn off all the three LEDs digitalWrite(R_Pin, LOW); digitalWrite(G_Pin, LOW); digitalWrite(B_Pin, LOW); delay(MyDelay); // one second delay: MyDelay milli seconds are equal to 1 second. digitalWrite(R_Pin, HIGH); delay(MyDelay); digitalWrite(R_Pin, LOW); delay(MyDelay); digitalWrite(G_Pin, HIGH); delay(MyDelay); digitalWrite(G_Pin, LOW); delay(MyDelay); digitalWrite(B_Pin, HIGH); delay(MyDelay); digitalWrite(B_Pin, LOW); delay(MyDelay); } |
You can see that I have defined a variable named MyDelay, where I have stored the value 500. This represents the delay time in milliseconds. I have used the MyDelay variable in the delay() function throughout the entire program. Now, if I need to change the delay time, I can simply go to the top and change the value there.
Now, you can increase the delay time in just a second, try different delays and you will be amazed with the cool patterns. For the practical demonstration, you can watch the video tutorial available on “Electronic Clinic” YouTube channel.
Anyway, next we are going to generate different colors. As you know RGB LEDs, or Red-Green-Blue Light-Emitting Diodes can emit light in three primary colors: red, green, and blue. These LEDs are widely used in various applications due to their ability to produce a broad spectrum of colors by adjusting the intensity of each primary color. In this example, I am not going to control the light intensity but I am going to directly mix these primary colors.
Yellow (R+G)
Cyan (G+B)
Magenta (R+B)
White (R+G+B)
RGB LEDs Color Mixing Program 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
const int R_Pin = 9; // Red LED is connected to the Arduino PWM Pin 9 const int G_Pin = 10; // Green LED is connected to the Arduino PWM Pin 10 const int B_Pin = 11; // Blue LED is connected to the Arduino PWM Pin 11 int MyDelay = 1000; void setup() { pinMode(R_Pin, OUTPUT); pinMode(G_Pin, OUTPUT); pinMode(B_Pin, OUTPUT); } void loop() { // Turn off all the three LEDs digitalWrite(R_Pin, LOW); digitalWrite(G_Pin, LOW); digitalWrite(B_Pin, LOW); delay(MyDelay); // one second delay: MyDelay milli seconds are equal to 1 second. digitalWrite(R_Pin, HIGH); delay(MyDelay); digitalWrite(R_Pin, LOW); delay(MyDelay); digitalWrite(G_Pin, HIGH); delay(MyDelay); digitalWrite(G_Pin, LOW); delay(MyDelay); digitalWrite(B_Pin, HIGH); delay(MyDelay); digitalWrite(B_Pin, LOW); delay(MyDelay); //Yellow (R+G) digitalWrite(R_Pin, HIGH); digitalWrite(G_Pin, HIGH); delay(MyDelay); digitalWrite(R_Pin, LOW); digitalWrite(G_Pin, LOW); delay(MyDelay); //Cyan (G+B) digitalWrite(G_Pin, HIGH); digitalWrite(B_Pin, HIGH); delay(MyDelay); digitalWrite(G_Pin, LOW); digitalWrite(B_Pin, LOW); delay(MyDelay); //Magenta (R+B) digitalWrite(R_Pin, HIGH); digitalWrite(B_Pin, HIGH); delay(MyDelay); digitalWrite(R_Pin, LOW); digitalWrite(B_Pin, LOW); delay(MyDelay); //White (R+G+B) digitalWrite(R_Pin, HIGH); digitalWrite(G_Pin, HIGH); digitalWrite(B_Pin, HIGH); delay(MyDelay); digitalWrite(R_Pin, LOW); digitalWrite(G_Pin, LOW); digitalWrite(B_Pin, LOW); delay(MyDelay); } |
I modified the program a bit. I changed the delay time back to 1000 milliseconds. Now, if we go to the loop() function…
You can see I added other instructions. There isn’t anything complicated, I am just turning ON and turning OFF multiple LEDs at a time to generate different colors.
To Generate the Yellow color I am simply turning ON the Red and Green LEDs at the same time. Then it stays ON for 1 second and then turns OFF.
Similarly, to generate the Cyan color I am turning ON the Green and Blue LEDs at the same time.
To generate the Magenta color I am turning ON the Red and Blue LEDs and for the White color I am turning ON all the LEDs at the same time.
To avoid making this article too long, I will use the push button in my next article. So, that’s all for now.
Watch Video Tutorial: