ATtiny Low Power Mode

ATtiny applications are usually quite different from those made with other microprocessors and development cards. The main factor that causes these differences is that ATtiny microprocessors are small and capable.

So if we told you that you could almost halve power consumption on your ATtiny projects so far, would you be more interested in ATtiny? With its low power mode features, ATtiny will greatly extend the battery life of your projects and save lives in scenarios that are difficult to return or are constantly hard to access.

In this context, operations will be performed using the ATtiny85 and ATtiny402 microprocessors. But if you have an ATtiny microprocessor listed below, you can do the same directly in the specified section.

The section for ATtiny85 is compatible with ATtiny25, ATtiny25V, ATtiny45, ATtiny45V, ATtiny85, ATtiny85V.

Section for ATtiny402: ATtiny202 is compatible.

ATtiny85 Low Power Application

First of all we need a programmer to be able to perform the necessary tests on the ATtiny85 microprocessor and install the program code, you can follow the path you want in this regard, you can use the programmer part that is on USB-AVR or ATtiny85 development cards directly as a suggestion.

We also explained arduino IDE support and how to install programs in our previous ATtiny85 content, where you can see the details.

ATtiny development cards with micro USB, Type-C input support direct programming, so you don't need any external programmers.

This program code, ATtiny85 for the scenario, is a very adequate microprocessor, this microprocessor with 8 pins can handle many applications thanks to its 5 I/O pin.

Here's how to make the program code simple: Let's create a 15-minute meter and turn on the LED every 2 seconds while the meter is running and sound the alarm when the 15-minute time is over. And when the reset signal arrives, let's take over the program and restart it.

Circuit Diagram

ATtiny Low Power Mode

Installed Circuit Image

ATtiny Low Power Mode

By default, ATtiny85 uses its 1 Mhz timer, i.e. its timer, unless specified in the code section. You can also choose the 8 Mhz internal clock if you want. You can also make this selection directly from the Arduino IDE section. However, to use the 8 Mhz clock, you must follow these steps: you need to select the clock speed of 8 Mhz in the Card settings section and press "Print Bootsloader" in the Tools section, which does not actually print a bootstrapper to the ATtiny85 microprocessor, but makes the necessary pins, i.e. fuse settings, for 8 Mhz.

Low Power Mode

ATtiny85 supports low power sleep mode, during this sleep time, the clock stops, the current spent on the ADC and I/O part is limited and power is saved.

There are four ways to wake the microprocessor from sleep:

  • With reset pin (0) interrupt.
  • With pin interrupt.
  • With watchdog timer interrupt.
  • By giving a start command with Universal Serial Interface (USI).

In this application, we will do the wake-up call with a reset signal, which we can actually think of as a kind of pin 0 interrupt. We will use the top 4 different methods in other applications and provide information about them.

This is how we define the sleep mode we want to use:

set_sleep_mode(SLEEP_MODE_PWR_DOWN);

We use these commands to activate sleep mode:

sleep_enable();
sleep_cpu();

Optimizing Sleep Mode

If some functions and features are not used in your project, you can turn them off completely in the sleep section or in the program section. According to ATtiny85's datasheet, you can turn off the following properties:

  • Analog to Digital Converter (ADC) = Analog Dijital Dönüştürücü
  • Analog Comparator = Analog Karşılaştırıcı
  • Brown-out Detector
  • Watchdog Timer = Security Scheduler
  • Pin Outputs = Pin Çıkışları
  • Analog Input Buffer = Analog Giriş Tamponu

By default, only the ADC feature is active from the above features, even this feature uses approximately 320μA in stand-alone sleep mode, although you do not use any ADC features or code.

With the following code you can easily disable the ADC feature, we will use this code in the setup() section.

adc_disable();

Analog Comparator, Brown-out Detector and Watchdog Timer and Analog Input Buffer are not enabled by default, so there is no advantage in disabling them.

Identifying input and output pins (I/O) first in sleep function can be an important step for the continuity of the program and the project, we have not been able to create an environment that can fully test this, but it makes sense that we can do so.

Results

In tests with a 3V Watch battery, 0.2μA power consumption occurred instantly during sleep.

ATtiny Low Power Mode

If we consider the approximate capacity of this battery (CR2032) to be 200mA, it gives us a battery life of almost 40 years as a result of the calculations.

Values measured at 3V and 5V voltages, clock speeds of 1 Mhz and 8 Mhz:

5V3V
1 Mhz8 Mhz1 Mhz8 Mhz
During Operation2.5 mA8 mA0.7 mA3.6 mA
In Sleep Mode0.5 μA0.5 μA0.2 μA0.2 μA

Program ID

/* ATtiny85 Low Power Application */#include <avr leep.h=""></avr>  // Utility macro#define adc_disable() (ADCSRA <ADEN))  &= ~(1// ADC shutdown// constants  const int buzzer = 1;              // buzzer pinconst int LED = 0;                  // LED pinconst unsigned long Alarm = 900000; // alarm time - 15 minutesunsigned long Start Time = 0;        // start timevoid setup () {
  pinMode(LED, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  adc_disable(); // ADC uses ~320uA
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}

void sleepModu (void)
{
  sleep_enable();
  sleep_cpu();
}

void beep (void) {
  for (int i=0; i <  500; i++) {
    digitalWrite(buzzer, HIGH);
    delay(1);
    digitalWrite(buzzer, LOW);
    delay(1);
  }
}

void LedYakma (int wait) {
  digitalWrite(LED, HIGH);
  delay(wait);
  digitalWrite(LED, LOW);
}

// Main Loop ----------------------------------------------void loop () {
  // long led
  LedYakma (125);
  delay(125);
  LedYakma (125);
  do {
    // led burning part
    LedYakma(1);
    delay(2000);
  } while (millis() - Start Time < Alarm);
   // Alarm sound
  beep();
  sleepModu();
  // Continue after reset
}















ATtiny402 Low Power Application

ATtiny402 is a new member of the ATtiny series microprocessors developed by ATmel. With the major update to the ATtiny and ATmega microprocessor series, the naming and features have changed considerably.

You can access this exchange and update in our article here.

ATtiny402 is considered 0-series according to the new nomenclature, the reason we prefer this microprocessor especially is quite similar to the ATtiny85 that we often use.

We will implement the same project we did with ATtiny85 in this microprocessor, that is:

Let's create a 15-minute meter and turn on the LED every 2 seconds while the meter is running and sound the alarm when the 15-minute time is over. And when the reset signal arrives, let's take over the program and restart it.

Circuit Diagram

ATtiny Low Power Mode

Installed Circuit Image

ATtiny Low Power Mode

The ATtiny402 is only available in the SOIC case, so it is necessary to solder it in this way to use it as DIP.

Low Power Mode

Just like ATtiny85, this microprocessor supports 3 different sleep modes; idle, standby, and power off(power-down). Power off sleep mode provides the lowest power consumption; this shuts down the processor and all peripherals except the watchdog timer (WDT) and periodic interval timer (PIT) portion of rtc.

There are three ways to wake the processor from sleep:

  • SENSE with the cut of the pin.
  • TWI address match.
  • By creating a timeout in the Periodic Interval Timer (PIT).

In section ATtiny85, we did a wake-up call from power mode with the reset pin, but the new ATtiny microprocessors don't have a reset pin, so we'll do it using pin-sense cutting.

This is how we define the sleep mode we want to use:

set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable(); 

We use these commands to activate sleep mode:

sleep_cpu();

Pin-Sense Interrupt

New AVR microprocessors support four types of cutting in each pin: Detect both edges (BOTHEDGES), detect rising edges (RISING), detect falling edges (FALLING), and detect low level (LEVEL).

However, there may be a direct complication in this use.Some pins, called asynchronous pins, can wake the processor with any of these four input types; In ATtiny402, these asynchronous pins are PA2 and PA6.

Other pins can wake the processor from sleep only when bothedges and LEVEL interrupts. To be simple, we used a LEVEL cut on the PA3 pin, which eliminates the need to select a specific pin.Each I/O pin has a separate pin control port to configure the input pull and interrupt detection type.Pin control port PORTA for PA3. Pin3CTRand a pullup and a LEVEL interrupt perception configuration expression in PA3 are:

  PORTA. PIN3CTRL = PORT_PULLUPEN_bm | PORT_ISC_LEVEL_gc;

If you want a different sense type, you can change the word LEVEL appropriately with BOTHEDGES, RISING, or FALLING.

We also need to provide a interrupt service routine, but since we only use cutting to wake the processor, it does not need to do anything other than clear the PA3 cutting flag:

ISR(PORTA_PORT_vect) {
  PORTA. INTFLAGS = PORT_INT3_bm;
}

accuracy

The timer duration is specified in milliseconds with the following statement:

const unsigned long Alarm = 900000;

This timer uses the processor clock for its timing, so the accuracy depends on the accuracy of the internal system clock.

In ATtiny85, used in the original version of this project, the factory calibration of the built-in RC oscillator is rated ± 10%.By performing a custom calibration using the OSCCAL record, you can increase it up to ±%1 or connect an external crystal.

The factory calibration of the built-in RC oscillator in ATtiny402 is much more accurate, ±%2.However, you do not have the option of connecting an external crystal.

Optimizing Sleep Mode

To minimize power consumption while in sleep mode, it's important not to leave I/O lines as empty inputs; otherwise, they can consume power by oscillating. We can avoid this by enabling input pullups on them in setup() :

  pinMode(Unused2, INPUT_PULLUP);
  pinMode (Unused3, INPUT_PULLUP);
  pinMode (Unused5, INPUT_PULLUP);

Alternatively, you can define them as output.

In ATtiny85, it was important to disable ADC, otherwise this would have significantly strengthened sleep.This is not required here because ADC is automatically disabled on new ATtinys.

Although processor clock speed affects power consumption while the processor is running, it has nothing to do with sleep current because the processor is later stopped.

Results

In tests with 3V Watch battery, 0.1 μA power consumption occurred instantly during sleep.

ATtiny Low Power Mode

If we consider the approximate capacity of this battery (CR2032) at 200mA, it gives us a battery life of almost 200 years as a result of the calculations.

 5V3V
ATtiny4020.11μA0.10μA
ATtiny850.47μA0.17μA

As a result, it is that ATtiny402 uses significantly lower current in sleep, especially at the 5V supply voltage, than the old ATtiny85.

Compiling ATtiny402 Program

You can build ATtiny402 using megaTiny Core created by a GitHub user named SpenceKonde. On the Board menu, select ATtiny412/402/212/202 under megaTinyCore.Check that the next options are set as follows (ignore other options):

Chip: " ATtiny402 "
Clock Speed: "5MHz"
Programmer: "jtag2updi (megaTinyCore)"

You can then install the program on ATtiny402 using a UPDI programmer. You can convert any Arduino card to a UPDI programmer, as described in the Make UPDI Programmer.

You can ignore the error "Cannot find flash and boot memory in the description".

Program ID

#include <avr leep.h=""></avr>  const int Speaker = 0;              // PA6 buzzer pinconst int Lamp = 1;                 // PA7 LED pinconst int Button = 4;               // PA3 buttonconst int Not Used2 = 2;              // PA1const int Not Used3 = 3;              // PA2const int Not Used5 = 5;              // PA0/UPDI// Constants  const unsigned long Alarm = 900000; // 15 minutesvoid playBeep(void) {
  tone(Speaker, 523, 250);
  delay(250);
  tone(Speaker, 659, 250);
  delay(250);
  tone(Speaker, 523, 500);
  delay(500);
  noTone(Speaker);
}

void flashLed (int wait) {
  digitalWrite(Lamp, HIGH);
  delay(wait);
  digitalWrite(Lamp, LOW);
}

ISR(PORTA_PORT_vect) {
  PORTA. INTFLAGS = PORT_INT3_bm;        // Cleaning the PA3 flag
}

// Main loop *********void setup() {
  pinMode(Lamp, OUTPUT);
  pinMode(Speaker, OUTPUT);
  PORTA. PIN3CTRL = PORT_PULLUPEN_bm;    

pinMode(Not Used2, INPUT_PULLUP);
  pinMode(Unused3, INPUT_PULLUP);
  pinMode(Unused5, INPUT_PULLUP);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
}

void loop () {
  unsigned long StartTime = millis();
  flashLed (125);
  delay(125);
  flashLed (125);
  while (millis() - StartTime < Alarm) {
    flashLed( 1);
    delay(2000);
  } 
  // Alarm
  playBeep();
  delay(1000);
  PORTA. PIN3CTRL = PORT_PULLUPEN_bm | PORT_ISC_LEVEL_gc;
  sleep_cpu();
  PORTA. PIN3CTRL = PORT_PULLUPEN_bm;                    
}