Average Ticker

Raspberry Pi Time Lapse Code Example

Raspberry Pi Time Lapse CodeCode a Raspberry Pi time lapse camera using Python

I’ve been working on making a Raspberry Pi Time Lapse camera out of my 8×2 display ProtoCam board that you may have seen me recently blog about.

A few weeks back I attended the Croydon Raspberry Jam to do a bit of a ‘Show & Tell’ stand, and I brought this project along to test if it works, and obviously show people what I had made.

Now usually when making a time lapse camera out of a Raspberry Pi I would use the ‘raspistill’ command with the ‘-tl’ section to take snaps every X seconds – just like I’ve showed you before. However I’ve taken a different approach this time as I wanted my code to perform an action in between each shot.

For a different, simple method of performing time lapse photography with the Pi – read on!

Video

Before I explain the results, here’s a video of the finished product. I took a shot about every 25 seconds for the entire day, and squished it into about 1.5 minutes of footage:

Why Change What Already Works?

If you’re wondering why I’m not just using the usual time lapse function in the ‘raspistill’ command, let me explain the project.

I’m using one of my ProtoCam boards with a mini 8×2 LCD display attached to it:

8x2 LCD with a Raspberry Pi

My 8×2 LCD display project using a ProtoCam board


The project is a simple menu + button system that lets you start the time lapse command that has been pre-set in the code.

However, the key thing here is that I want the display to show the number of images taken at any given time. To do this, I have to set an action after each picture is taken, to update the number shown on the LCD, then take another shot.

This wouldn’t work with the usual ‘Raspistill’ command as you can’t ‘interrupt’ it (well, I don’t know how to anyway!)

How Does It Work?

Its very simple, and almost a bit of ‘barbaric’ coding some might say.

Here’s a snippet of my code to show you the main section that takes the pictures and updates the display:

 def tlapse():  
  var = 0 #var starts a 0  
  while var < 840: #var adds '1' after every shot, script will stop at 840  
   timelastchecked = 0 #  
   if time.time() >= timelastchecked:  
    timelastchecked = time.time()+2  
    imagecount = ""  
    f=os.popen("ls -l | grep ^- | wc -l") #returns a line with the # of files in the directory 
    for i in f.readlines():  
     imagecount += i  #turn that line into 'i'
     imagecount = imagecount[0:-1] #chops a character off the end of the string  
     imagecount = imagecount.zfill(4) #this makes the number a 4 digit number 
 
     lcd_byte(LCD_LINE_1, LCD_CMD)  
     lcd_string("T-Lapse",3) #line one on the LCD - the '3' is to align the text right  
     lcd_byte(LCD_LINE_2, LCD_CMD)  
     lcd_string(imagecount,3) #our image count, aligned right   
 
     utc_datetime = datetime.datetime.utcnow() 
     s=utc_datetime.strftime("%d_%m_%Y_%H-%M-%S.jpg") #this is our filename string  
     filename = s #our string is now called 'filename'  
     GPIO.output(GREENLED, True) #LED on  
     os.system('raspistill -o ' + filename + ' ') #take a still using the filename set above   
     GPIO.output(GREENLED, False) #LED off  
     var = var + 1  #add 1 to var
     time.sleep(25)   #wait 25 seconds


Ok let’s break it down…

First Section: Setting a maximum number of shots

Here we create a variable called ‘var’. It starts at 0, and every time this loop cycles, there’s a line at the bottom of the script section (not in this section) that adds 1 to var.

  var = 0 #var starts a 0   
  while var < 840: #var adds '1' after every shot, script will stop at 840   
   timelastchecked = 0 #   
   if time.time() >= timelastchecked:   
   timelastchecked = time.time()+2   


We use this to set a maximum number of shots for our scenario – in my case I worked out that the day would take about 840 shots, so I added the ‘while’ line for that.

You will also see ‘timelastchecked’ here – I took this from an old project I worked on before. I don’t actually think this is needed, but I was too scared to remove it as my project was working – so – it stays! (#Average anyone?)

Second Section: Count the number of files in the directory

In this section of the code, we run a command to count the number of files in the directory, which we will later show on the LCD.

 imagecount = ""   
   f=os.popen("ls -l | grep ^- | wc -l") #returns a line with the # of files in the directory   
   for i in f.readlines():   
    imagecount += i #turn that line into 'i'  
    imagecount = imagecount[0:-1] #chops a character off the end of the string   
    imagecount = imagecount.zfill(4) #this makes the number a 4 digit number   


You can see from the # notes that the command returns a string and we turn it into ‘i’. Unfortunately this number is followed by a character that I don’t want to show on my LCD.

To get around this, I use the [0:-1] after the string name to chop one character from the end of the string. You can can also chop characters from the front using this as well.

Lastly, we use ‘zfill(4)’ to turn that string into 4 digits.

Now we have a 4-character string we can play with, so let’s move on…

Third Section: Update the LCD display

I’m not talking through the whole code here – I just want to show you how this particular part works. If you want to learn how to programme an LCD, there are a lot of tutorials on the web, and I’ve even written one or two myself.

The following section updates the LCD with the new count of files/images in our directory:

     lcd_byte(LCD_LINE_1, LCD_CMD)  
     lcd_string("T-Lapse",3) #3 is align right  
     lcd_byte(LCD_LINE_2, LCD_CMD)  
     lcd_string(imagecount,3) #our image count, aligned right   


The first two lines drive the first line on the LCD, and the second two control the second line.

The first line will simply show “T-Lapse” (I’ve only got 8 characters to play with remember), and the second line shows our-digit ‘imagecount’ string.

It’ll look something like this:

Raspberry Pi LCD Camera Counter

The number on the LCD updates every time a picture is taken


Fourth Section: LEDs and Camera

This last section lights the LED on my board, takes the picture, then turns the LED off. I did this to give me an easy way of seeing if the camera was working…..plus it was a good excuse to add more to the board!

    utc_datetime = datetime.datetime.utcnow()   
    s=utc_datetime.strftime("%d_%m_%Y_%H-%M-%S.jpg") #this is our filename string   
    filename = s #our string is now called 'filename'   
    GPIO.output(GREENLED, True) #LED on   
    os.system('raspistill -o ' + filename + ' ') #take a still using the filename set above    
    GPIO.output(GREENLED, False) #LED off   
    var = var + 1 #add 1 to var  
    time.sleep(25)  #wait 25 seconds  


The first 2 lines here grab the date and time, and then turn this date/time into a string to use as the image filename format for our image files, and even includes the ‘jpg’ part for us.

This method is a good way of tracking when photos were taken, and also avoids duplication in filenames.

Lines 4-6 take the photo using ‘raspistill’, with the LED controlled either side of it. You can see in the code where we’ve adopted the ‘filename’ string to use as each picture’s file name.

Lastly in lines 7 and 8 we add ‘1’ to var as I mentioned earlier, and then get the Pi to take a rest for 25 seconds before looping back round and checking if var is 840 yet. This gives us the time between shots that gives us that time lapse effect when we compile the images together.

The Full Code

I’ve concentrated this blog on showing you the specific section that controls my Raspberry Pi time lapse camera, to help give you ideas on how you might use this to run some kind of actions between images. Anything else you see in this post is very likely to be covered in my other posts.

I’ve added my full code for you to play with, including the imports and LCD controlling sections, in PasteBin:

http://pastebin.com/upKaB8mW


As always, if you have any questions, contact me!

Average Man

Be the first to comment on "Raspberry Pi Time Lapse Code Example"

Leave a comment

Your email address will not be published.


*


This website uses cookies. Please visit our privacy policy page for more information. Privacy policy

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close