<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://www.marcelpost.com/wiki/index.php?action=history&amp;feed=atom&amp;title=ATMega328_WDT_sleep</id>
	<title>ATMega328 WDT sleep - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.marcelpost.com/wiki/index.php?action=history&amp;feed=atom&amp;title=ATMega328_WDT_sleep"/>
	<link rel="alternate" type="text/html" href="https://www.marcelpost.com/wiki/index.php?title=ATMega328_WDT_sleep&amp;action=history"/>
	<updated>2026-07-04T20:10:03Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>https://www.marcelpost.com/wiki/index.php?title=ATMega328_WDT_sleep&amp;diff=2677&amp;oldid=prev</id>
		<title>Admin at 00:26, 27 January 2017</title>
		<link rel="alternate" type="text/html" href="https://www.marcelpost.com/wiki/index.php?title=ATMega328_WDT_sleep&amp;diff=2677&amp;oldid=prev"/>
		<updated>2017-01-27T00:26:21Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
For my wireless sensor nodes I have been using the ATmega328P-AU (TQFP) microcontrollers with great success. However, I wanted to make sure that I can get the power consumption down to a minimum whilst in sleep mode as these units will run off a 100mAh LiPo battery. Needless to say, the runtime of the device greatly depends on how low we can get the power consumption when the unit is in sleep mode.&lt;br /&gt;
&lt;br /&gt;
[[File:Sleeping-bats.jpeg]]&lt;br /&gt;
&lt;br /&gt;
The ATMega328 chip is the same as used in the Arduino Uno and should be compatible with the Duemilanove as only the form factor differs (TQFP vs DIP).&lt;br /&gt;
&lt;br /&gt;
With the sketch loaded into the mcu I was able to get the power consumption down to around 5uA.&lt;br /&gt;
&lt;br /&gt;
Brown-out detection is disabled, but if you need that for your project, expect the consumption to go up to about 23uA.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
  ATMega328 (same as in Arduino Duemilanove/Uno)&lt;br /&gt;
&lt;br /&gt;
  Watchdog-timer sleep sketch&lt;br /&gt;
&lt;br /&gt;
  Current in sleep mode: ~5uA  &lt;br /&gt;
  &lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;avr/interrupt.h&amp;gt;&lt;br /&gt;
#include &amp;lt;avr/sleep.h&amp;gt;&lt;br /&gt;
#include &amp;lt;avr/wdt.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define LED 13&lt;br /&gt;
&lt;br /&gt;
// the setup function runs once when you press reset or power the board&lt;br /&gt;
void setup() {&lt;br /&gt;
  &lt;br /&gt;
  // initialize LED pin as an output.&lt;br /&gt;
  pinMode(LED, OUTPUT);&lt;br /&gt;
  digitalWrite(LED,LOW);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// the loop function runs over and over again forever&lt;br /&gt;
void loop() {&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)&lt;br /&gt;
  delay(100);              // wait for a second&lt;br /&gt;
  digitalWrite(LED, LOW);    // turn the LED off by making the voltage LOW&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
  //enter sleep mode&lt;br /&gt;
  system_sleep();  &lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
//initialize watchdog&lt;br /&gt;
void setup_watchdog(int ii) &lt;br /&gt;
{&lt;br /&gt;
  // 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms&lt;br /&gt;
  // 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec&lt;br /&gt;
&lt;br /&gt;
  uint8_t bb;&lt;br /&gt;
  if (ii &amp;gt; 9 ) ii=9;&lt;br /&gt;
  bb=ii &amp;amp; 7;&lt;br /&gt;
  if (ii &amp;gt; 7) bb|= (1&amp;lt;&amp;lt;5);&lt;br /&gt;
  bb|= (1&amp;lt;&amp;lt;WDCE);&lt;br /&gt;
&lt;br /&gt;
  MCUSR &amp;amp;= ~(1&amp;lt;&amp;lt;WDRF);&lt;br /&gt;
  // start timed sequence&lt;br /&gt;
  WDTCSR |= (1&amp;lt;&amp;lt;WDCE) | (1&amp;lt;&amp;lt;WDE);&lt;br /&gt;
  // set new watchdog timeout value&lt;br /&gt;
  WDTCSR = bb;&lt;br /&gt;
  WDTCSR |= _BV(WDIE);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// system wakes up when watchdog is timed out&lt;br /&gt;
void system_sleep() &lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
  ADCSRA = 0;                // switch Analog to Digitalconverter OFF&lt;br /&gt;
  &lt;br /&gt;
  setup_watchdog(9);         // approximately 8 seconds sleep&lt;br /&gt;
&lt;br /&gt;
    /* The 5 different modes are:&lt;br /&gt;
          SLEEP_MODE_IDLE         -the least power savings&lt;br /&gt;
          SLEEP_MODE_ADC&lt;br /&gt;
          SLEEP_MODE_PWR_SAVE&lt;br /&gt;
          SLEEP_MODE_STANDBY&lt;br /&gt;
          SLEEP_MODE_PWR_DOWN     -the most power savings&lt;br /&gt;
     */&lt;br /&gt;
 &lt;br /&gt;
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here&lt;br /&gt;
&lt;br /&gt;
  noInterrupts ();           // timed sequence follows&lt;br /&gt;
  sleep_enable();&lt;br /&gt;
 &lt;br /&gt;
  // turn off brown-out enable in software&lt;br /&gt;
  MCUCR = bit (BODS) | bit (BODSE);  // turn on brown-out enable select&lt;br /&gt;
  MCUCR = bit (BODS);        // this must be done within 4 clock cycles of above&lt;br /&gt;
  interrupts ();             // guarantees next instruction executed&lt;br /&gt;
  sleep_cpu ();              // sleep within 3 clock cycles of above&lt;br /&gt;
&lt;br /&gt;
  sleep_disable();           // System continues execution here when watchdog timed out &lt;br /&gt;
&lt;br /&gt;
  ADCSRA = 1;                // switch Analog to Digitalconverter ON&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
// Watchdog Interrupt Service / is executed when watchdog timed out&lt;br /&gt;
ISR(WDT_vect)&lt;br /&gt;
{&lt;br /&gt;
 // nothing here&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>