The Sleepy Pi 1 contains an Arduino processor (ATMEGA328P) and its functionality is governed by the code that the Arduino contains. That means that to shutdown / start the Raspberry Pi you need to write / load / run some code on the Arduino. Fortunately there are some examples and a library that help you along the way.
NOTE: Sleepy Pi 2 uses a different RTC and the library is slightly different. There will be a page dedicated to Sleepy Pi 2 coming. For now see the Github page for the latest code and examples.
Sleepy Pi 1 library
The Sleepy Pi Arduino library contains routines that control the main functionality of the hardware. The latest version can be downloaded from GitHub.
Inheritance
In code terms it takes the form of a Sleepy Pi class and in true C++ tradition makes use of inheritance. For those new to programming and C++, inheritance provides a convenient way to make use of other code and integrate it into main class as if it born with it in.
The Sleepy PI currently makes use of three libraries:
- DS1374RTC to control the on-board Real-time Clock
- LowPower to provide sleep and low power modes to minimize power whilst we are waiting for something to happen.
- (Time Library) which is used by the DS1374RTC
These must be present in your Sketchbook/libraries folder so that the Arduino IDE knows where to find them.
To use an inherited routine as if it was our own we simply use the “.” notation:
For example, to use the LowPower libraries “powerDown” feature on SleepyPi we could write:
SleepyPi.powerDown(SLEEP_8S, ADC_CONTROL_OFF, BOD_OFF);
Concepts
The Sleepy Pi is a very flexible board and it can turn it’s hand to many things. In broad concept though, the Sleepy Pi is designed to maximize battery life in Raspberry Pi applications that do not require the RaspPi to be on all the time. In order to do this best, when the Arduino has cut the power to the RaspPi, it should itself go into a low power mode and then “wakeup” either on some event or periodically to check something.
Arduino Wakeup Options
The Arduino processor used is typical of many microcontrollers, in that it has the ability to “sleep” and when doing so reduce it’s power consumption. When asleep, though, we need some way of waking it up. I’ve found that shouting at it, is not too effective, nor is throwing a cup of water over it. Instead, I’ve found that these three methods work best:
- Triggering an Interrupt.
- Using an Alarm Clock
- Using the Watchdog Timer.
Triggering an interrupt is best performed using an I/O line. We can setup a GPIO line such that if it goes high or low or even changes it will wake the processor.
Using an Alarm Clock is really just a variation on the interrupt method, but is highlighted separately here because we actually have an Alarm line that goes from the RTC to the Arduino. We can set-up the RTC to trigger this Alarm at the desired time, which in turn wakes up the processor (there is no snooze button though).
Using the Watchdog Timer is an internal timer that can still run when the main parts of the processor are asleep. You can only set-up the timer with fixed times which are:
- 15ms
- 30ms
- 60ms
- 120ms
- 250ms
- 500ms
- 1s
- 2s
- 4s
- 8s
The watchdog is a great way to wake-up the Sleepy Pi to perform some periodic task – like take a sensor reading, check whether some other event has happened.
In all cases you can decide whether or not to wake the RaspPi.
Here is a simple example of using the watchdog timer:
#include "SleepyPi.h" #include <Time.h> #include <LowPower.h> #include <DS1374RTC.h> #include <Wire.h> const int LED_PIN = 13; void setup() { // Configure "Standard" LED pin pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN,LOW); // Switch off LED // initialize serial communication: In Arduino IDE use "Serial Monitor" Serial.begin(9600); } void loop() { // Enter idle state for 8 s with the rest of peripherals turned off SleepyPi.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART0_OFF, TWI_OFF); // Do something here // Example: Read sensor, data logging, data transmission. Serial.println("I've Just woken up"); digitalWrite(LED_PIN,HIGH); // Switch on LED delay(250); digitalWrite(LED_PIN,LOW); // Switch off LED }
LowPower Library
Getting your processor to sleep can be a bit of a Black Art at times and generally doesn’t involve rocking it or taking it for a drive, but instead wading through the intricacies of the processor datasheet. This is where the LowPower library comes in and takes the pain out of using low power sleep modes.
There are a number of different flavours of sleep and they each have a different levels of power consumption. In essence these are (in rough order from highest power to lowest):
- Idle.
- Adc reduction mode.
- Power save.
- Power down.
- Standby.
- Extended Standby.
For a complete overview of this visit the Rocketscream website. When used with the Sleepy Pi, the code snippet becomes:
#include "LowPower.h" #include "SleepyPi.h" void setup() { // No setup is required for this library } void loop() { // Sleep for 8 s with ADC module and BOD module off SleepyPi.powerDown(SLEEP_8S, ADC_CONTROL_OFF, BOD_OFF); // Do something here // Example: read sensor, log data, transmit data }