Raspberry Pi Time Lapse Code Example

I've been working on making a Raspberry Pi Time Lapse camera out of my 8x2 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!

Code a Raspberry Pi time lapse camera using Python


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 8x2 LCD display attached to it:

Raspberry Pi 8x2 Display ProtoCam Board

My 8x2 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:

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

Continue reading here: Using Bar Graph Displays With The Raspberry Pi

Was this article helpful?

0 0

Readers' Questions

  • Heikki
    How to zoom out raspberry pi camera?
    1 year ago
  • To zoom out the Raspberry Pi camera, you can adjust the focus settings of the lens. Follow these steps:
    1. Install the `rpi.gpio` library, which will allow you to control the camera using Python. Open the terminal and run the following command:
    2. ```shell pip install RPi.GPIO ```
    3. Initialize the camera in your Python code:
    4. ```python import picamera camera = picamera.PiCamera() ```
    5. Set the focus mode to "manual":
    6. ```python camera.focus_mode = 'off�
    7. 39; ```
    8. Adjust the zoom level by modifying the focus distance:
    9. ```python camera.zoom = (0.
    10. 0, 0.0, 1.0, 1.0)
    11. ``` The above line sets the zoom level to the entire frame (no zoom). You can adjust the values in the `zoom` attribute to achieve the desired zoom level. The values are in the order `(x, y, w, h)`, representing the position of the top-left corner (x,y) and the width and height of the zoomed-in region, respectively. For example, `(0.
    12. 1, 0.1, 0.8, 0.8)` would zoom in 10% from all corners and show an 80% zoomed-in region.
    13. Capture a photo or start recording video as desired:
    14. ```python camera.capture('image.jpg') # Capturing a photo # or camera.start_recording('video.h264') # Starting video recording ``` Remember to adjust the zoom values to experiment with different zoom levels.