Instructables

ATtiny85 Mini RGB Mood Light!

Picture of ATtiny85 Mini RGB Mood Light!
IMG_1332.JPG
IMG_1331.JPG
IMG_1333.JPG
IMG_1334.JPG
I have seen RGB mood lights using Arduino, PIC, and larger AVR chips, but never one with the ATtiny85. That's why I decided to make one.

This mood light is super simple to make and all the parts can be purchased online for about $5.00 total (not including the Arduino used to program the chip).

This is a contest entry in the LED contest with Elemental LED and the Hurricane Lasers contest, so if you like it, vote!

EDIT: I have changed this instructable so that it is a NIGHT LED Mood Lamp. It now only lights up when the ambient light in the room is very low (at night with all the lights off). You do need an LDR and a 10K ohm resistor to add this part though. If you want it to not have this feature, just remove the LDR/10K resistor part and connect Analog Input 3 of the ATtiny (pin 2) straight to ground.
 
Remove these adsRemove these ads by Signing Up

Step 1: How does it work?

The ATtiny85 only has 2 PWM pins, so obviously you can't make all 3 colors of the RGB LED fade in and out smoothly, right? Wrong.

The way I got around this was by using software PWM. This means that I can fade in and out of all 3 colors using any of the pins on the ATtiny.

How software PWM works is by setting the pin HIGH and then LOW at different rates so that the LED looks like it's dimming. This is called Persistence of Vision or POV (for more on POV, see my instructable here). The LED blinks so rapidly that the human eye can't detect that it is flashing at all, and it sees instead that the LED appears to be dimming.

actually, I built this and ran it using my original code from my arduino sketch, apparently there is a hush hush third PWM that's buggy but functional on the AtTiny, and the code eliminates functions and uses for().

agrady19953 months ago

Thank you SO MUCH!!! I was trying to figure out which ATtiny to get, but it looks like it will work with this! now all I need is to be able to program it with my stupid Arduino 2560.. It's not working so far.. I think i need to add the .1uF cap to ground... :/

This is the ONLY (current) reason I wanted to program an ATtiny anyway xD

WWC1 year ago
Hi
This is my first go around with ATtiny85, other than the blink sketch. So i thank you for this project.

Is all of the transitions in colors smooth?
I get a blink from red to green, or a fast switch. Not a smooth transition as all the other colors.
Is this the nature of this sketch or have i thrown a monkey wrench in here somewhere?

Thanks

W
xBacon (author)  WWC1 year ago
Yes, the transitions of the colors should be smooth. Between red and green, it should slowly fade yellow. Try re uploading the sketch and checking your wiring.
WWC xBacon1 year ago
Now that i investigate it more looks like it's the LED i have doing that.
siliconghost WWC3 months ago

I had a similar problem and it ended up being because I still had the AtTiny85 running at 1Mhz. You need to go through the process of burning the boot loader at 8Mhz before it will be smooth.

WWC xBacon1 year ago
I had 2 RGB's in a row that were not switching smoothly. All the other RGB's i have tried are fine. Whats the chances of 2 defective in a row out of 50?

Thanks
W
Great able! Just got my shipment of my ATtiny45's (they were out of 85's, and I didn't want to pay $10 shipping @ other site) I have a problem:
I have common anode(seriously? Who thought of that?)RGB LEDS. Can I just change something in the code, or do I have to use transistors?
xBacon (author)  waterlubber1 year ago
I think you can just connect the common anode with resistors to +5V, and change all the ++ things in the 'for' statements to --. Tell me if this works.

I had one of these too. All you have to do is connect the LED to +5V instead of GND. I didn't have to change anything else in the code.

codongolev1 year ago
go where to find out how to connect to the arduino?

Go here to see how to program a ATtiny45/85 with an Arduino

https://www.youtube.com/watch?v=30rPt802n1k

Mike61584 months ago

"Go here and it will show you how to connect the ATtiny85 to the Arduino and program it."

Go where???

You can replace the 3 resistors hooked up to the LED anodes with only one. Just connect the LED anodes directly to the microcontroller, and put one 220 ohm (or appropriate) resistor between the common cathodes and ground.

Be warned that some RGB LEDs need a different value resistor on the red anode than the blue and green ones. In that case, you would have to use 3 resistors on the anodes. In this case, since all 3 colors apparently use the same value resistors, you can put one of that value on the cathodes and still have the same results.

Just make sure you use the right resistor. :)
I've improved this code. New features include:

Easier to read and understand
Generalized fadeUp and fadeDown functions
Arbitrary speed control
Easy control of light threshold

Here is the new code:

const int redPin = 2;
const int greenPin = 1;
const int bluePin = 0;
const int sensor = 3;
int lightThresh = 550; // Light threshold. 0 - 1023. Lower number = activation at lower light level.
int time = 50; // Speed control. Higher number = slower fades.


void setup()
{
  pinMode(redPin, OUTPUT);   
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin, OUTPUT);
  pinMode(sensor, INPUT);
}
//fadeUp & fadeDown syntax: (color to fade, color to turn on, color to turn off)
void loop() {
  if (analogRead(sensor) <= lightThresh)
  {
    fadeUp(greenPin, redPin, bluePin);   //red to yellow
    fadeDown(redPin, greenPin, bluePin); //yellow to green
    fadeUp(bluePin, greenPin, redPin);   //green to cyan
    fadeDown(greenPin, bluePin, redPin); //cyan to blue
    fadeUp(redPin, bluePin, greenPin);   //blue to purple
    fadeDown(bluePin, redPin, greenPin); //purple to red
  }
  else if (analogRead(sensor) > lightThresh)
  {
    //turn all colors off
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
  }
}

void fadeUp(int fadePin, int onPin, int offPin) {
  //set constant colors
  digitalWrite(onPin, HIGH);
  digitalWrite(offPin, LOW);
  //set current brightness out of 1000
  for(int bright = 1; bright < 1000; bright = bright + 10) {
    //set PWM lengths
    int on = bright;
    int off = 1000 - bright;
    //software PWM for 'time' ms
    for(int run = 0; run < time; run++) {
      digitalWrite(fadePin, HIGH);
      delayMicroseconds(on);
      digitalWrite(fadePin, LOW);
      delayMicroseconds(off);
    }
  }
}

void fadeDown(int fadePin, int onPin, int offPin) {
  //set constant colors
  digitalWrite(onPin, HIGH);
  digitalWrite(offPin, LOW);
  //set current brightness out of 1000
  for(int bright = 1; bright < 1000; bright = bright + 10) {
    //set inverse PWM lengths
    int on = 1000 - bright;
    int off = bright;
    //software PWM for 'time' ms
    for(int run = 0; run < time; run++) {
      digitalWrite(fadePin, HIGH);
      delayMicroseconds(on);
      digitalWrite(fadePin, LOW);
      delayMicroseconds(off);
    }
  }
}


I would recommend setting your internal ATtiny85 clock to 8 MHz, instead of the default 1 MHz. This is optional, but it provides you with more accurate control of the fade time, as the chip isn't limited by its slow clock speed. This is easy to do within the Arduino programming environment, and it just as easily reversed. However, it does take more power, and will drain a battery faster. I am running mine on USB 5V, so it isn't a concern. If you are interested, just go here to see how to do it.
PowerCat1 year ago
@mikroskeem once you click "Upload" on the arduino software, it'll fail to upload to the atmega, so then click Start > Run > %temp%

Look for a folder called something like build7137078911822462892.tmp the newest one in your system. Then find inside the file SketchName.cpp.hex

That is the compiled program. Simply upload that hex file using avrdude.
mikroskeem1 year ago
Where can i get another version of it? i don't use Adruino IDE, since i program with parralel port
Ploopy1 year ago
Cool
marcusone1 year ago
You can use a Common Anode version of the RBG LED -> Actually you just connect the common to +3v via 1 resistor (so saves you 2 resistors this way). Then in the code switch all the HIGH -> LOW and LOW->HIGH. I just switched the "off" part to HIGH's, as I didn't care if the code was technically wrong (where you state/label which color is being changed).
cowlick31 year ago
For the longest time I believed this myth that the ATTiny85 only has 2 PWM pins as well. Until I started testing the pins myself and found a 3rd one, so there are at least 3 (PB0, PB1 & PB4) which you can PWM with simple analogWrite() calls. I've tested smooth HSV fades with an RGB LED and it works perfectly. It even works when using the slower 1MHz clock without any noticeable flicker (and lower power consumption). It could have something to do with the core that you use. I think I'm using the "Arduino-Tiny" core.
you can actually squeeze 5 out of it :) check this out http://www.kobakant.at/DIY/?p=3393
Wait a sec... analogWrite? I thought you had to use xBacons method. Cool!
Where do you get it?
xBacon (author)  waterlubber1 year ago
Here: http://code.google.com/p/arduino-tiny/downloads/detail?name=arduino-tiny-0100-0015.zip
Thanks!
xBacon (author)  cowlick31 year ago
Ok, I will try out that core. Thanks!
nice work getting around the PWM issue! I'd really like to use an ATtiny for a project soon.
xBacon (author)  amandaghassaei1 year ago
Thanks! You can get the '85s for about 80c each at Digi-key or Mouser if you didn't already know :)