Cool stuff for Raspberry Pi, Arduino and all electronics hobby projects

Use Witty Pi 2 to Build Solar Powered Time Lapse Camera

Witty Pi 2 can define complex ON/OFF sequence for your Raspberry Pi. Thanks to the schedule script, you can keep your Raspberry Pi in OFF state, and turn it on only when necessary. By doing so you are saving a lot of energy, which is perfect for solar/battery powered system.
In this tutorial, I will show you how to build a solar powered time lapse camera with Witty Pi 2. The required materials are shown in the picture below:
01

  1. Witty Pi 2
  2. Raspberry Pi (can be model A+, B+, 2B, Zero V1.3 or 3B, I use 3B here)
  3. Raspberry Pi Camera Module
  4. Solar Panel (choose one according to your requirements. I use Voltaic’s 6W solar panel, with maximum output 6V/1A)
  5. Power bank (should support charge and discharge at the same time. Here I use Voltaic’s V15 power bank)
  6. USB cable and micro USB adapter

The Idea

The block diagram below shows the main idea:
02
Backed by the solar panel and power bank, Witty Pi 2 can wake up your Raspberry Pi according to the schedule script. A small BASH script will be executed after system is up, and it will take a picture, save it to a directory and then shutdown Raspberry Pi. This system can run until the world ends!
You may ask, why we need the power bank here? It is because solar energy may not be stable. When weather goes to cloudy or rainy, your solar panel may not have enough output to power your Raspberry Pi. The power bank can store enough energy and make sure your Raspberry Pi will work normally.

Choosing the Solar Panel

You should consider many factors when choosing the solar panel, such as:

  • What model of Raspberry Pi are you using?
  • How long your Raspberry Pi will stay on each time?
  • How often will your Raspberry Pi get woken up?
  • How is the weather of your location?
  • Where will you place your solar panel?

Different model of Raspberry Pi will draw different current. If you are not sure about the difference, you can check out this comparison on Raspi.tv.
The weather and the location you place the whole system will determine how long (in average) your solar panel can efficiently work everyday. It should generate enough mAh to support running the whole system for a day.

About the Power Bank

Most power banks in the market have a feature that they will turn themselves off when the load doesn’t draw enough current. This feature is useful since it can save your power, but in our case it becomes kind of annoying: when Witty Pi 2  is in standby state, it draws too small current (~1mA), and most of power banks will shutdown automatically. As a result, Witty Pi 2 can not get powered when it tries to turn on your Raspberry Pi, what a shame! The reason that I use Voltaic’s V15 power bank is that, it has an “Always On” mode. Just hold its power button for 6 seconds and it will enter this mode and will never turn off the power bank, while only consume very small current (~3mA) from the battery.
What if you want to use other power banks? How to avoid the auto power cut? The answer is “pulsing dummy load”, which is a new feature in Witty Pi 2. The idea is to use a resistor as dummy load, to draw enough current, to cheat the current monitor in power bank. Most power banks need ~80 mA to keep themselves alive. If you use a resistor to consistently draw 80mA current, it consumes 80mAh per hour (1920 mAh per day). In order to save the energy, you don’t have to keep the resistor connected, instead you connect it with a fixed interval, hence the actual current in average will go down. Witty Pi 2 has such a pulsing dummy load built-in, and you just need to configure the blue jumper to turn it on:
03
By default the blue jumper cap is on the left, which means the pulsing dummy load is off and Witty Pi 2 draws about 1mA during standby. If you put the blue jumper cap on the right (as shown in figure above), you turn the dummy load on, and it will have a current peak (~100mA) for every 10 seconds. The actual current in average is about 15mA with the dummy load turned on.

So if your power bank can not keep always on, please don’t forget to turn on the dummy load on your Witty Pi 2.

Let’s Build

Now we can start the building. First we connect the camera module to Raspberry Pi.
05
Then we mount Witty Pi 2 on Raspberry Pi, and the camera cable should go out from the gap on the left:
06
Use a USB – mini USB cable to connect Witty Pi 2 to the output port of power bank.
07
Now we need to connect the solar panel to the power bank. The 6W solar panel I use has 3.5mm output plug, while the V15 power bank expects a micro USB input (your case may vary). So I need to use an adapter to finish the connection, as shown in the figure below. Here I would like to remind you that, your solar panel may not regulate its output voltage.  In best case (worse case?) it can output voltage higher than 7V. 7V is not very high voltage and most power bank can live with it, but there is no guarantee that it won’t hurt your power bank. So maybe you would like to consider a regulator (like this one).
08
After connecting them all together, it is time to find a place to hold them. I use a temporary setup as shown below. You can of course make something better and more stable. It depends on what you plan to do with it, whether the solar panel can collect enough sun shine there, and whether your cat will mess it up etc. If you decide to put them outside of the house, please make a shelter for them, or a sudden rain could kill them.
04

Software

First you need to make sure you have installed the software for Witty Pi 2. Please follow the “Software Installation” chapter in the user manual.
You will need to write a simple script that can take a picture and correctly shutdown your Raspberry Pi. This script will be invoked by the software of Witty Pi 2.
Let’s create a directory in the home directory to store the new script file and pictures.

mkdir time-lapse-camera

Create the script file:

cd time-lapse-camera
nano run.sh

Below is the script content:

#!/bin/bash
# file: run.sh
#
# This script takes a picture and shutdown Raspberry Pi
#
# take a picture
file_name=$(date +'/home/pi/time-lapse-camera/%Y%m%d_%H%M%S.jpg')
/usr/bin/raspistill -o $file_name
# shutdown Raspberry Pi by pulling down GPIO-4
gpio -g mode 4 out
gpio -g write 4 0  # optional

This script calls “raspistill” command to take the picture, and save it with the date time as file name. Then it pull down GPIO-4 to notify Witty Pi 2 to shutdown your Raspberry Pi.
Please notice the absolute paths for jpg file and raspistill command, they are necessary since the script will be executed as root user.
Here you can see the recommended approach to shutdown Raspberry Pi in your own script: gpio -g mode 4 out. This command will set GPIO-4 pin to output mode, as a result the pin will go to LOW state automatically, so the following “gpio -g write 4 0” command is optional.
You also need to make this script executable, by calling this command:

chmod +x run.sh

This script should run on boot, so it can quickly take the picture, and then shutdown your Raspberry Pi to save power. There are many ways that can run this script on boot, however the easiest way is to register your own script in the “extraTasks.sh” file, which will be executed in background after Witty Pi 2 is initialized.

cd ~/wittyPi
nano extraTasks.sh

You can append your command at the end of “extraTasks.sh” script, to have your own script registered:

#!/bin/bash
# file: extraTasks.sh
#
# This script will be launched in background after Witty Pi 2 get initialized.
# If you want to run your commands after boot, you can place them here.
#
/home/pi/time-lapse-camera/run.sh

Make sure to use the absolute path of your “run.sh” script. If you have other commands to run, you can place them here too.

Make Schedule

It is almost done! The only missing part is to tell Witty Pi when to wake up your Raspberry Pi. Let’s assume we want to wake up Raspberry Pi every hour. This could be done by two different approaches.

Option A: via wittyPi.sh

You can schedule the startup time with wildcard (??), so it will get repeated automatically.
After running “sudo ./wittyPi.sh”, you select the 5th item, and then input “?? ??:00:00”,  the result looks like this:

>>> Current temperature: 35.0°C / 95°F
>>> Your system time is: Sat 16 Jul 2016 22:43:58 CEST
>>> Your RTC time is:    Sat 16 Jul 2016 22:43:58 CEST
Now you can:
  1. Write system time to RTC
  2. Write RTC time to system
  3. Synchronize time
  4. Schedule next shutdown
  5. Schedule next startup  [?? ??:00:00]
  6. Choose schedule script
  7. Reset data...
  8. Exit

It is done! You can press the button to turn off your Raspberry Pi and it will wake up every hour, take a picture and then go to sleep again.

Option B: via Schedule Script

It seems a little overkill since the schedule script is designed for more complex schedule. However it can be done indeed. You can create a new schedule script file in the “schedules” directory:

nano ~/wittyPi/schedules/time_lapse_camera.wpi

Then you can define its content like this:

# Turn Raspberry Pi on every hour
BEGIN   2016-07-01 00:00:00
END     2025-07-31 23:59:59
ON      M5      WAIT  # keep ON state until shutdown externally
OFF     M55           # keep OFF state until next o'clock

The “WAIT” syntax will tell Witty Pi 2 to skip scheduling the shutdown at the end of the ON state, so it will not try to shutdown your Raspberry Pi automatically (the run.sh script will).
To use this schedule script, you need to choose it when running wittyPi.sh, by selecting the 6th item (Choose schedule script) and pick the schedule script you just created.
Using schedule script is a little bit complex than specifying the startup time, but the advantage is that it can support more complex scenarios. You can wake up your Raspberry Pi at any moment in the day, you can even skip the weekend etc.

Are You Locked Out?

Have you considered that, after the deployment you hardly have a chance to login your Raspberry Pi any more? Every time after your Raspberry is turned on, it immediately takes a picture and then shuts itself down, and the entire process only lasts about 30 seconds! Are you really locked out now? Do you have to remake your SD card to access your Raspberry Pi?
Fortunately we can disable the shutdown on hardware level. Just use a Dupoint wire to connect GPIO-4 and 3.3V pins, hence GPIO-4 is strongly pulled up and the “run.sh” can not actually pull it down any more. By doing so you can still login your Raspberry Pi and do whatever you want.
09
Be careful! The GPIO-4 is just next to the GND pin, make sure not to connect 3.3V with GND by mistake! I actually did that once, and thanks to the poly-fuse on Witty Pi 2, nothing really bad happened, and Raspberry Pi can still boot normally after removing the wire.
Remarks: you need to confirm your Witty Pi works normally before using this trick. If your Raspberry Pi’s serial port is not properly configured, i.e. TXD pin is low and GPIO-4 is shorted to GND, connecting GPIO-4 to 3.3V can lead to serious consequence and D2 will be heated and even burn up!
After login your Raspberry Pi with this trick, if  you unplug the Dupoint wire, your Raspberry Pi may shutdown immediately as the GPIO-4 pin has been set to LOW via software.

Email Me the Photo!

It is indeed a time lapse camera now, you can let it take photos and later use the trick above to login your Raspberry Pi and copy those pictures to your USB disk, or you can download them via SFTP protocol. It is quite useful already, but we can make it even better.
The Raspberry Pi I use here is a Raspberry Pi 3, which has built-in WIFI. When it is turned ON it will automatically connected to my WIFI. This means it is connected to the Internet when it is working. So if we could let the run.sh script to send us the new photo via Email, that would be fantastic!
You will need to install these three packages to send Email in BASH script:

sudo apt-get install sendemail
sudo apt-get install libnet-ssleay-perl
sudo apt-get install libio-socket-ssl-perl

The latter two packages are required to support TLS when communicating with email server. After the installation, you can type “sendemail” in the console to see all supported parameters. Here I give you an example for sending email with file attchament via Gmail server:
sendemail -o tls=yes -f YourEmail@gmail.com -t RecevierEmail@domain.com -s smtp.gmail.com:587 -xu YourEmail@gmail.com -xp YOURPASSWORD -u “Time Lapse Camera Email” -m “I am still alive!” -a FILEPATH
You need to replace the sender/receiver email address, your password and the file path with the real values. After running this command in your console, it will send you the email with file attached. Now you can integrate it into the “run.sh” script:

#!/bin/bash
# file: run.sh
#
# This script takes a picture, send it out via email and shutdown Raspberry Pi
#
# take a picture
file_name=$(date +'/home/pi/time-lapse-camera/%Y%m%d_%H%M%S.jpg')
/usr/bin/raspistill -o $file_name
# send it out via email
sendEmail -o tls=yes -f YourEmail@gmail.com -t RecevierEmail@domain.com -s smtp.gmail.com:587 -xu YourEmail@gmail.com -xp YOURPASSWORD -u "Time Lapse Camera Email" -m "I am still alive!" -a "$file_name"
sleep 3 # just to be safe
# shutdown Raspberry Pi by pulling down GPIO-4
gpio -g mode 4 out
gpio -g write 4 0  # optional

I let the script wait for 3 sends after the email is sent, it may not be necessary and I just want to be safe.
If you are using other Raspberry Pi models, you can simple connect a WIFI dongle to it to get the same result.

The Result

Now you can reboot your Raspberry Pi (don’t forget to unplug the Dupoint wire), then it will start sending you the email with new pictures! Below is what I get in my inbox:
10
This small time lapse camera consumes very little power. According to my estimation, the entire system uses less than 200mAh per day. Although the solar panel on my window can get sun shine for only about 3 hours per day, it is way more than enough to charge the power bank to full state. Considering the power bank has 4,000 mAh capacity, I never need to worry about the power for it anymore, even in those days with bad weather.

See also

Join Waitlist We will inform you when the product arrives in stock. Please leave your valid email address below.