Welcome back to our programming tutorial using the Arduino IDE. Today we will deal with loops to operate color changing on a RGB LED.
You can take a look at the previous chapters of the course here:
- Arduino IDE: for loops against while / do while #6
- Arduino IDE: while and do while loops #5
- Arduino IDE: turn on LEDs using a button (if) #4.1
- Arduino IDE: the conditional construction IF #4
- Arduino IDE: arithmetic and logical operators #3
- Arduino IDE: variables, constants and macros #2
- How can I use Arduino IDE to write a program? #1
- Arduino IDE: what it is and how it works #0
RGB LEDs can be a sparkling way to add originality and fun to your project.
The RGB LED can be considered as the union of three LEDs (one red, one green and one blue LED) in a singular structure. The use and connection (both electronically and programmatically) is not very different compared to traditional LEDs.
There are two different versions of RGB LEDs on the market, the first with common anode, and the second with common cathode. The version with common anode uses 5V on the pin, while the second version with common cathode uses GND grounding on the pin.
In order for the correct operation of the RGB LED to occur, we use three resistors to limit the current passing through it.
An RGB LED is a LED that has 3 different semiconductors inside, each capable of emitting a different color and in particular red, green and blue. As is well known, all the colors of the spectrum can be made from a combination of the fundamental colors red green and blue.
We use PWM (pulse width modulation) to vary the brightness of the LEDs. As the Arduino PWM can manage values from 0 to 255, we can associate this value with the amount of each color component to form different tints. For example, by setting the PWM of red to 128 and the PWM of blue to 255 we can obtain purple.
Pulse Width Modulation (PWM) is a technique for controlling input/putput varying the duty cycle of a square wave with the voltage of the signal. In this case PWM is used to control the brightness of each individual LED. The pulse is controlled by the analogWrite function.
The RGB LED will be programmed to first turn on with the red color state, then it will change to green, and finally it will emit blue light, and cyclically return back to the red color.
List of the required components:
- (1) x Elegoo UNO R3 (Arduino UNO)
- (1) x Breadboard with 830 solderless tie-points
- (1) x RGB LED 5mm
- (3) x Resistor 220 ohm resistor
- (4) x M-M (male-male) connectors
Everything you need is available in the Elegoo Advanced Starter Kit.
The connection diagram is as follows:
The wiring diagram is as follows:
There are several ways to write the code using different iterative structures.
Code with for loops:
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 |
//definition of the PINs #define BLUE 3 #define GREEN 5 #define RED 6 void setup() { pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); digitalWrite(RED, HIGH); //Red LED ON digitalWrite(GREEN, LOW); //Green LED OFF digitalWrite(BLUE, LOW); //Blue LED OFF } //variables declaration int redValue; int greenValue; int blueValue; void loop() { #define delayTime 10 //Wait time redValue = 255; greenValue = 0; blueValue = 0; for(int i = 0; i < 255; i=i+1) { redValue = redValue - 1; greenValue = greenValue + 1; analogWrite(RED, redValue); analogWrite(GREEN, greenValue); delay(delayTime); } redValue = 0; greenValue = 255; blueValue = 0; for(int i = 0; i < 255; i += 1) { greenValue = greenValue - 1; blueValue = blueValue + 1; analogWrite(GREEN, greenValue); analogWrite(BLUE, blueValue); delay(delayTime); } redValue = 0; greenValue = 0; blueValue = 255; for(int i = 0; i < 255; i += 1) { blueValue = blueValue - 1; redValue = redValue + 1; analogWrite(BLUE, blueValue); analogWrite(RED, redValue); delay(delayTime); } } |
Code with the while() loop:
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 |
//definition of the PINs #define BLUE 3 #define GREEN 5 #define RED 6 void setup() { pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); digitalWrite(RED, HIGH); //Red LED ON digitalWrite(GREEN, LOW); //Green LED OFF digitalWrite(BLUE, LOW); //Blue LED OFF } //variables declaration int redValue; int greenValue; int blueValue; void loop() { #define delayTime 10 //Wait time int i = 0; redValue = 255; greenValue = 0; blueValue = 0; while (i < 255) { redValue = redValue - 1; greenValue = greenValue + 1; analogWrite(RED, redValue); analogWrite(GREEN, greenValue); delay(delayTime); i = i + 1; } redValue = 0; greenValue = 255; blueValue = 0; i = 0; while (i < 255) { greenValue = greenValue - 1; blueValue = blueValue + 1; analogWrite(GREEN, greenValue); analogWrite(BLUE, blueValue); delay(delayTime); i = i + 1; } redValue = 0; greenValue = 0; blueValue = 255; i = 0; while (i < 255) { blueValue = blueValue - 1; redValue = redValue + 1; analogWrite(BLUE, blueValue); analogWrite(RED, redValue); delay(delayTime); i = i + 1; } } |
Code with the do…while() loop:
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 |
//definition of the PINs #define BLUE 3 #define GREEN 5 #define RED 6 void setup() { pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); digitalWrite(RED, HIGH); //Red LED ON digitalWrite(GREEN, LOW); //Green LED OFF digitalWrite(BLUE, LOW); //Blue LED OFF } //variables declaration int redValue; int greenValue; int blueValue; void loop() { #define delayTime 10 //Wait time int i = 0; redValue = 255; greenValue = 0; blueValue = 0; do { redValue = redValue - 1; greenValue = greenValue + 1; analogWrite(RED, redValue); analogWrite(GREEN, greenValue); delay(delayTime); i = i + 1; } while (i < 255) redValue = 0; greenValue = 255; blueValue = 0; i = 0; do { greenValue = greenValue - 1; blueValue = blueValue + 1; analogWrite(GREEN, greenValue); analogWrite(BLUE, blueValue); delay(delayTime); i = i + 1; } while (i < 255) redValue = 0; greenValue = 0; blueValue = 255; i = 0; do { blueValue = blueValue - 1; redValue = redValue + 1; analogWrite(BLUE, blueValue); analogWrite(RED, redValue); delay(delayTime); i = i + 1; } while (i < 255) } |
Previous articles:
- Arduino IDE: for loops against while / do while #6
- Arduino IDE: while and do while loops #5
- Arduino IDE: turn on LEDs using a button (if) #4.1
- Arduino IDE: the conditional construction IF #4
- Arduino IDE: arithmetic and logical operators #3
- Arduino IDE: variables, constants and macros #2
- Arduino IDE: how can I use Arduino IDE to write a program? #1
- Arduino IDE: what it is and how it works #0
Follow us to keep yourself updated with the news!