When using microcontroller in battery-based applications we really need to look at current consumption. This will affect the power consumption and hence the lifetime of the device.

This post shows some tests I performed to try and reduce the current consumption of an ATtiny85 AVR microcontroller using sleep modes and switching off various aspects of the IC when they are not required.

The test results and code for the Arudino IDE is shown here.

Test circuit

I needed a test circuit to check both the correct functioning of the different inputs and outputs and also for comparison of the current consumed when using different power modes.

My test circuit comprised of:

  • Software serial output every 1 second on pin 2 (Arudino 3)
  • Fast PWM 32kHz output at 50% duty on pin 5 (Arduino 0)
  • On/Off 0.5s output on pin 6 (Arduino 1)
  • Analog read every 1 second on pin 7 (Arduino A1)
  • FET output being driven at 32kHz (Fast PWM) at 50% duty (but NO load) on pin 3 (Arduino 4)

I wanted all of this to function at all times. I built this circuit and rigged up a Fluke multimeter to accurately measure the current.

I used a test input voltage of 16.0V DC from a bench power supply. This is similar to the maximum voltage from a 12V lead acid battery.

Using this test circuit with no power saving I found that the current consumption was on average 16-17mA:

Interestingly, when I did not have a serial output lead attached, the current consumption dropped to 10-13mA. This means that driving a serial output requires an extra 5-7mA. I am unsure why, apart from current required on the output Tx pin? Any ideas anyone?

While not a huge current draw this would still  be a high current draw on a small lead-acid battery. This is designed for a renewable energy powered system and hence every uA matters.

Power reduction

There are a few ways we can save power. These include reducing the operating frequency, reducing the operating voltage, put the unit into sleep mode when not required, switch off any internal microcontroller modules when not in use.

I did not want to reduce the operating frequency as this would affect the PWM frequency output and the serial output. I did not want to reduce the operating voltage as this would require a re-design of the FET driver. So I have the option of putting the IC into sleep mode and switching off any modules which are not used.

ATTiny Sleep Modes

From the ATtiny25/45/85 data sheet we can look at section 7 which refers to Power Management and Sleep Modes.

There are also a couple of interesting videos here and here from InsideGadgets. And its always worth reading up from the Arudino website about putting the ‘normal’ arduino (ATmega328) to sleep here.

Another article on testing the Arduino for power modes here.

There are three sleep modes on the ATTiny25/45/85 as shown in the data and copied here:

ATTiny power management

There are also a number of ‘modules’ which can also be switched off to save even more power. These are also mentioned in the data sheet and include:

  • Analog to digital converter – I switch this off before going to sleep.
  • Analog comparator – In Power_Down sleep mode this is disabled automatically.
  • Brown-out detector – I use this to check if voltage has dropped. This is set with fuses internal to the microcontroller
  • Internal voltage reference – This is required for the Brown-out detection.
  • Watchdog timer- This is used to wake from sleep so cannot switch off.
  • The port pins – These are all set to input before sleeping to help save power

Arduino and ATTiny in sleep

In order to put the ATTiny to sleep we need to include two libraries: avr/sleep.h and avr/interrupt.h. One is required to give us access to the sleep functions and the the other is required to control the interrupts to awake from sleep.

To start with I just want to do a power down and see what happens and the current reduction.

Using the following code I set the ATtiny to sleep for 4 seconds and then do all the normal functions (ADC read, Fast PWM output, write to the serial port, light the LED for 0.5 seconds), then go back to sleep.

Putting the IC to sleep massively changed the power consumption. It now consumed around 200uA (0.2mA) when in sleep, with an average of around 2mA, including the LED ON time.

Yey! In sleep much less current consumption (the reading is in uA…)

Averaging over 30ish seconds we gat an average of 2.85mA. This depends upon what is switched on when the unit is not asleep, but is much better than our previous value of 12mA.

Arduino code for ATTiny85

Here is the sample code including the sleep function. There are a few things to note here: We use the watchdog timer to wake up from sleep, so no external interrupt is required. When we do wake up due to the watchdog timer the watchdog flag is set, which allos our main code to run and then go back to sleep.

Putting the outpins into input before sleeping mode saves LOTs of current. When I did not do this the current consumption was still around 4mA when in sleep mode.

This allows me to reduce the power consumption of my project (the open source charge controller project).

Now I need to write a program flow which will put the unit into sleep mode as often as possible.

Edit 28/1/14:

Here is a very good tutorial on sleep modes for the Arduino:


5 responses to “Sleep Modes on ATTiny85

  1. Thanks for doing this. I would like to reuse some of this code for my own LED lighthouse simulator which I originally did in assembly using the Atmel Studio IDE on Win XP. I am trying to get your code to compile but it keeps telling me its run out of program memory (2k). I am using Arduino 1.8.2 on Max OS and setting the board description to ATTiny25/45/85. It seems to think this code is for a 25. Is there something I add to the code to tell the IDE that it’s for an 85? ( I also tried Adafruit Gemma Tiny85 but had an issue with A0).

  2. Two typos above, A1 not A0 and Trinket not Gemma.
    I changed A1 to 7 for the analog input and it compiled using Adafruit Trinket.

  3. According to datasheet (see fig. 22-12), an ATtiny in deep sleep, with Watchdog enabled, need less than 10µA.
    The remaining 180µA has to be consumed by the board (LDO, Pull-up/down resistor etc.).
    Ps : Another typo : “Arudino 3”.

  4. Very useful information and code, thanks. With a logic level P-channel mosfet controlled by the ATtiny to switch power to the servo in my project, I have now achieved a sleep-mode current of 6.5 microamps.

Leave a Reply

Your email address will not be published. Required fields are marked *