element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Arduino Sleep, pin interrupt
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 7 replies
  • Subscribers 391 subscribers
  • Views 2143 views
  • Users 0 members are here
  • arduinotutorials
  • arduino_tutorials
  • arduino tutorials
Related

Arduino Sleep, pin interrupt

Former Member
Former Member over 10 years ago

I am trying to explore the codes for Arduino to sleep. I am trying to write a simple program whereby the LED will blink once and go to sleep. a button will then be pressed to wake up the Arduino and it will loop.

 

Results: The LED blink once and goes to sleep. But when it wakes up, the LED stays on. Why is this so?

 

My codes are as follows:

 


#include <avr/sleep.h>

 

int led = 4;

 

void setup() {
  sleep_enable();
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  pinMode(led, OUTPUT);
  pinMode(2,INPUT);
}

 

void loop() {
  digitalWrite(led, HIGH);
  delay(1000);
  digitalWrite(led, LOW);
  attachInterrupt(0, interruptFunction, HIGH);
  sleep_cpu();
  }

 

void interruptFunction() {
  sleep_disable();
  } 

  • Sign in to reply
  • Cancel
  • gadget.iom
    0 gadget.iom over 10 years ago

    It looks like you're setting the sleep mode to power down in the setup routine, but there is no reference to sleeping anywhere in the loop or the interrupt function.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 10 years ago in reply to gadget.iom

    There is an example here that may be useful: Arduino, Zigbee and Embedded Development: Sleeping Arduino - Part 2 Wake Up Via An External Interrupt

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago

    Thank you all for your help.

    Paul Ellison:

    The reference I put is:

    sleep_cpu();

     

    This code is reference to: http://forcetronic.blogspot.sg/ (Reducing Arduino’s Power Consumption Part 1)

     

    The arduino was able to sleep. But when it wakes up, the led stays on. It stuck at:

    digitalWrite(led, HIGH);
    delay(1000);

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to gadget.iom

    I have also tried the code you suggested,

    #include <avr/sleep.h>
    #include <avr/power.h>

    int led = 4;
    int pin2 = 2;

    void pin2Interrupt(void)
    {
      detachInterrupt(0);
    }

    void enterSleep(void)
    {
      attachInterrupt(0, pin2Interrupt, LOW);
      delay(100);
      set_sleep_mode(SLEEP_MODE_PWR_DOWN);
      sleep_enable();
      sleep_mode();
     
      /* The program will continue from here. */
     
      /* First thing to do is disable sleep. */
      sleep_disable();
    }

    void setup()
    {
      pinMode(pin2, INPUT);
    }

    void loop()
    {
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    enterSleep(); 
    }

     

    However there is 2 issues:

    Firstly I need the program to continue from the  main loop program not enterSleep.

    Secondly, the LED was dim. (I am still trying to figure out why - I find no fault with LED or the Arduino Board)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 10 years ago in reply to Former Member

    I have just mocked together a setup with an Arduino NANO and a jumper, This code seems to work ok.

     

    #include <avr/sleep.h>
    
    int wakePin = 2;                 // pin used for waking up
    int led=13;
    
    void wakeUpNow() {
      // execute code here after wake-up before returning to the loop() function
      // timers and code using timers (serial.print and more...) will not work here.
      // we don't really need to execute any special functions here, since we
      // just want the thing to wake up
    }
    
    void setup() {
      pinMode(wakePin, INPUT_PULLUP);
      pinMode(led, OUTPUT); 
      attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function wakeUpNow when pin 2 gets LOW
    }
    
    void sleepNow() {
        set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
        sleep_enable();          // enables the sleep bit in the mcucr register
        attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
        sleep_mode();            // here the device is actually put to sleep!!
        // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
        sleep_disable();         // first thing after waking from sleep: disable sleep...
        detachInterrupt(0);      // disables interrupt 0 on pin 2 so the wakeUpNow code will not be executed during normal running time.
    }
    
    void loop() {
      digitalWrite(led, HIGH);
      delay(1000);
      digitalWrite(led, LOW);
      sleepNow();     // sleep function called here
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • Former Member
    0 Former Member over 10 years ago in reply to gadget.iom

    I have tried the code you have given me. It works!

    Thank you very much for your help. Appreciate it!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 10 years ago in reply to Former Member

    You're welcome! image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube