8-pin AVR based Mood Lamp

I got a sample of a RGB LED (Red Green Blue Light Emitting Diode). So, I decided to make something fun out of it. Using an 8-pin AVR microcontroller, ATtiny45, I made a simple prototype to control the RGB LED using PWM or Pulse Width Modulation.

Then, I put the prototype inside a translucent candle vase. The vase diffused the light from the RGB very nicely and below is the video of the result.

I soldered a potentiometer to ATtiny45 to provide an input to its on-chip ADC or analog-to-digital converter. The rate of color transitions of the RGB LED can, then, be controlled by adjusting the potentiometer.

The following are the pictures of the prototype. No printed circuit boards were used to simplify the project. The project is powered by two AA batteries.

Below is the schematic of the prototype.

The following is the source code for the project. The compiler that I used is WinAVR.

#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>

int main(void)
{ unsigned char a=0,b=0,c=0,aa=0,bb=0,cc=0,temp;
 DDRB=0xFF;
 PORTB=0xFF;

//initialize ADC
 ADMUX=0b00100011;
 ADCSRA=0b10000100;

//Set OC0A on Compare Match, clear OC0A at BOTTOM
 TCCR0A|=(1<<COM0A1); 
 TCCR0A|=(1<<COM0A0); 
 //Set OC0B on Compare Match, clear OC0B at BOTTOM
 TCCR0A|=(1<<COM0B1); 
 TCCR0A|=(1<<COM0B0);
 //Fast PWM, TOP=0xFF, Update of OCRx at BOTTOM
 TCCR0A|=(1<<WGM01);     
 TCCR0A|=(1<<WGM00);
 //clkI/O/(No prescaling)
 TCCR0B&=~(1<<WGM02); 
 TCCR0B&=~(1<<CS02);  
 TCCR0B&=~(1<<CS01);
 TCCR0B|=(1<<CS00);

OCR0A=0×00;
 OCR0B=0×00;

//PWM1B: Pulse Width Modulator B Enable
 GTCCR|=(1<<PWM1B);
 //OC1x Set on compare match. Cleared when TCNT1= $00.
 GTCCR&=~(1<<COM1B1);
 GTCCR|=(1<<COM1B0);
 //clock select bits
 TCCR1&=~(1<<CS13);
 TCCR1|=(1<<CS12);
 TCCR1&=~(1<<CS11);
 TCCR1|=(1<<CS10);

//OCR1B=0xFF;
 //OCR1C=0xFF;

while(1)
 { ADCSRA |= (1<<ADSC);
  while((ADCSRA&0×10)==0×00);
  temp=ADCH;

OCR0A=c;
  OCR0B=b;
  OCR1B=~a;

while(temp>0)
  { temp–;
   _delay_ms(1);
  }

if(aa==0)
  { a=a+1;
   if(a==0xFF)
   aa=1;
  }

  if(aa==1)
  { a=a-1;
   if(a==0)
   aa=0;
  }

if(bb==0)
  { b=b+3;
   if(b==0xFF)
    bb=1;
  }

  if(bb==1)
  { b=b-3;
   if(b==0)
    bb=0;
  }

if(cc==0)
  { c=c+5;
   if(c==0xFF)
    cc=1;
  }

  if(cc==1)
  { c=c-5;
   if(c==0)
    cc=0;
  }
 }
}

Speak Your Mind

*