Program RGB LEDs with Arduino
Wondering how to wire up and program an RGB LED?
We will describe setting up the circuit, talk about the LED and discuss the code used to adjust the hue.
Important Note: Make sure to add a current-limiting resistors for each LED color (~220 ohm) when you hooking up your RGB LED.
Careful, viewers: The schematic omits the series resistors (which were included on the breadboard.)
Weird! I followed everything to the dot. However, connecting the ground pin to the ground lead of the RGB LED doesnt turn on the LED, but not connecting the ground pin to one of the leads of R,G turns the LED On.. I suppose the RGB LED is broken?
why did’t you declared the variable in the setup and initialized it with 0. This way the program won’t declare a variable every time in the loop.
The reason I chose to do that is for variable scope. If a variable is declared in the setup(), it can only be used within setup().
If a variable is declared outside of setup() and loop(), it can be used (changed and referred to) anywhere in the program, this property is called global scope. In most small Arduino programs, using variables with global scope is not that big of an issue, however, as program size and complexity grows, using variables of global scope can increasingly become hard to debug. By declaring the variable inside loop(), I am limiting its scope to loop(). It is pretty trivial in this example, because the program is so small, but that is how I chose to do it. I hope this helps answer your question.
Interesting to know analogWrite can be applied to digital pins 3 , 5 and 6