HOME    PARENT PAGE (solar power circuit details)     StumbleUpon.Com Recommend to StumbleUpon

ATtiny code for watchdog circuit for solar power project

(filename: PCB269-SolarWBackup-ATtinyCode.htm)

For a long time, I have wanted to power small computer projects with solar power. And have them run though the night off of a battery. At last, an answer for some of that want came my way.

I created a PCB to carry Nick Gammon's circuit, as explained on the parent page of the page you are reading.

This page gives you...

This page gives you the code for the ATtiny that you may wish to include in instances of PCB269 you choose to build. The ATtiny gives you the watchdog feature, wihch unlocks your Arduino if it gets frozen by a slowly rising Vcc after the power supply has failed after too many hours of heavy cloud cover. (The watchdog feature is an optional extra. You could just press the Reset button by hand, when the Arduino locks up... if you happen to notice.)

I did not create this code!

This code created by Nick Gammon, and generously shared, "under the Creative Commons Attribution 3.0 Australia License unless stated otherwise"

You can get it from Nick Gammon's original post, "Solar Powered Arduino", where you may find updates to what is here. But, in case that "goes away" for some reason, here is what the code was 21 Nov 17.

Note: Besides putting the code into the ATtiny, you need to adjust the following fuses: E2 should be low / DD should be high. Dealing with fuses is an adventure I have not yet had, but the information by dntruong looks like it will give me what I need... I hope it is enough for you, too! He presents two alternative paths to success... which is best for you will depend on prior experience and presonal preferences. If you know better fuse setting procedures, I would welcome news of them!

Nick doesn't explicitly address what the third fuse byte's settings need to be, i.e. the "Fuse Extended" byte. A look at the ATtiny's datasheet left me, a rank novice in these matters, fairly confident that it should be $FF, all bits "unprogrammed". (Yes, it seems that an unprogrammed bit should be holding a "1", that if you CLEAR a bit, i.e. make it "0", THAT constitutes "programming" that bit.)

I suspect that this "Fuse Extended: all bits unprogrammed, i.e. "1"'s" is one of those "everyone knows" things. Only one of the 8 bits is actually used, at least according to the datasheet I downloaded 11/17, and it's default state, "1" seems right. (Changing that would enable "Self-Programming the Flash", whatever that may be! (It's in the datasheet, if you want to go there.)) The other 7 bits are "reserved for future roles", and should hold default values- "1"s, "unprogrammed".)

// Reset solar panel watchdog
// Author: Nick Gammon
// Date: 22 March 2015

// ATMEL ATTINY 25/45/85 / ARDUINO
// Pin 1 is /RESET
//
//                  +-\/-+
// Ain0 (D 5) PB5  1|    |8  Vcc
// Ain3 (D 3) PB3  2|    |7  PB2 (D 2) Ain1
// Ain2 (D 4) PB4  3|    |6  PB1 (D 1) pwm1
//            GND  4|    |5  PB0 (D 0) pwm0
//                  +----+

/*

 After reset waits for TIME_TO_WAIT minutes, then brings D0 (pin 5) high for long
 enough to activate a MOSFET and reset the other board.

 Fuses: Low: E2 High: DD

 (Brownout at 2.7V)

*/


#include <avr/sleep.h>    // Sleep Modes
#include <avr/power.h>    // Power management
#include <avr/wdt.h>      // Watchdog timer

const byte MOSFET = 0;          // pin 5
unsigned long counter = 0;

const float TIME_TO_WAIT = 30; // minutes
const unsigned long SLEEPS_TO_WAIT = TIME_TO_WAIT * 60.0 / 8.0;  // 8 second sleeps

// watchdog interrupt
ISR (WDT_vect)
  {
   wdt_disable();  // disable watchdog
  }  // end of WDT_vect

#if defined(__AVR_ATtiny85__)
  #define watchdogRegister WDTCR
#else
  #define watchdogRegister WDTCSR
#endif

void setup ()
  {
  wdt_reset();
  pinMode (MOSFET, OUTPUT);
  ADCSRA = 0;            // turn off ADC
  power_all_disable ();  // power off ADC, Timer 0 and 1, serial interface
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  }  // end of setup

void loop ()
  {
  counter++;

  if (counter >= SLEEPS_TO_WAIT)
    {
    digitalWrite (MOSFET, HIGH);
    delayMicroseconds (10000);
    digitalWrite (MOSFET, LOW);
    // our job here is done
    sleep_enable ();       // ready to sleep
    sleep_cpu ();          // sleep
    }

  goToSleep ();
  }  // end of loop

void goToSleep ()
  {
  noInterrupts ();       // timed sequence coming up
  // pat the dog
  wdt_reset();

  // clear various "reset" flags
  MCUSR = 0;
  // allow changes, disable reset, clear existing interrupt
  watchdogRegister = bit (WDCE) | bit (WDE) | bit (WDIF);
  // set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
  watchdogRegister = bit (WDIE) | bit (WDP3) | bit (WDP0);    // set WDIE, and 8 seconds delay

  sleep_enable ();       // ready to sleep
  interrupts ();         // interrupts are required now
  sleep_cpu ();          // sleep
  sleep_disable ();      // precaution
  }  // end of goToSleep
  

That's it!

That's it for this "Here's the code" subordinate page. It wasn't meant to convey very much. Most of the information about PCB269, the inexpensive circuit for solar power with "overnight/ overcloud" battery backup is on the main page for PCB269.






   Search this site                 powered by FreeFind
 
Site Map    What's New    Search


Click here to visit my main homepage where you can explore other areas, such as education, programming, investing.




Ad from page's editor: Yes.. I do enjoy compiling these things for you. I hope they are helpful. However... this doesn't pay my bills!!! Sheepdog Software (tm) is supposed to help do that, so if you found this stuff useful, (and you run a Windows or MS-DOS PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

How to email or write this page's editor, Tom Boyd


Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes. A few "bad attributes" due to Google+ button, etc.


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....