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!


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! (#