Introduction: Attiny RGB Mood Light

Picture of Attiny RGB Mood Light

Making an RGB led fader might seem simple, but shrinking it down to a tiny chip is extremely cool. The reason this is hard is because to fade leds we would normally use a PWM pin. The ATtiny however only has two PWM pins, since we want to use an RGB LED with three different channels, we would need three. To circumvent this problem we will have to use something called Software PWM. Software PWM is manually controlling the pins’ state to provide “simulated” fading.

Step 1: The Parts

Picture of The Parts

This project doesn't require too many parts. You will need the following:

- 1 Arduino
- 1 ATtiny45
- 3 RGB LED’s (Common Anode)
- 3 180 Ohm Resistors
- 1 8 pin DIP IC holder
- 1 7805 Voltage Regulator
- 1 9 Volt battery clip
- 1 Breadboard
- Some Jumper wires

Step 2: The Code

// attiny85 RGB LED rainbow fade with LDR
const int redPin = 2; 
const int grnPin = 1; 
const int bluPin = 0; 

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT); 
}

void loop()
{
redtoyellow();
yellowtogreen();
greentocyan();
cyantoblue();
bluetomajenta();
majenatored();
}
delay (30);
}

void redtoyellow()
{
digitalWrite(redPin, HIGH);
digitalWrite(bluPin, LOW);
// fade up green
for(byte i=1; i<100; i++) {
byte on = i;
byte off = 100-on;
for( byte a=0; a<100; a++ ) {
digitalWrite(grnPin, HIGH);
delayMicroseconds(on);
digitalWrite(grnPin, LOW);
delayMicroseconds(off);
}
}
}

void yellowtogreen()
{

digitalWrite(grnPin, HIGH);
digitalWrite(bluPin, LOW);
// fade down red
for(byte i=1; i<100; i++) {
byte on = 100-i;
byte off = i;
for( byte a=0; a<100; a++ ) {
digitalWrite(redPin, HIGH);
delayMicroseconds(on);
digitalWrite(redPin, LOW);
delayMicroseconds(off);
}
} 
}

void greentocyan()

{
digitalWrite(grnPin, HIGH);
digitalWrite(redPin, LOW);
// fade up blue
for(byte i=1; i<100; i++) {
byte on = i;
byte off = 100-on;
for( byte a=0; a<100; a++ ) {
digitalWrite(bluPin, HIGH);
delayMicroseconds(on);
digitalWrite(bluPin, LOW);
delayMicroseconds(off);
}
} 
}

void cyantoblue()

{
digitalWrite(bluPin, HIGH);
digitalWrite(redPin, LOW);
// fade down green
for(byte i=1; i<100; i++) {
byte on = 100-i;
byte off = i;
for( byte a=0; a<100; a++ ) {
digitalWrite(grnPin, HIGH);
delayMicroseconds(on);
digitalWrite(grnPin, LOW);
delayMicroseconds(off);
}
} 
}

void bluetomajenta()

{
digitalWrite(bluPin, HIGH);
digitalWrite(grnPin, LOW);
// fade up red
for(byte i=1; i<100; i++) {
byte on = i;
byte off = 100-on;
for( byte a=0; a<100; a++ ) {
digitalWrite(redPin, HIGH);
delayMicroseconds(on);
digitalWrite(redPin, LOW);
delayMicroseconds(off);
}
} 
}

void majenatored()

{
digitalWrite(redPin, HIGH);
digitalWrite(grnPin, LOW);
// fade down blue
for(byte i=1; i<100; i++) {
byte on = 100-i;
byte off = i;
for( byte a=0; a<100; a++ ) {
digitalWrite(bluPin, HIGH);
delayMicroseconds(on);
digitalWrite(bluPin, LOW);
delayMicroseconds(off);
}
} 
}

Step 3: Upload to the Arduino

Copy and paste the above code into your Arduino window, then upload it to your Arduino. Remember to check that you have the board set to Arduino Uno (Or whatever version you are using) To check that the programmer is set to program the arduino, click tools > board > Arduino Uno. Once you have the board connected, click the upload button in the top left corner of the screen. It should look like an arrow. Once the program is uploaded, you should get a message saying “done uploading”.

Step 4: Breadboard It

Picture of Breadboard It

Breadboard the circuit as shown in the picture. The Pins from the arduino go to each resistor then to the led. Don’t forget the connect the Anode side of the LED to 5 Volts

Step 5: Program the Tiny Chip

Picture of Program the Tiny Chip

To shrink things down we use a smaller IC. There are some pros and cons to doing this, mainly, it’s less expensive and smaller, but it has less pins we can use. This chip uses the same programming language as the Arduino so we can still use the same code.
To program the ATtiny we are going to use the Arduino as an ISP (In system programmer). In essence we are making the Arduino program the little chip.

The numbers on the inside of the IC are the hardware pin numbers. The numbers on the outside are the numbers we use when programming.
Connect Pin 10 of the Arduino to pin 1 on the ATtiny (Those are hardware numbers) Connect pin 11 of the Arduino to pin 5 on the ATtiny Connect pin 12 of the Arduino to pin 6 on the ATtiny Connect pin 13 of the Arduino to pin 7 on the ATtiny

First, plug just the Arduino into the computer, make sure the board is still set to UNO. Then click file > examples > Arduino as ISP. Upload the sketch to the Arduino.
Once the sketch is uploaded, add a 10uF electrolytic capacitor between the ground and reset pins of the Arduino. Remember to put the negative side of the capacitor to ground. Now, we can set up the IDE (Integrated Development Environment, or Arduino Programming Environment) to upload the RGB code. Click tools > programmer > Arduino as ISP. After you do that, we need to make sure that the code is setup for the ATtiny chip. Click tools > board > ATtiny45 (Or whatever ATtiny you are using) Once all of that is done you can upload the program. Note: You will get 2 errors saying “Please define pagel and BS2 signals” Don’t worry, that’s normal.

Step 6: Test the Tiny Chip

Picture of Test the Tiny Chip

We need to make sure that the code you have put on the Tiny chip is going to work. Connect the Chip to power and ground. Once you have power and ground hooked up, connect pins 0, 1, and 2 (Hardware Pins) to each of the three RGB cathode pins. (Make sure to use a ~180Ω resistor!) After that, connect the anode pin to the positive side of your breadboard.

Step 7: Make the Board

Picture of Make the Board

Now that we know it works, we want to make this into a more permanent circuit board. To do that we are going to use EagleCad. Open the attached file “RGB board” in Eagle. Print out the file on clay based paper, then iron or laminate it on to your copper board. It should look something like this once you have it ironed on.

Once you have it ironed on, place the copper board in an acid bath for 15 mins. Check the board every few minutes. If you keep it in too long, the traces may come off. Once all the copper is gone, you can wash the excess acid off your board, drill the holes and solder in all the parts.

Step 8: Done!

Picture of Done!

You should be able to plug in a 9 volt battery and get a nice RGB fading.

Comments

pascal_stv made it! (author)2016-05-30

Attiny25 ok

Suppression ligne 21 -> 22 delais(30) }

pascal_stv (author)pascal_stv2016-05-30

thanks for 1st instructable Works well

nomuse (author)2015-08-20

Nice.

You can increase the PWM frequency even higher by using direct register writes instead of the Arduino digitalWrite() macro; the latter takes something like 27 clock cycles to run!

Aka "PORTA |= (1 << 3);" and so forth.

AdamF5 (author)2015-03-10

Thanks so much. Upgrade for the kids lamp!

daniel32145 (author)AdamF52015-03-10

Good to know you liked it!

diy_bloke (author)2015-03-01

I left this message somewhere else as well, but it might come in handy here as well:
Though you are right on the software PWM, there is some discussion on wether yje Attiny85 has 2 or 3 PWM outputs.
Supposedly it has 3, all addressable from inside the IDE
IC leg 6 (PB1),
IC leg 5 (PB0),
IC leg 3 (PB4),
Supposedly, strictly speaking IC leg 2 (D3/PB3) is also capable of PWM, but internally it shares the same timer used on leg 3.
I didnt try it myself, but that is what I have been reading. Some discussion going on here:

http://forum.arduino.cc/index.php?topic=134754.0

siliconghost (author)2015-02-28

I don't see the eagle file for the PCB anywhere on this Instructable. Is it just me?

dudes (author)2014-11-04

This worked perfectly on the Uno board, but when I loaded it to the ATTiny(85 not 45), the cycle became extremely slow. Is there a way to adjust the speed of the color change?

daniel32145 (author)dudes2014-11-04

You can change the speed by changing this line of code "delay (30);" in the void loop. Reducing the delay will speed up the RGB fading. The reason it goes slower on an ATtiny, is because the tiny chips run at 8Mhz instead of 16Mhz, which the Uno board runs at.

dudes (author)daniel321452014-11-04

Ok I tried changing the delay time, all the way down to 1 and it's not any faster than when it was at 30.

daniel32145 (author)dudes2014-11-04

I just realized, there's a typo in the code in the example, make sure you remove the bracket before "delay". It's an extra bracket and it's not needed. Try that, see if it helps.

dudes (author)daniel321452014-11-04

It didn't seem to do anything, I also tried setting the delay to 1 microsecond (delayMicroseconds()) just to see if it would do anything. Nothing. It is fading like it should, just really slow, as if the delay were set at around 40-50 milliseconds.

dudes (author)dudes2014-11-04

I also tried simply removing the delay altogether, again no effect. So now I'm really confused.

HDL_CinC_Dragon (author)dudes2014-12-23

That delay will only affect how long it takes to go from "majentatored()" to "redtoyellow()" in the "loop()" method. The speed at which it fades through the colors is inside each of the fading methods.

Page 272 of The Arduino Cookbook has an AWESOME RGB fader code sample. You will have to change the pin numbers in the code but that is it. I'm using this on a Mega2560 for testing and using an ATtiny13A for the final product. On the Mega2560 the fade is absolutely flawless. On the Tiny13, which has one of the lowest RAM counts of them all, it seems to hang anytime the color codes get too high. I wish I had some 45's or 85's to try it on >_<

I hope this helped anyone who ends up here with a similar issue.

I forgot to mention you can look up the Arduino Cookbook for free on Google Books. Just search for "Arduino Cookbook" on www.books.google.com

thanks for the tip. Sadly there is no such code on page 272. But it might be from a different issue. Not the entire book can be seen though. Pages have been deleted on Google

Oh wow, I meant to say it's on page 227 -_-. My copy of the cookbook has the code I mentioned on page 227 but the current Google Books copy has the code starting on page 229. Section 7 of the cookbook is all about visual output and 7.4 is about sweeping an RGB using hue based math. It really is worth checking out!

thanks i will check it :-)

About This Instructable

24,452views

103favorites

More by daniel32145:Attiny RGB Mood Light
Add instructable to: