ATmega168-power-save-breadboard

ATmega168 Power Save Mode and Pin Change Interrupt

Most times your microcontroller is running in a loop, waiting for something to happen – like a button press. All this while it is consuming power, and this could be an issue, especially if you are running the circuit from a battery. To counter this problem, there are ways of reducing the power consumption of your chip. Here are the various “sleep modes” supported by the ATmega168.

[Table reproduced from Atmel ATmega168 datasheet for illustrative purpose]

ATmega168-sleep-modes

In a previous post about an Ambient Light sensor using an Op-Amp Comparator, I mentioned that we would be using the op-amp output to “wake up” an ATmega168. So here, we will be using the “power-save” mode from the above table, and use a pin-change interrupt to wake up from this sleep mode.

The setup used is shown below in the 2 schematics I had posted before. The output Vo from the op-amp should be connected to the pin 14 of the ATmega168 (PCINT0/PB0). Also, in this case, we are not using serial output, so you can remove the connections to the FTDI adapter.

IMG_1415

IMG_1412

Here is what the circuit looks like, hooked up on a breadboard:

ATmega168-power-save-breadboard

This is the how the program works:

  • When the program starts, start the 16-bit timer, set to fire an interrupt every 3 seconds. Start the timer only if pin 14 is low. (This means that the light level is high in our sensor circuit.).
  • When the chip is not sleeping, it will be flashing an LED attached to pin 6.
  • When the 16-bit timer interrupt happens, set a flag (a volatile variable) so that the main loop reads this flag and puts the chip to sleep mode. It will also turn the LED off at this point.
  • Set up a pin change interrupt on PCINT0 for any logical change in input. When the interrupt fires, if it was caused by a rising edge (ie., light to dark), wake up the chip, and stop the 16-bit timer. If the interrupt was caused by a falling edge (ie., dark to light), re-start the 16-bit timer.

You can read about pin-change interrupts and the 16-bit timer in the Atmel ATmega168 datasheet.

The complete code listing is below. The opening comments in the code also have all the required build commands.

2 thoughts on “ATmega168 Power Save Mode and Pin Change Interrupt”

  1. Technically, this isn’t using the Arduino library.

    You’re bypassing it completely and programming the 168 directly, which can often be very helpful and improve execution speed, (at the expense of having to understand the ATMega data sheet. This is what the Arduino library eliminates)

Add Comment Register



Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>