ATTiny85 Sleep Mode

#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/interrupt.h>

#define LED_PIN 0 // Pin connected to the load

const unsigned long tenSeconds = 10000; // 10 seconds in milliseconds
const unsigned long threeSeconds = 3000; // 3 seconds in milliseconds

volatile unsigned long timer = 0; // Variable to store elapsed milliseconds

void setup() {
  pinMode(LED_PIN, OUTPUT);
  // No need for specific clock setup for this code
}

void loop() {
  // Turn on LED (pin 0 HIGH)
  digitalWrite(LED_PIN, HIGH);
  
  // Delay for 3 seconds
  delay(threeSeconds);
  
  // Turn off LED (pin 0 LOW)
  digitalWrite(LED_PIN, LOW);
  
  // Reset timer
  timer = 0;
  
  // Enter sleep mode with power down (all peripherals disabled)
  while (timer < tenSeconds) {
    set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode to power down
    sleep_enable(); // Enable sleep mode
    sei(); // Enable interrupts (important for waking up)
    sleep_cpu(); // Enter sleep mode
    sleep_disable(); // Disable sleep mode (wakes up from interrupt)
  }
}

// Interrupt Service Routine (ISR) - This function gets called whenever Timer 0 overflows
ISR(TIM0_OVF_vect) {
  timer += 255; // Increment timer by milliseconds based on timer overflow
}

Output returns (wiring.c.o (symbol from plugin): In function __vector_5': (.text+0x0): multiple definition of __vector_5') How can I solve this problem?

Hi @mehmetemin20

The timer 0 overflow (OVR) interrupt service routine (ISR) is already defined and used by the Arduino millis() and micros() timing functions.

One option is to use one of timer 0's compare match ISRs instead.

This is achieved by first enabling the compare match channel interrupts in setup(), for example channel A:

TIMSK |= _BV(OCIE0A);  // Enable compare match A interrupts

Then using the COMPA handler function instead:

ISR(TIM0_COMPA_vect)    // COMPA Interrupt Service Routine
{
  // ISR code goes here...  
}

Thank you, It worked.

I can upload my code however my code did not work properly. I just wanted it to be in sleep mode for 10 seconds and wake up at the end of 10 seconds. I don't know why it can't wake up. Do you have any exapmle code that could help me to find What I want exactly?

If you want the device to wake up with out an external trigger, then you have to set the watchdog timer. If the time period is longer than 8 seconds then you have to reset it multiple times until the desired period has been reached. See Gammon Forum : Electronics : Microprocessors : Power saving techniques for microprocessors

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.