The RGB Flasher is an awesome Arduino microcontroller gadget that displays three colors (red, green, and blue) on one LED. You can easily build the circuit on the MakerShield, which will make it portable so you can carry it in your shirt pocket. You can use either the Fritzing diagram shown in Figure 9-4 or the circuit schematic diagram of Figure 9-5 to build the flasher.
Figure 9-4. The RGB Flasher Fritzing diagram
Figure 9-5. The RGB Flasher circuit schematic diagram
TECH NOTE
The common anode pin is the longest lead on the RGB LED.
After wiring the components onto the mini breadboard of the MakerShield pictured in Figure 9-6, upload Example 9-1 to the Arduino microcontroller.
Figure 9-6. The RGB Flasher built on a MakerShield
Example 9-1. The RGB Flasher sketch
/*
RGB Flasher
Flashes the red, green, and blue LEDs of an RGB LED
Turns on an LED on for one second, then off for one second for each
color LED.
15 Feb 2013
Don Wilcher
*/
// RGB pins wired to the Arduino microcontroller.
// give them names:
int redled = 9;
int grnled = 10;
int bluled = 11;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pins as outputs:
pinMode(redled, OUTPUT);
pinMode(grnled, OUTPUT);
pinMode(bluled, OUTPUT);
// turn RGB outputs off:
digitalWrite(redled, HIGH);
digitalWrite(grnled, HIGH);
digitalWrite(bluled, HIGH);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(redled, LOW); // turn the red LED on
delay(1000); // wait for a second
digitalWrite(redled, HIGH); // turn the LED off
delay(1000); // wait for a second
digitalWrite(grnled, LOW); // turn the green LED on
delay(1000); // wait for a second
digitalWrite(grnled, HIGH); // turn the green LED off
delay(1000); // wait for a second
digitalWrite(bluled, LOW); // turn the blue LED on
delay(1000); // wait for a second
digitalWrite(bluled, HIGH); // turn the blue LED off
delay(1000); // wait for a second
}
NOTE
You have been taking an LED pin HIGH to light it. This example takes a pin LOW to light it. This is because the common pin on the RGB LED goes to +5V and each element’s pin (R, G, and B) is a negative lead. As a result, each of those pins needs to be taken LOW to allow current to flow. This means that taking a pin HIGH turns it off. This is the reverse of what you’ve seen with discrete LEDs was there is one positive and one negative lead. In the case of this RGB LED, there is one positive lead (the common anode) and three negative leads (cathodes).
After uploading the RGB Flasher sketch to the Arduino Microcontroller, the red, green, and blue LEDs will be individually flashing in sequence. You can change the order of the LEDs by making new sketch RGB pin assignments along with appropriate breadboard wiring changes. Like a good scientist, remember to record your observations, modified sketches, and circuit schematic diagrams in your lab notebook!
TROUBLESHOOTING TIP
If the LEDs don’t turn on in the proper sequence, check your sketch pin assignments, as well as the orientation of the component on the MakerShield mini breadboard.
Leave a Reply