Raspberry Pi Extended Time Lapse Camera (with battery power!)

Posted on May 12, 2014 by super

Learning Outcomes:

In this tutorial we will turn a Raspberry Pi into a time lapse camera that takes pictures at a preset interval. The Raspberry Pi will be battery powered so that we can install it off grid and get some great time lapses of places you might not want to leave your expensive SLR camera running at. What makes this time lapse Raspberry Pi different to other tutorials is that we will be using a Sleepy Pi board to do some power management. In this way we can extend the life of the battery allowing our Raspberry Pi to operate in the field for longer periods of time so we can get longer and more spectacular time lapses! How will we do this? Simple! We will be turning on the Raspberry Pi, taking a picture, and then shutting down right after. In this way the Raspberry Pi is only consuming battery power while it is on and taking a picture. Not while it is just idling, increasing the amount of time the time lapse camera can work on battery power. To do this we will use the Sleepy Pi board, this is basically a ATMEL 328p processor (same as an arduino UNO), a RTC, and a power management circuit in one! The Sleepy Pi is capable of taking a power input and then feeding it to the Raspberry Pi through a switch and into the GPIO port. We will be writing an Arduino program that controls this switch and sets the duration between our time lapse pictures. This tutorial is a bit long and some experience with computers, Arduino, or Raspberry Pi will help, however I have tried to make this simple enough for a first timer to follow and implement!

Components required for this tutorial:

1 Raspberry Pi (Model A or B)

1 Sleepy Pi Board

1 Raspberry Pi Camera (Normal or Noir)

1 Accessory Battery Pack

1 SD card with Raspbian on it

1 Mod my Pi Case (It has an easy to use GPIO expansion port)

1 Raspberry Pi Power Supply

Optional             

Keyboard

Mouse

Ethernet Cable or Wi-Fi module and USB port hub (for the Raspberry Pi)

1 HDMI Cable

IMG_2729

Important Notes!

This might not all make sense until you read the full tutorial, but I wanted to put it up front as it will help you when programming the Sleepy Pi. Hopefully this will reduce many frustrations!

When I was working on creating this tutorial I used a USB serial port cable and GPIO adapter to program the Sleepy Pi, this made my job of trial and error a lot easier! However you will most likely be programming the Sleepy Pi by using the GPIO port on the Raspberry Pi so here are some tips to make this as easy as possible! When you are programming the Sleepy Pi make sure that the jumper is installed across the 2 pins, this will bypass the Sleepy Pi switch and keep the Raspberry Pi on! However your Raspberry Pi might still shut itself down! This is because the program on the Sleepy Pi is still sending the Raspberry Pi a message to shutdown which it will do even if it is receiving power. So what we need to do is turn off the shutdown script running on the Raspberry Pi, this can be done by going back into /etc/rc.local and commenting out the line that calls for the shutdown script to run.

Instructions:

Step 1: We should start our project with a clean copy of Raspbian for the Raspberry Pi, this will ensure that no other applications or programs interfere with our time lapse programming. You can get an SD card with Raspbian on it from us or make your own by downloading the image here and following the instructions here.

Raspbian Desktop

Step 2: Now we should boot up the Raspberry Pi without the Sleepy Pi or the camera installed. To do this insert the SD card into the Raspberry Pi, plug in a USB keyboard and mouse, connect your raspberry pi to the internet using an Ethernet cable (with the other end in your router) , connect your Raspberry Pi to a monitor, and then plug in the power supply (do the Pi end first and then plug it into an outlet).

Important: If you have a case I recommend that you put it in the case first, this will prevent any unwanted short circuits from occurring and thereby damaging your Raspberry Pi.

You will then see a screen that looks like this

Raspbian Config Screen

First select option 1 Expand Filesystem and wait for the program to run and return you to the configuration screen.

Then go to option 3 Enable Boot to Desktop/Scratch, hit enter, in the next menu select the second option Desktop Log in as user ‘pi’ at the graphical desktop and hit enter. This will install the Raspbian GUI which will make the next steps easier

You will then be returned to the main menu, go down to the bottom and select finish. Your Raspberry Pi will now restart and you will be brought into the Raspbian GUI ready for the next steps.

Step 3: We are now ready to start configuring our Raspberry Pi so that we can turn it into a time lapse camera! The first configuration step is to install the Arduino IDE so that we can program the Sleepy Pi through the Raspberry Pi’s GPIO port. Since Jonathan Watkins (the creator of the Sleepy Pi board) has already created a very good guide on how to do this I have just linked to his guide instead of re-writing it. Follow his guide here until the end and then return to this tutorial for the following steps.

IMG_2730

To check that this step worked shutdown your Raspberry Pi, install the Sleepy Pi board on the Pi’s GPIO port and then turn the Pi on again. Then go to Start –>Electronics–>Arduino IDE and open the Arduino IDE

Arduino Blink

In the Arduino IDE go to File–>Examples–>Basics–>Blink this will load the blink sketch which we will use to test our work. Now go to Tools–>Board and then select Sleepy Pi (at the end of the list). We are ready to upload the code to the Sleepy PI, click the right facing arrow at the top of the Arduino IDE to upload it.

Zoom-In-on-Buttons

The Arduino IDE might ask to use a different serial port, select the one available and click OK. If everything was done right the amber LED next to the micro USB port on the Sleepy Pi will start to blink. If it blinks go on to the next steps, if not retrace your steps to see what you may have done wrong.

Step 4: We need to prepare the Raspberry Pi so that when it receives a command from the Sleepy Pi it shuts itself down properly. What do we mean by this? Well we want the software to turn the pi off softly using the shutdown command instead of just cutting the power to it. If we repeatedly cut the power to the Pi without the shutdown command we will end up corrupting the SD card, which will lead to the Pi not working and all your photos being lost! (Don’t worry you can wipe the SD card and start over with it).

To do this once again we follow an excellent guide created by Jonathan Watkins, you can find it here.

Once you are done following the steps there you can come back and continue the tutorial here.

Step 5: In the previous step we setup the Raspberry Pi so that the Sleepy Pi can shut down the Raspberry Pi nicely. Now we need to program the Arduino to turn the Pi on, let it boot up, send a shutdown command, and then cut the power to the Raspberry Pi once it has shut down. The program is setup to run in a loop so that it constantly cycles the Raspberry Pi on and off, we will then setup the Pi in the next steps to take a picture every time it boots up! In this way we create the time lapse camera! The delay between boot up and shutdown cycles will determine how often we take a picture. The reason we did it like this is because we will be fully shutting down the Pi in order to conserve battery power, the Pi does not have a low power mode so anything in the Pi’s memory will be lost when it is shut down. This means it is difficult to use things like the camera’s time lapse command, as the script will be reset every time we boot up the Pi. Meaning it starts running from the beginning, possibly rewriting your previous pictures! Obviously this prevents us from creating a time lapse as we will only have one picture to work with!

To program the Sleepy Pi just copy and paste this code into the Arduino IDE, save it, and upload it to your Sleepy Pi as before.

int LED = 13;
int SW = 16;         // Power the Pi
int GPIOA = 17;     // Cmd Pi to Shutdown
int GPIOB = 7;         // Pi is running
int State = 0;

void setup()
{
pinMode(LED, OUTPUT);
pinMode(SW, OUTPUT);
pinMode(GPIOA, OUTPUT);
pinMode(GPIOB, INPUT);
digitalWrite(GPIOA, LOW);

}

void loop()
{
switch (State)
{
case 0:
digitalWrite(LED, HIGH);
digitalWrite(SW, HIGH);
delay(Boot);
State = 1;
break;
case 1:
digitalWrite(GPIOA, HIGH);
State = 2;
break;
case 2:
if (digitalRead(GPIOB) == LOW)
{
State = 3;
}
else
{
State = 0;
}

break;
case 3:
digitalWrite(LED, LOW);
digitalWrite(SW, LOW);
delay(5000);
digitalWrite(GPIOA, LOW);
State = 0;
break;
default:
break;
}
}

Then shutdown the Pi, remove the jumper on the Sleepy Pi and plug the power supply into the Sleepy Pi. Now sit back and watch your monitor, the Raspberry Pi should boot up get to the desktop, then stay on for a bit before shutting itself down. A short while later it should boot back up and do the whole thing again. Congrats you got the Sleepy Pi to turn your Raspberry Pi on and off!

If it does not boot up try hitting the reset button on the Sleepy Pi (little brown button next to the GPIO port). I found that sometimes after uploading code to the Sleepy Pi it would not work but that resetting the board makes it work. If the reset trick does not work, double check your work!

Step 6: We have already gotten our Pi to wake up, turn on, and then go back to sleep so that it can hibernate and save power. Now we need to get it to take a picture at the beginning of each boot cycle. To do this we have to follow some simple steps.

raspberry-pi-lx terminal

First we will need to enable the camera on the Raspberry Pi, open LX Terminal and enter

sudo raspi-config

This will take you to the Raspbian configuration screen then go down to 5 Enable Camera and enable the camera. Then go to 8 Advanced Options then A3 Memory Split, and check that it says 128. If not change it to 128 and save the changes. Then exit the configuration screen. Turn off the Pi install the camera and turn it on again.

IMG_2732

IMG_2733

Now enter the following in LX Terminal

raspistill -o image.jpg

This should take a picture with the camera and save it to /home/pi. Check the image is there using the file manager under StartàAccessoriesàFile Manager. If the image is not there you have not properly configured your camera, retrace your steps to see what may have gone wrong.

If you did get a picture you are ready to go on to the next step.

Type the following into LX Terminal

mkdir ./camera

This makes a folder that we will save all of our pictures in

cd bin

sudo nano camera.sh

This will bring us into an editor that will allow us to write the camera script. Copy the following into the editor.

#!/bin/bash

DATE=$(date +”%Y-%m-%d_%H-%M-%S”)

raspistill -o /home/pi/camera/$DATE.jpg

Then hit control+x, and Y to save, hit enter to return to the LX Terminal. If you want to adjust any of the cameras parameters you can do that by changing the raspistill command using the Raspberry Pi camera commands which can be found here. For example the line raspistill -vf -hf -o /home/pi/camera/$DATE.jpg will invert the image.

Now type in the following into LX Terminal

sudo chmod 755 /home/pi/bin/camera.sh

This will turn the script into an executable file so that the Pi can run it

Now we need to enable this script at startup in the same way we did for the Sleepy Pi.

sudo nano /etc/rc.local

Then add the following line of code after the one for the Sleepy Pi but before the exit one.

/home/pi/bin/camera.sh

Now hit ctrl x and then y to save, hit enter to return to the LX Terminal.

Now type in

sudo reboot

The Raspberry Pi should restart and take a picture before the desktop loads. Then go to the camera folder and see if a picture is there. If it takes a picture and it saves it in the camera folder congratulation you are almost done! If it did not work as always recheck your work, typically it’ll be a small error like a letter in one part is capitalized but not in another or you have an extra space somewhere.

Step 7: Turn off the Raspberry Pi, install the Sleepy Pi as before, and power up the Raspberry Pi through the Sleepy Pi. Now watch what the Raspberry Pi does, it should boot up, take a picture, go into the GUI, shut off after a while, then idle for a bit before turning on again and going through the whole cycle again. If it does this then congrats you are done with the software side!

IMG_2734

Step 8: Now you can prepare your Raspberry Pi time lapse camera for its life in the wild! First modify the Arduino program to take pictures at a longer interval. To do this just change the duration of the delay in switch case 3 (currently it is 5000) to whatever value you want, remember this is in milliseconds. This value will delay the program for the desired amount of time, after that time the Pi will turn on again and take a picture before shutting down and sleeping. Then upload the code to the Sleepy Pi the same way as we did before (remember to turn off the shutdown script or the Sleepy Pi will still turn of your Raspberry Pi). Now attach the battery pack to the Sleepy Pi and watch what it does, it should take pictures at the selected interval. If it does this then we got the hardware and software all setup properly! Now we are ready to build the whole ordeal into some sort enclosure! You can see the crude one I made for my project bellow, it works ok but I would like to make it more weather proof in the future. Once you have your camera setup where you want it just plug the battery in to start it! When you are done taking pictures you can find guides online to help you stitch all of the images together into a time-lapse video. I am not going to detail how to do this as there are plenty of methods and guides online on how to do this. Enjoy your new programmable camera!

IMG_2737 IMG_2738 IMG_2739 IMG_2742

Conclusion:

In this tutorial we learnt how to build a long lasting battery operated time lapse camera using a Raspberry Pi and Sleepy Pi add on board. The benefits of doing it this way is that we can install the time lapse camera anywhere we want (even if there is no power out there) and we reduce the amount of power consumed while idling allowing us to take pictures for a much longer period of time than otherwise possible. I tried to keep this tutorial as simple as possible so that people of all skill levels can use it, so there are some simplifications in the setup of the camera and Sleepy Pi. More knowledgeable users can expand on this tutorial and make the whole setup more versatile or capable. For example the camera command currently only takes a picture and saves it with a time stamp, one of the first things you can play with is the various camera commands that are built into the Raspbian OS. These will allow you to set the white balance, contrast, exposure, and other camera related features so that the Pi camera takes the best pictures possible. Another thing we can do is add a bunch of sensors to the Sleepy Pi board, these can be used to trigger the Raspberry Pi on or as a data logger with their values periodically recorded by the Pi. In this way we get an intelligent time lapse camera that not only gives us a visual but also physical information about what was going on when the picture was taken. The Sleepy Pi also has a real time clock (RTC) on board, we could use this to very precisely set the time lapse cameras interval! Once the RTC is setup with the current date and time we can get the Pi to take a picture at the beginning of every hour, or do a burst of pictures at a particular time of day, or even only take pictures on one day of the week. Lastly we can also put the Sleepy Pi into a low power or sleep mode when not turning the Raspberry Pi on, this will result in an even lower level of power consumption by the time lapse camera allowing even longer run times! I will try and cover some of these aspects of the Sleepy Pi/Raspberry Pi time lapse camera in a future tutorial! For now we would love to see your time lapse videos!

Be the first to leave a comment

Leave a Reply