The old forum isn't available anymore so I can't search for the topic I'm searching for.
I want to have an Arduino (nano), to wake up and go to sleep (or deep sleep), with the same button.
Is this possible?
I think there is a wake on interrupt available to wake an Arduino that is in deep sleep?
And when the Arduino is working, and the same button is pressed for a certain time (let's say 3 seconds), the Arduino tells by an LED that it goes to sleep and the button should be released, and then after a few seconds goes to sleep (or deep sleep).
I don't think I've ever used sleep on an Arduino And I wanted to search the old forum.. but this isn't available anymore?
Put Arduino to sleep/wake with 1 button
-
- Posts: 304
- http://meble-kuchenne.info.pl
- Joined: Tue Dec 08, 2020 5:11 pm
- Has thanked: 88 times
- Been thanked: 63 times
-
- Valued Contributor
- Posts: 1367
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 330 times
- Been thanked: 475 times
-
- Valued Contributor
- Posts: 1279
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 122 times
- Been thanked: 643 times
Re: Put Arduino to sleep/wake with 1 button
Should be possible to do this.
If the Arduino is asleep a pin change interrupt will wake it - going to sleep will require timing a press (3s) then waiting for the button to be released before sleeping again. Or time length of press and sleep if >3s.
#include <avr/sleep.h> in supplementary code
Then set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu();
(There are other sleep modes.)
In a code block.
Martin
If the Arduino is asleep a pin change interrupt will wake it - going to sleep will require timing a press (3s) then waiting for the button to be released before sleeping again. Or time length of press and sleep if >3s.
#include <avr/sleep.h> in supplementary code
Then set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu();
(There are other sleep modes.)
In a code block.
Martin
Re: Put Arduino to sleep/wake with 1 button
Thanks guys,
Could someone please ping the people at Matrix that they should update this topic to the old forum: https://flowcode.co.uk/forums/viewtopic.php?t=1266
This must become: https://www.flowcode.co.uk/mmforums/index.php
I have add the supplementary code and a C block with:
At first test, the AVR seems to go asleep, but wakes up at it's own..
I'm going to test it via a interrupt to poll for this button on D13, nothing else should be able to wake the Arduino..
Any suggestions are always welcome..
Could someone please ping the people at Matrix that they should update this topic to the old forum: https://flowcode.co.uk/forums/viewtopic.php?t=1266
This must become: https://www.flowcode.co.uk/mmforums/index.php
I have add the supplementary code and a C block with:
Code: Select all
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
I'm going to test it via a interrupt to poll for this button on D13, nothing else should be able to wake the Arduino..
Any suggestions are always welcome..
-
- Valued Contributor
- Posts: 1279
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 122 times
- Been thanked: 643 times
Re: Put Arduino to sleep/wake with 1 button
The Arduino only has individual pin interrupts (Int0 and Int1) which are on Pin D2 and D3.
Pin13 - is usually connected to the inbuilt LED.
I did a simple demo (I used an Uno, so you'll need to change the target to Nano). I set an interrupt (rising edge of Pin3) to wake from sleep - the interrupt does nothing.
Pin3 is pulled to ground via a resistor (I used 10k) and pulling it high (connect to 5V) wakes the Arduino. Here it just flashes an LED (in properties but I used 13 for the inbuilt) for 5s then sleeps.
The Arduino stays asleep until VCC is applied to Pin3 - but it would be possible to set multiple interrupts and it would wake on any (Rx UART etc)
Martin
Pin13 - is usually connected to the inbuilt LED.
I did a simple demo (I used an Uno, so you'll need to change the target to Nano). I set an interrupt (rising edge of Pin3) to wake from sleep - the interrupt does nothing.
Pin3 is pulled to ground via a resistor (I used 10k) and pulling it high (connect to 5V) wakes the Arduino. Here it just flashes an LED (in properties but I used 13 for the inbuilt) for 5s then sleeps.
The Arduino stays asleep until VCC is applied to Pin3 - but it would be possible to set multiple interrupts and it would wake on any (Rx UART etc)
Martin
- Attachments
-
- sleep.fcfx
- (10.41 KiB) Downloaded 136 times
-
- Valued Contributor
- Posts: 1279
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 122 times
- Been thanked: 643 times
Re: Put Arduino to sleep/wake with 1 button
I modified to act as a sleep/wake button. Still using pin D3 to allow the use of an interrupt to wake and detect button press.
Here - if the 'button' is pressed for >3 seconds (the LED is held on whilst the button is pressed) then the MCU sleeps (turning the LED off as well) - pressing again wakes the MCU and resumes flashing the LED.
I needed a small delay (I used 10ms but this might be excessive) to allow for 'bounce' on the 'switch' - which in my case is a jumper attached to VCC. I found that without this delay it would sometimes wake immediately from sleep.
Martin
Here - if the 'button' is pressed for >3 seconds (the LED is held on whilst the button is pressed) then the MCU sleeps (turning the LED off as well) - pressing again wakes the MCU and resumes flashing the LED.
I needed a small delay (I used 10ms but this might be excessive) to allow for 'bounce' on the 'switch' - which in my case is a jumper attached to VCC. I found that without this delay it would sometimes wake immediately from sleep.
Martin
- Attachments
-
- sleep.fcfx
- (14.99 KiB) Downloaded 137 times
Re: Put Arduino to sleep/wake with 1 button
Thanks mnfisher..
I found out that maybe there is another way to check if the button is released.
If I start the sleep sequence at the rising edge of the INT (now on D3), this means that the button is released.
I didn't realise that my button is pulled up..
I'm going to test this somewhat more.
Many thanks!
Edit: I've added the code: in the supplementary code, and call a C block when the chip should go to sleep.
As a test I added an output on D13 that is turned on in the beginning of the flowchart (the onboard LED), but that stays on even when the chip should be asleep?
Shouldn't that has to dim when the chip is asleep?
I found out that maybe there is another way to check if the button is released.
If I start the sleep sequence at the rising edge of the INT (now on D3), this means that the button is released.
I didn't realise that my button is pulled up..
I'm going to test this somewhat more.
Many thanks!
Edit: I've added the code:
Code: Select all
#include <avr/sleep.h>
Code: Select all
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
As a test I added an output on D13 that is turned on in the beginning of the flowchart (the onboard LED), but that stays on even when the chip should be asleep?
Shouldn't that has to dim when the chip is asleep?
-
- Valued Contributor
- Posts: 1279
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 122 times
- Been thanked: 643 times
Re: Put Arduino to sleep/wake with 1 button
No - pins will stay in the same state - so if your aim is to save power then turn off any output pins before sleeping... Ideally pins should be set to input etc - but you might want LED on (power?) even though MCU is sleeping.Shouldn't that has to dim when the chip is asleep?
Martin
Re: Put Arduino to sleep/wake with 1 button
My excuses I didn't receive a mail that there was an new answer.
I've been reading about other options to change settings when the chip is asleep.
But what puzzles me is what the chip does when it goes to sleep.
Does it keep the place in the program in "mind" when it goes to sleep and resumes it when it wakes up from this point, or is it possible to make the chip start the whole program again after waking up?
For me -in this application- it starts the program from start after waking up.
How does it work?
I've been reading about other options to change settings when the chip is asleep.
But what puzzles me is what the chip does when it goes to sleep.
Does it keep the place in the program in "mind" when it goes to sleep and resumes it when it wakes up from this point, or is it possible to make the chip start the whole program again after waking up?
For me -in this application- it starts the program from start after waking up.
How does it work?
-
- Valued Contributor
- Posts: 1367
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 330 times
- Been thanked: 475 times
Re: Put Arduino to sleep/wake with 1 button
Hi
Not an expert in your target, but generally when a chip goes to sleep it pauses where it is at, then resumes upon wake up.
You could put it to sleep, then the next instruction upon wake up a reset (maybe with a NOP in between)
You may need to tweak above to suit but that would be an easy option I think, generally speaking.
No doubt others more familiar will advise better.
Regards
Not an expert in your target, but generally when a chip goes to sleep it pauses where it is at, then resumes upon wake up.
You could put it to sleep, then the next instruction upon wake up a reset (maybe with a NOP in between)
You may need to tweak above to suit but that would be an easy option I think, generally speaking.
No doubt others more familiar will advise better.
Regards