LED RGB and Neopixel with Arduino
What Will You Learn?
You will learn to program two different types of LEDs, an RGB and a neopixel, learning much more about electronics and programming.
Connect the RGB LED to Arduino
Connect the RGB LED to your Arduino microcontroller; It has 4 pins that will go in different lines of our breadboard, connect it as in image 2.
Also check the following tables for more details:
Second smallest pin
Second long pin
Program the RGB LED
Start programming the RGB Led, this means a red, green and blue LED, to be able to combine these three colors to produce more tones.
Define the variables at the top:
int redLed = 9;
int blueLed = 11;
int greenLed = 10;
And in the void setup section, write:
You just declared the variables: their name, where they are found and also the port type (output).
In the void loop section, write:
You have just programmed each of the LEDs to turn on, with a waiting time of 500 milliseconds. Verify your code, save it (LedRGB-Hello-World) and upload it to your Arduino, you will see how the Led turns color to color, and how the colors are combined.
Different On, Off, and Wait Combinations
Play with different combinations, on, off and wait, below is an example, copy and paste it in the void loop section:
Program the Neopixel
Now program the Neopixel, they are individually addressable types of RGB LEDs, to program it start by writing variables like the other LEDs:
int redNeopixel = 2;
int greenNeopixel = 3;
int blueNeopixel = 5;
Now in the “void setup” section we define our variables as output:
In the “void loop” section write:
A tip: to turn on your LED you write HIGH but you can also write 1 and to turn off you write LOW just like you can write 0.
With your code finished, verify it, save it, and upload it to your Arduino microcontroller with the buttons at the top. Cool! you can see it work.
function: is a section of a program that calculates a value independently from the rest of the program
void setup: The setup () function is called when a sketch begins. Use it to initialize variables, pin modes, start using libraries, etc. The setup () function will only run once, after every power-up or reboot of the Arduino board.
void loop: After creating a setup () function, which initializes and sets initial values, the loop () function does just what its name suggests and repeats consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.
int: Integers are your main data type for storing numbers.
pinMode: Sets the specified pin to behave as an input or an output.
digitalWrite: Write a HIGH or LOW value to a digital pin. Its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
delay: Pauses the program for the time (in milliseconds) specified as a parameter. (There are 1000 milliseconds in a second.)
Take it Further?
To learn more about arduino, following the MoonMakers lessons, and learn more about the fascinating world of programming and create your own projects