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.

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.
<p>Any ideas as to how I can slow down the rate at which the colors cycle? Love this project, by the way, you're awesome!</p>
<p>OK, finally got around to build it and found the light changes extremely slow (on 8MHz). Which core are you using?</p>
<p>found it. forgot to set the fuses for 8MHz</p>
Nice work buddy
<p>How can I make this without the sensor? I just want it to run constantly. Thank you!</p>
<p>you could take the IF...ELSE loop out of the program.<br>If you do not know how to do that, well you could replace the sensor by a wire bridge or leave it open, depending on how it is wird<br>In the program you see that if the value read from the sensor is greater or equal than 201, the lights shld go out.<br>As I presume the lights shld go out when it is light ( low resistance of LDR) and on when it is dark (high resistance of LDR), I guess just omitting the sensor would do the trick.<br></p>
I did get it, and after a little learning it was very easy to run without any other components. Forgive me I just started learning on Christmas, and thank you!
<p>we all have to start learning at one point :-)</p>
Where can i get another version of it? i don't use Adruino IDE, since i program with parralel port
<p>ide supports parallel programmer</p>
<p>okay... i couldn't get it working :/</p>
<p>I do not know why that is, the only thing I know is that the IDE supports a parallel programmer. If you cant get it to work you have to upload the hex file: </p><p>http://forum.arduino.cc/index.php?topic=114503.0</p>
<p>Great article.<br>Though you are right on the software PWM, there is some discussion on wether it has 2 or 3 PWM outputs.<br>Supposedly it has 3, all addressable from inside the IDE<br>IC leg 6 (PB1),<br>IC leg 5 (PB0), <br>IC leg 3 (PB4), <br>Supposedly, strictly speaking IC leg 2 (D3/PB3) is also capable of PWM, but internally it shares the same timer used on leg 3.<br>I didnt try it myself, but that is what I have been reading. Some discussion going on here: </p><p>http://forum.arduino.cc/index.php?topic=134754.0</p>
<p>a special thanks to you https://www.youtube.com/watch?v=rupib_N7zqw</p>
<p>other tutorial great how to install attiny 0022 with library http://makezine.com/2011/10/10/how-to-shrinkify-your-arduino-projects/ </p>
<p>Which LDR was used for this?</p>
<p>Nice Instructable. Thank you for the work. It was fun to build. </p>
<p>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().</p>
<p>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... :/</p>
<p>This is the ONLY (current) reason I wanted to program an ATtiny anyway xD</p>
Hi <br>This is my first go around with ATtiny85, other than the blink sketch. So i thank you for this project. <br> <br>Is all of the transitions in colors smooth? <br>I get a blink from red to green, or a fast switch. Not a smooth transition as all the other colors. <br>Is this the nature of this sketch or have i thrown a monkey wrench in here somewhere? <br> <br>Thanks <br> <br>W
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.
Now that i investigate it more looks like it's the LED i have doing that.
<p>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.</p>
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? <br> <br>Thanks <br>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: <br>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?
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.
<p>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.</p>
go where to find out how to connect to the arduino?
<p>Go here to see how to program a ATtiny45/85 with an Arduino </p><p>https://www.youtube.com/watch?v=30rPt802n1k</p>
<p>&quot;Go here and it will show you how to connect the ATtiny85 to the Arduino and program it.&quot;</p><p>Go where???</p>
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. <br> <br>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. <br> <br>Just make sure you use the right resistor. :)
<strong>I've improved this code. New features include: </strong><br> <br> Easier to read and understand<br> Generalized fadeUp and fadeDown functions<br> Arbitrary speed control<br> Easy control of light threshold<br> <br> <strong>Here is the new code:</strong><br> <br> <em>const int redPin = 2;<br> const int greenPin = 1;<br> const int bluePin = 0;<br> const int sensor = 3;<br> int lightThresh = 550; // Light threshold. 0 - 1023. Lower number = activation at lower light level.<br> int time = 50; // Speed control. Higher number = slower fades.<br> <br> <br> void setup()<br> {<br> &nbsp; pinMode(redPin, OUTPUT);&nbsp;&nbsp;&nbsp;<br> &nbsp; pinMode(greenPin, OUTPUT);&nbsp;&nbsp;&nbsp;<br> &nbsp; pinMode(bluePin, OUTPUT);<br> &nbsp; pinMode(sensor, INPUT);<br> }<br> //fadeUp &amp; fadeDown syntax: (color to fade, color to turn on, color to turn off)<br> void loop() {<br> &nbsp; if (analogRead(sensor) &lt;= lightThresh)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; fadeUp(greenPin, redPin, bluePin);&nbsp;&nbsp; //red to yellow<br> &nbsp;&nbsp;&nbsp; fadeDown(redPin, greenPin, bluePin); //yellow to green<br> &nbsp;&nbsp;&nbsp; fadeUp(bluePin, greenPin, redPin);&nbsp;&nbsp; //green to cyan<br> &nbsp;&nbsp;&nbsp; fadeDown(greenPin, bluePin, redPin); //cyan to blue<br> &nbsp;&nbsp;&nbsp; fadeUp(redPin, bluePin, greenPin);&nbsp;&nbsp; //blue to purple<br> &nbsp;&nbsp;&nbsp; fadeDown(bluePin, redPin, greenPin); //purple to red<br> &nbsp; }<br> &nbsp; else if (analogRead(sensor) &gt; lightThresh)<br> &nbsp; {<br> &nbsp;&nbsp;&nbsp; //turn all colors off<br> &nbsp;&nbsp;&nbsp; digitalWrite(redPin, LOW);<br> &nbsp;&nbsp;&nbsp; digitalWrite(greenPin, LOW);<br> &nbsp;&nbsp;&nbsp; digitalWrite(bluePin, LOW);<br> &nbsp; }<br> }<br> <br> void fadeUp(int fadePin, int onPin, int offPin) {<br> &nbsp; //set constant colors<br> &nbsp; digitalWrite(onPin, HIGH);<br> &nbsp; digitalWrite(offPin, LOW);<br> &nbsp; //set current brightness out of 1000<br> &nbsp; for(int bright = 1; bright &lt; 1000; bright = bright + 10) {<br> &nbsp;&nbsp;&nbsp; //set PWM lengths<br> &nbsp;&nbsp;&nbsp; int on = bright;<br> &nbsp;&nbsp;&nbsp; int off = 1000 - bright;<br> &nbsp;&nbsp;&nbsp; //software PWM for 'time' ms<br> &nbsp;&nbsp;&nbsp; for(int run = 0; run &lt; time; run++) {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(fadePin, HIGH);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delayMicroseconds(on);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(fadePin, LOW);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delayMicroseconds(off);<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp; }<br> }<br> <br> void fadeDown(int fadePin, int onPin, int offPin) {<br> &nbsp; //set constant colors<br> &nbsp; digitalWrite(onPin, HIGH);<br> &nbsp; digitalWrite(offPin, LOW);<br> &nbsp; //set current brightness out of 1000<br> &nbsp; for(int bright = 1; bright &lt; 1000; bright = bright + 10) {<br> &nbsp;&nbsp;&nbsp; //set inverse PWM lengths<br> &nbsp;&nbsp;&nbsp; int on = 1000 - bright;<br> &nbsp;&nbsp;&nbsp; int off = bright;<br> &nbsp;&nbsp;&nbsp; //software PWM for 'time' ms<br> &nbsp;&nbsp;&nbsp; for(int run = 0; run &lt; time; run++) {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(fadePin, HIGH);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delayMicroseconds(on);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(fadePin, LOW);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delayMicroseconds(off);<br> &nbsp;&nbsp;&nbsp; }<br> &nbsp; }<br> }</em><br> <br> <strong>I would&nbsp;recommend&nbsp;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&nbsp;isn't&nbsp;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 <a href="http://ediy.com.my/index.php/tutorials/item/49-change-the-tiny-arduino-between-1mhz-and-8mhz" rel="nofollow">here</a> to see how to do it.</strong>
@mikroskeem once you click &quot;Upload&quot; on the arduino software, it'll fail to upload to the atmega, so then click Start &gt; Run &gt; %temp% <br> <br>Look for a folder called something like build7137078911822462892.tmp the newest one in your system. Then find inside the file SketchName.cpp.hex <br> <br>That is the compiled program. Simply upload that hex file using avrdude.
You can use a Common Anode version of the RBG LED -&gt; 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 -&gt; LOW and LOW-&gt;HIGH. I just switched the &quot;off&quot; part to HIGH's, as I didn't care if the code was technically wrong (where you state/label which color is being changed).
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 &amp; 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 &quot;Arduino-Tiny&quot; 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! <br>
Where do you get it?
Here: http://code.google.com/p/arduino-tiny/downloads/detail?name=arduino-tiny-0100-0015.zip
Thanks! <br>
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.
Thanks! You can get the '85s for about 80c each at Digi-key or Mouser if you didn't already know :)

About This Instructable




Bio: I'm a big Arduino fan. I'm always looking for new and exciting things to do with electronics and Arduino. I also like video ... More »
More by xBacon:DIY Infrared Proximity Sensor (Arduino Compatible) Color Changing LED Tree Plug-in LED Color Fading Night Light 
Add instructable to: