arduino‎ > ‎

lesson2multicolored

Multicolor LED Tutorial

In this tutorial you'll create a simple circuit that can change the color of a light using a button. Below you'll find a diagram and link to the code.


Set up

  1. Open your Arduino software on your laptop

  2. Use a USB wire to connect your Arduino board to your computer

  3. Select the Arduino under Tools > Port


Schematic

When you setup the Arduino it's very important that all the wires go into the same spots as shown below. Remember that LEDs have a long side and a short side. For this LED the longest wire should go to ground.


RGB LEDs

In this project we're using a new type of led. In the past a LED was one color, but now this LED can show three colors. Each wire (except the longest) turns on a different color.


Code

With this new LED use the code below to program your arduino! Simply copy this file into your Arduino editor and upload it!





void setup() {

pinMode(7, OUTPUT);

pinMode(8, OUTPUT);

pinMode(4, INPUT);



digitalWrite(4, HIGH);

}


////////////////////////////////////////////////////////////

void loop() {

// Decides if the button is being pressed or not.

if (digitalRead(4) == LOW){

  // Make the LED green.

  digitalWrite(7, HIGH);

  digitalWrite(8, LOW);

}else{

  // Make the LED blue.

  digitalWrite(7, LOW);

  digitalWrite(8, HIGH);

}

}



Stretch Goals:


  1. Can you get the LED to change between red and blue? Hint: The answer doesn’t require changing and code.

  2. Can you make it so the LED rotates between 2 colors every second?

  3. Can you make the color purple?

  4. Can you make it so the LED rotates between 3 colors every second?

  5. Go crazy! How many LEDs can you get on a board? Get them flashing at different intervals and colors.  Make sure you use resistors so you don’t fry the LEDs.

  6. Use a button to turn all the LEDs on or off.