Linux Format

Time-lapse photograph­y.....

Les Pounder reveals how it’s possible to create a straightfo­rward timelapse controller for the picamera that’s running an analogue control system.

-

Learn how to take better time-lapse photograph­s by reading Les Pounder’s handy project.

Using the Raspberry Pi camera for timelapse is a simple project, but ensuring that the subject of your photograph­y is lit correctly can be a little tricky. In this project we shall create a timelapse controller that has analogue controller­s for the brightness and contrast of the images to be captured. The files saved will also have a timestamp to ensure that they’re captured correctly and can be reviewed in chronologi­cal order.

So let’s start by building the hardware.

Circuit city

To build the circuit for this project we need to place an MCP3008 analogue-to-digital converter onto the breadboard. Pay special attention to a dip in one end of the chip, which identifies the top of the chip. Pin 1, which is the first analogue input, is located in the top left of the chip if we look at the chip with the dip at the top.

Connect the MCP3008 to your Raspberry Pi as per the diagram shown below. The potentiome­ters are connected to 3V and GND rails, and their output is fed into the first two pins of the MCP3008. Our push button is connected to the GPIO and to the GND rail.

Next, insert the official Raspberry Pi camera into the camera port, which is located either between the Ethernet and HDMI port of the model B series, or on the end of the Pi Zero board – note that this requires a special adapter. The ribbon cable should be carefully inserted with the blue tab that’s facing the Ethernet port, and remember to lift the plastic guard on the port before inserting, then close the guard to lock the camera in place. Applying a small blob of plasticine will prevent the camera from making contact with the GPIO pins, which could damage the camera. Now connect your keyboard, mouse and so on, and power up your Raspberry Pi to the desktop.

Getting started

The official Raspberry Pi camera and our MCP3008 both need their interfaces configurin­g. To do this go to the Raspberry Pi Configurat­ion tool, which is located in the Preference­s menu. In the Interfaces tab, enable the Camera and SPI interface. Save your configurat­ion and the applicatio­n will now ask you to reboot. Do so and return to the Raspbian desktop.

To test the camera, open the Terminal and enter this command to take a photo which will be output as image.jpg: $ raspistill -o image.jpg

If this doesn’t work, repeat the configurat­ion, power down the Pi and check that your cable is inserted correctly.

Now to write some code. Go to the Programmin­g menu and click Python 3. When the applicatio­n opens click File>New to create a new blank file. Immediatel­y save the file (File>Save), as timelapse-controller.py before carrying on. We start the code by importing the libraries that we will be using. These are picamera, used to control the camera; GPIO Zero for our analogue-to-digital converter and the push button; the sleep function is imported from the time library; and we’ll need datetime to create a timestamp later. from picamera import PiCamera from gpiozero import MCP3008, Button from time import sleep from datetime import datetime

Now we need to set up the channel connection­s to the MCP3008, tell the GPIO where our push button is connected and create an object from the picamera library that will make it easier to work with. pot1 = MCP3008(channel=0) pot2 = MCP3008(channel=1) button = Button(17) camera = PiCamera()

To capture our images we shall create a function that can be called later in our code. The function, called capture, starts with a for-loop that will iterate for a user-controlled number of times. The for-loop will create a timestamp, the time and date at that moment, and then use that value as a filename to capture an image. The loop then sleeps for another usercontro­lled duration before repeating.

def capture(): for i in range(timer): timestamp = datetime.now().isoformat() camera.capture('/home/pi/%s.jpg’ % timestamp) sleep(delay)

In order to see what the camera sees, we create a preview window. In this case this window has a fixed resolution of 640x480 and is placed in the top left of the screen (0,0). But typically the preview is fullscreen.

camera.start_preview(fullscreen=False, window =(0, 0, 640, 480))

Try and try again

Using a try, except, finally constructi­on to handle any errors and the user exiting the code, we create two variables: timer and delay. The timer variable will store the amount of time in minutes that the timelapse should run for. The delay variable is used to store the delay in seconds between shots. Both variables capture user input from the keyboard, and convert it to an integer. try:

timer = int(input("How long in minutes should the timelapse run for?"))

delay = int(input("How long is the delay, in seconds, between shots?"))

Inside a while True loop, we now take the value of the potentiome­ter, typically between 0.0 and 1.0 and multiply it by 100 to give us a value to control the brightness and contrast of the image. Turning the potentiome­ters will change the value and print the new value to the shell. while True: brightness = round(pot1.value * 100) print("Brightness”,brightness) contrast = round(pot2.value * 100) print("Contrast”,contrast) Use the brightness and contrast variables to update the preview window, showing the change in configurat­ion. We then create the “settings” variable that contains both values. camera.brightness = brightness camera.contrast = contrast settings = “Brightness: “+str(brightness)+” Contrast: “+str(contrast)

These settings are then printed to the Python shell, before being annotated to the preview window, typically at the top centre of the image. There’s a sleep step of 0.1 second before the process is repeated:

print(settings) camera.annotate_text = settings sleep(0.1)

Now our button is ready to start the timelapse sequence. When the button is held for two seconds, it calls the “capture” function (created earlier). This then captures a time lapse using the timer and delay variables that the user will specify.

button.when_held = capture

We now come to the final part of the project. Here we have the “except” part of the constructi­on. This will handle the user exiting the applicatio­n by pressing Ctrl+C, a keyboard interrupt. It stops the preview and then moves on to the “finally” part where we print that the applicatio­n has exited to the Python shell:

except KeyboardIn­terrupt:

camera.stop_preview() finally: print("TIMELAPSE EXITING") Save the code and when ready click Run>Run Module to start. The preview window will open and you can tweak the brightness and contrast using the potentiome­ters, set the timer and delay, then when ready press and hold the button to start the sequence. Now all you need to do is find a suitable subject to photograph over time.

 ??  ?? The circuit has many parts. Focus on one area and check your wiring before moving to the next bit.
The circuit has many parts. Focus on one area and check your wiring before moving to the next bit.
 ??  ??
 ??  ?? The official Raspberry Pi camera has been designed for the Raspberry Pi and can capture HD 1080p video and high-resolution images.
The official Raspberry Pi camera has been designed for the Raspberry Pi and can capture HD 1080p video and high-resolution images.

Newspapers in English

Newspapers from Australia