Introduction: RGB LED Lighting Effects With Adafruit Trinket

About: I'm a software architect with an interest in Arduino, Raspberry Pi, electronics, robotics and generally making cool projects with my kids.


This instructable is to make programmable light effects using an RGB LED (this is an LED containing individual Red, Green and Blue elements) and the low cost Adafruit Trinket microcontroller. These lights can be used and re-used for multiple projects such as night lights, party lights and pumpkin lights for Halloween. The entire project only costs around $10 fully assembled.




Step 1: Preparation

Here are the components I used for this project:

1 x Adafruit Trinket (5V) (http://www.adafruit.com/products/1501)
1 x Adafruit Perma Proto Breadboard PCB (http://www.adafruit.com/products/589)
1 x Diffuse common-annode RGB LED (http://www.adafruit.com/products/302)
3 x 220 ohm resistor
1 x 9V battery connector
Some female headers that shipped with the trinket
Some male headers (http://www.adafruit.com/products/392)

Note that you can use a solderless breadboard instead of soldering this onto a PCB (and you should always do this first to make sure it works before committing to PCB). However, if you want to keep and reuse this project, I definitely recommend using a PCB. The great thing about the Adafruit breadboard PCB is that it is laid out exactly like a regular breadboard with lines of connected pads which makes soldering so much easier in my opinion.

Step 2: Solder the Female Headers Onto the PCB

Because we want to be able to remove the Trinket for programming (and maybe to use in other projects later on) we don't want to solder it directly onto the PCB so we used headers instead. We will solder male headers to the PCB and solder female headers to the trinket so that the trinket can easily be removed.

Insert the female headers on the right hand side of the board in rows D and G so they are spaced correctly to accept the male header pins that we will solder to the Trinket. To keep the board level on the table simply insert some female headers temporarily at the other end of the board but we won't be soldering those ones.

With the board level, go ahead and solder the 5 pins of each header to the PCB. I usually solder one on each side first to provide some support before soldering the rest of them.

Step 3: Soldering the Male Headers to the Trinket

The next step is to insert the male headers that came with the Trinket into the the female headers that are now attached to the PCB. The male headers have longer pins on one side and these longer pins should be inserted into the female headers.

Now the Trinket can be placed on top with the shorter pins holding it in place and the trinket can be soldered to these headers. I recommend using helping hands if you have them as shown in this photo.

Once this step is complete it should be possible to easily remove the trinket.

Step 4: Adding the RGB LED

Next we solder the RGB LED into place. The longest pin is the annode and the other three pins are the R, G and B leads. Make a note of where the annode pin is soldered. In my case it is the third pin from the left in column 4 on the PCB.

Step 5: Final Soldering Steps

The final soldering step is to add three resistors, four wires, and a battery connector as follows:

For each LED lead except the annode, solder a resistor to bridge the gap between the top and bottom half of the breadboard. Then solder a wire to connect the resistor to where the trinket's digital pins #0, #1 and #4 will be located when the trinket is connected. It is important to use these pins since these are the only PWM pins. In the photos below, the green wires are for this step.

Next, add a wire to connect the annode pin of the LED to the 5V pin of the trinket (the red wire in the photo).

Finally, add the battery connector with red connected to the Battery pin of the trinket and black connected to the GND pin.

Step 6: Writing the Code

We are now ready to write some code and upload it to the trinket. First of all follow the instructions on the Adafruit web site for setting up your Arduino IDE for use with the trinket.

The first part of the code defines the pins we want to use and provides functions for writing to the LEDs. The main loop simply cycles through a series of colors with a delay between each color change.

It is important to disconnect the trinket from the PCB when uploading code because digital pin #4 is shared with the USB port.

NOTE: After posting this code I found that there was an issue using PWM on pin 4. Adafruit just posted how to make code changes to work around this and I will update the code below when I get a chance. See this link for more information: https://learn.adafruit.com/introducing-trinket/programming-with-arduino-ide#analogwrite


int pinR = PB0; // Digital pin #0
int pinG = PB1; // Digital pin #1
int pinB = PB4; // Digital pin #4

// the setup routine runs once when you press reset:
void setup() {
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
}

void set(int rgb[]) {
set(rgb[0], rgb[1], rgb[2]);
}

void set(int r, int g, int b) {
// using common annode LED so low means ON!
analogWrite(pinR, 255-r);
analogWrite(pinG, 255-g);
analogWrite(pinB, 255-b);
}

void loop() {
int d = 100; // delay of 100ms means the lights will change 10 times per second
set(255, 0, 0);
delay(d);
set(128, 255, 0);
delay(d);
set(0, 255, 0);
delay(d);
set(0, 0, 255);
delay(d);
set(0, 255, 255);
delay(d);
set(255, 255, 0);
delay(d);
set(255, 0, 255);
delay(d);
set(255, 128, 0);
delay(d);
}

Step 7: Finished!

Once you reconnect the trinket after uploading the code you should see the LED cycling through colors after a few seconds.

Now you can experiment with other light patterns until you find the one you like.