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.
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
- 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
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); } } }
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”.
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
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.
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.
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.
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.
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.
Done!
You should be able to plug in a 9 volt battery and get a nice RGB fading.