This article goes into greater depth for the Arduino Code needed to sleep and wake the System.
The key to writing Sleepy Pi 2 Arduino code is to understand that the Sleepy Pi 2 is essentially intelligently controlled Raspberry Pi power switch. Our goal is to control the power to the Raspberry Pi and around this we have several decisions to make:
In addition to controlling the power to the Raspberry Pi, we also want to minimise the power used by the Arduino when the Raspberry Pi is off. The Arduino at the heart of the Sleepy Pi does have sleep modes so we can decide what level of sleep we need.
Before sleeping or waking we need to address powering the Raspberry Pi. These aspects are covered in a separate article here.
n order to wake must first be put to sleep. In order to do this, we call one of the Low Power Sleep routines. The lowest power routine is SleepyPi.powerDown and will be used throughout this post, but there are others you can use. Which one we call, will depend to a certain extent on how we intend to wake and they will be described below.
It’s also worth noting that when we call the Sleep routine, for example:
The code stops at that point. If you could step through the code line by line, we’d see it literally go into that routine and not come out. It stops dead in its track right there. On wakeup it then comes out of that routine and executes the next line of code and so on.
It’s also a good idea to switch the Rpi off before you go to sleep….
The Sleepy Pi once asleep can be woken in many ways. These can be termed Asynchronous or Synchronous / Timed.
Asynchronous triggers are not on a schedule and as their name implies can occur at any time. These include the INT1 interrupt and also a transition on another GPIO pin.
Synchronous / Timed can be further divided in “scheduled” or “periodic”. Scheduled are at a particular time whereas periodic are set repeating intervals.
Once we have woken up the Arduino we have to decide whether to wake up the Raspberry Pi. We can wake up the RPi by default when we wakeup, but we do also have the option to consider other factors in code before deciding whether to power the Rpi. For example we might check an analogue input, for example a light sensor. If if was light enough, wake up the Rpi and do something, like take a picture.
This is the simplest mode – we put the system to sleep forever. We power down the Raspberry Pi and put the Arduino into a deep sleep by this routine:
To wake up the system we need a random or asynchronous event on the external interrupt INT1 pin of the Arduino. This is connected to the onboard white switch and is also brought out to a pin header. This can be used for a remote switch or an-other input. This input is pulled high, so a high to low transition is required to wake up the Arduino.
To show how this is set this up in code refer to the examples:
Examples include: WakeOnPin, ButtonOnOff, ButtonOnOff3
The Arduino can be woken on a GPIO pin change. This can be rising, falling or just a change. The setup for this is a little more involved and a third party library: PinChangeInt is used to make things easer. To use the library, first choose a pin to use as the wakeup source, configure it, create an interrupt routine, attach it. For example something like this:
You can use virtually any pin for this purpose and on Sleepy Pi 2, just select a spare one.
With these modes we will be maintaining the lower possible mode until some arbitrary event happens. This could be weeks or many months between triggers for example might be a motion controlled camera.
This mode is superficially similar to the Asynchronous mode (and they are not mutually exclusive) but the key in thinking here is that we are going to use some sort of timed interrupt. This can take the form of using the on board Real Time Clock (RTC) to trigger an interrupt on the external INT0 pin or to use the Arduino Watchdog to trigger an internal interrupt.
When we use the internal Arduino Watchdog, we can wake the Arduino up at regular intervals or periodically.
When we use the Real Time Clock we can wake the Arduino either periodically using a timer or on a schedule using the clock and calendar, very much like we would with an alarm clock. This can be a one-off or a repeating alarm for example everyday at 9-00am. You could also set the alarm to go off every hour, so there is overlap to the timer functionality. Basically there are many ways of doing the same thing.
Note: If we want to wake the Arduino periodically we use either the Watchdog or the RTC functions, they pretty much do the same thing. However the RTC allows you to set arbitrary intervals, whereas with the watchdog you have to adhere to its set intervals.
The Watchdog is an internal timer that can be used for a variety of uses, chiefly for keeping a watch on the processor and to reset it if there is a problem. There is another use for it as a periodic timer and that is how we use it here. With the processor asleep, we can ask the Watchdog to wake us up periodically as an alternative to “Periodic with RTC Timer”. The main difference between these two approaches is that the Watchdog has a fixed set of intervals. These are:
To use the Watchdog we select the mode of sleep that we want (as usual) and then select the interval from a predefined set of constants found in LowPower.h and shown above. For some reason the constants and the actual time don’t exactly line up, for example 16ms and SLEEP_15MS – don’t ask me why?
These modes all use the RTC’s onboard timer rather than it’s full clock / calendar. The code follows this form:
You set the timebase for the timer and then the timer value. It’s a countdown timer, so when it gets to zero it fires. The timebase you can use is either:
And the timer value is an 8-bit number so can be a maximum of 255.
As you can see the Arduino wakes on a set periodic interval and sleeps in between. You can use this to check on something like an analogue input of just use it to wake the Rpi at set intervals, like every hour. You can use these modes in conjunction with qualification code, for example, take a picture every hour, but only during the daylight hours.
Examples include: WakePeriodically, WakePiPeriodically, WakePiPeriodicallyDuringDay.
This mode uses the RTC Alarm function. This acts like a typical alarm clock in that you set a time for the alarm to go off which then fires off the Alarm INT0 pin. It doesn’t have to be a set date, you can set it to go off every day at a set time.
The code takes the normal form of first setting the alarm and then putting the Arduino to sleep. For example:
In this case the SleepyPi.setAlarm function is overloaded and can take a varying number of parameters. Use one of these to setup the alarm:
You can see from these options that you can set to wake every set number of minutes – very similar to what we could do using the timer or watchdog.
You could set an hour and minute, so for example, it would wake every day at 9:07 am for example.If you add day into this, then it would be that day per month, for example 17th of each month at 9:07. Alternately, you can set a day of the week so for example, this could be every Monday at 9:07am do something.
Your email address will not be published. Required fields are marked *