Peter Talman
Published © GPL3+

ATtiny85 LED Flicker

Three PWM outputs to 3 x high brightness LEDs for a flame flicker effect.

IntermediateWork in progress2 hours2,038

Things used in this project

Hardware components

ATtiny85
Microchip ATtiny85
×1
High Brightness Red LED
×1
High Brightness Yellow/Orange LED
×2
BC548B Transistor
×3
Resistor 1k ohm
Resistor 1k ohm
×3
Resistor 47 ohm
×3

Software apps and online services

avrdude

Story

Read more

Schematics

Firelight Schematic

Circuit layout for the firelight

Flicker effect video

Video showing raw effect.

Flicker effect diffused

Video showing effect through cotton wool.

Code

ATTINY85 Three PWM control outputs

C/C++
Main code to run the LED sequence to simulate flicker
#define F_CPU 8000000UL
#define LED_DELAY 1 //Overall effect delay
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

int brightness1 = 0;
int brightness2 = 64;
int led_delay1 = 0;
int led_delay2 = 0;
char led_direction = 1;

int main(void) {
//Initialise code here

//Set Timer0 prescaler
  DDRB = 0x13; //PB0 = 1, PB1 = 2, PB2 = 4, PB3 = 8, PB4 = 10
  TCCR0A = (1 << COM0B1)|(1 << COM0A1)|(1 << WGM00); //PWM mode
  TCCR0B = (1 << CS01)|(1 << CS00); //div64
  OCR0A = 0; //PB0
  OCR0B = 0; //PB1
  
//Set Timer1 prescaler
  GTCCR = (1 << PWM1B)|(1 << COM1B0); //PWM mode
  TCCR1 = (1 << CS12)|(1 << CS11)|(1 << CS10); //div64
  OCR1B = 0; //PB4
 
//Main loop
	while (1) {
	    
		//led_delay parameter allow tweaking of each LED output
	    if(led_delay2==5){
	    	OCR0A = brightness1 + 89;
		    led_delay2 = 0;
	    }
	    
		//Scroll the red LED up and down across 150ms * (254 - 64) seconds
	    if(led_delay1==150){
            OCR0B = brightness2;
			
			if(led_direction){
				brightness2++;
			} else {
				brightness2--;
			}
			
			//Keep the red LED brightness between 64 and 254 (dim and bright, but not off)
		    if((brightness2<=64)||(brightness2>=254)){
				led_direction = !led_direction;
			}
			
            led_delay1 = 0;
	    }
	    
		brightness1 = brightness1 + 13;
		OCR1B = brightness1;
		
		_delay_ms(LED_DELAY);
		led_delay1++;
		led_delay2++;
	}

	return 1;
}

Credits

Peter Talman
1 project • 1 follower
Contact

Comments

Please log in or sign up to comment.