Fading LED with AtTiny85 + Watchdog Timer

Hello everyone,

I’m creating a light installation with 100 “breathing” LEDs to simulate fireflies (slowly fading in and out). For each firefly I’m using an AtTiny85 with a 3V battery, LED and resistor soldered together. I’ve had good success with the following code to fade in and out the LEDs in a slow and steady mode:

int ledPin = 0; 
void setup() {
void loop() {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=2) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=2) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect

Here is my ask: I’m worried about battery life. In my tests, the battery died after 3-4 days, but I’d like to get the installation going for about 2 weeks without having to exchange the battery.

I came across various posts referring to a watchdog timer - it seems like this could be a good solution for my problem. Unfortunately this is a topic that is very unknown to me and I was hoping someone here could help me with the programming.

For reference, I found the following code while doing research - however, I’ve been unable to change the code from a “flashing” LED to a slow fade in and out. I’m not determined to use this specific code, but I thought it might be helpful. I’m very open for any suggestion that might point me in the right direction.

Ultra Low Power Led Flasher
with AtTiny85 @ 1Mhz
by Luca Soltoggio
#include <avr/sleep.h>
#include <avr/wdt.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
int pinLed = 0;
volatile boolean f_wdt = 1;
void setup(){
setup_watchdog(8); // approximately 4 seconds sleep
void loop(){
if (f_wdt==1) {  // wait for timed out watchdog / flag is set when a watchdog timeout occurs
f_wdt=0;       // reset flag
digitalWrite(pinLed,HIGH);  // let led blink
pinMode(pinLed,INPUT); // set all used port to intput to save power
pinMode(pinLed,OUTPUT); // set all ports into state before sleep
// set system into the sleep state
// system wakes up when wtchdog is timed out
void system_sleep() {
cbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_mode();                        // System sleeps here
sleep_disable();                     // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN);                    // switch Analog to Digitalconverter ON
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
f_wdt=1;  // set global flag

Some other thoughts I had in regards to saving battery life:

  • Use yellow LED instead of white
  • Use AtTiny 13 instead of AtTiny 45
  • Am I using the wrong resistor (currently using a 220 Ohm resistor)

Many thanks in advance to this amazing community for helping me out with my questions!

You could do this with the watchdog timer but it won't help the battery unless you also shutdown power to other things such as ADC and timers.

I would not call that a firefly simulation.

Fireflies flash with different patterns. Females have some level control, most males are just on/off. |409x500|440x500