45-60 min
In Progress
Lesson 1 of 0
In Progress

LED RGB and Neopixel with Arduino

45-60 min

Intermediate

14-18

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

Step 1

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.

Step 2

Also check the following tables for more details:

LED RGB

Arduino 

Second smallest pin

D9

Larger pin

GND

Second long pin

D10

Smaller pin

D11

Neopixel

Arduino 

V

3.3V

R

D2

B

D5

G

D3

Program the RGB LED

Step 3

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:

   pinMode(redLed, OUTPUT);

   pinMode(blueLed, OUTPUT);

   pinMode(greenLed, OUTPUT);

You just declared the variables: their name, where they are found and also the port type (output).

Step 4

In the void loop section, write:

   digitalWrite(redLed, HIGH);

   delay(500);

   digitalWrite(blueLed, HIGH);

   delay(500);

   digitalWrite(greenLed, HIGH);

   delay(500);

  “

Step 5

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

Step 6

Play with different combinations, on, off and wait, below is an example, copy and paste it in the void loop section:

   digitalWrite(redLed, HIGH);

   digitalWrite(blueLed, LOW);

   digitalWrite(greenLed, LOW);

   delay(500);

   digitalWrite(blueLed, HIGH);

   digitalWrite(redLed, LOW);

   digitalWrite(greenLed, LOW);

   delay(500);

   digitalWrite(greenLed, HIGH);

   digitalWrite(redLed, LOW);

   digitalWrite(blueLed, LOW);

   delay(500);

Program the Neopixel

Step 7

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;

Step 8

Now in the “void setup” section we define our variables as output:

  pinMode(redNeopixel, OUTPUT);

  pinMode(greenNeopixel, OUTPUT);

  pinMode(blueNeopixel, OUTPUT);

Step 9

In the “void loop” section write:

  digitalWrite(redNeopixel, HIGH);

  digitalWrite(blueNeopixel, LOW);

  digitalWrite(greenNeopixel, LOW);

  delay(500);

  digitalWrite(blueNeopixel, HIGH);

  digitalWrite(redNeopixel, LOW);

  digitalWrite(greenNeopixel, LOW);

  delay(500);

  digitalWrite(greenNeopixel, HIGH);

  digitalWrite(redNeopixel, LOW);

  digitalWrite(blueNeopixel, LOW);

  delay(500);

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.

Step 10

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.

Further Resources

Vocabulary

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.)

Learn More!

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

Diego Francisco June 4, 2021
Soy Maker de tiempo completo e inquieto en saber como funcionan las cosas leal amigo del conocimiento I am a full-time Maker and restless in knowing how things work, loyal friend of knowledge.
Print Project
FEEDBACK