Tải bản đầy đủ (.pdf) (10 trang)

Practical Arduino Cool Projects for Open Source Hardware- P7 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (167.31 KB, 10 trang )

CHAPTER 3  TIME-LAPSE CAMERA CONTROLLER

Figure 3-2. Schematic for non-camera-specific portion of the circuit
Instructions
Assemble Prototyping Shield
Control connections to the camera are made using a pair of reed relays that provide complete electrical
isolation between your Arduino and the camera itself. In fact, this project has been designed to use the
same pins as the first two outputs on the relay board used in the very first project, Appliance Remote
Control, so that if you’ve already built that project, you can use the same board without modification.
That’s exactly what we did, and the only difference is that rather than wiring the 3.5mm stereo socket
directly to the relay outputs, we needed to connect it to a pair of headers to plug into the relay outputs.If
you’re building a shield specifically for this project you can start by following the instructions for the
Appliance Remote Control project in Chapter 2, but only fit the first two relays that are connected to
Arduino digital I/O lines 5 and 6 along with their matching reverse-biased diodes.
Then wire up the 3.5mm stereo socket so that the outer (shield) connection goes to one terminal on
both relays, the center (ring) connection for the focus trigger goes to the other terminal of the relay on
digital pin 5, and the tip connection for the shutter trigger goes to the other terminal of the relay on
digital pin 6.
The result is a 3.5mm stereo socket with one common connection and a pair of independently
switched connections that have one link in common. The two relays will then be connected to “focus”
and “shutter release,” respectively, in the camera.
Connect Camera Shutter Release
There are several different methods shown here for controlling the shutter release of your camera, and
which one you use will depend on the camera itself.
39
CHAPTER 3  TIME-LAPSE CAMERA CONTROLLER
Remote Shutter Release Connector
If you are exceptionally lucky, your camera will have a remote shutter release connector. If this is the
case, your job is made easy and you can assemble a cable that links your Arduino directly to your
camera. Many Canon, Olympus, and Panasonic cameras, in particular, have a shutter release connector.
Because there’s no standardization among manufacturers, you may have to do a bit of research for your


particular camera to figure out what the connections are.
Most remote shutter release connections have provision for both focus and shutter (“shoot”)
control. This allows you to tell the camera whether to activate auto-focus when taking the photo, which
is usually what you want.
Panasonic Cable Connection
Panasonic cameras, such as the FZ series, use a 4-conductor 2.5mm connector that can be a bit hard to
find. It looks like a smaller version of the 3.5mm connector used on earbuds and some headphones, but
with four connections rather than three. Despite requiring a 4-conductor jack, they only use two of the
connections, and also require a set of external resistors to indicate to the camera what command you
want to send to it (see Figure 3-3).


Figure 3-3. Manual control connection for Panasonic cameras
The Panasonic connection puts a resistance of around 62K across the connections when plugged in
so that the camera can detect it. When the “focus” switch is activated, the resistance falls to around 29K,
and when the “shoot” switch is activated the resistance falls to 1K8.
To make a cable to connect a Panasonic camera to your time-lapse controller, start by stripping
back one end of the shielded cable and soldering the shield to the ring connection on a 2.5mm 4-
connection plug. Then solder the cable’s internal conductor to the first ring, the one adjacent to the
sleeve.
The other end of the cable is a bit tricky to assemble. Start by soldering the shield to the sleeve
connection on a 3.5mm stereo line plug, then fit a 33K resistor so that it goes from the sleeve connection
to the ring connection. Next, connect the 27K resistor from the ring connection to the tip connection.
Finally, connect the internal conductor in the cable to one end of the 1K8 resistor, and the other end of
the resistor to the tip connection (see Figure 3-4). If you keep all the connections neat and tight and use
some heat-shrink tubing as insulation at appropriate points, you should be able to make the whole
assembly small enough to fit within the plastic cover for the plug.

40
CHAPTER 3  TIME-LAPSE CAMERA CONTROLLER


Figure 3-4. Automatic control cable for Panasonic cameras
Canon Connection
Many Canon cameras use a similar system to the Panasonic remote shutter release, but with a
commonly available stereo 3.5mm connector instead. The Canon system uses all three connections in
the plug—one for ground and the other two for the “focus” and “shoot” switches—making it even
simpler to assemble (see Figure 3-5). In fact, all you need to build your own Canon external shutter
release is a 3.5mm plug, two push buttons, and some wire. In this project, we’ll use the same circuit, but
rather than using manual switches, we’ll make a cable that connects the camera to the reed relay
outputs to control focus and shoot.


Figure 3-5. Manual control cable for Canon cameras
Strip back one end of the shielded cable and solder it onto a 3.5mm stereo line plug, using the shield
for the sleeve connection and one of the internal conductors for the ring next to it. Connect the other
conductor to the tip. Our personal preference is to be consistent with the audio color codes typically
used in 3.5mm plugs and connect the red conductor to the ring (the “focus” connection), and white to
the tip (the “shoot” connection).
Then strip back the other end of the shielded cable and solder on another 3.5mm stereo line plug
using the exact some connections, forming a “straight through” cable that connects the sleeve on one
41
CHAPTER 3  TIME-LAPSE CAMERA CONTROLLER
plug to the sleeve on the other, the ring on one to the ring on the other, and the tip on one to the tip on
the other (see Figure 3-6).


Figure 3-6. Automatic control cable for Canon cameras
In both Panasonic and Canon cables, the remote shutter release mechanism uses two outputs from
the time-lapse controller, and as far as the software is concerned, they behave in exactly the same way.
The only difference is the cable you need to assemble to connect the Arduino to the camera.

Infrared Remote Control
If you’re a bit lucky, your camera will have an infrared remote control intended to allow you to control it
from a distance, such as when it’s sitting on a tripod and you want to take a group photo while you’re in
the group. Even if your camera didn’t ship with a remote control, it may have an IR receiver intended for
use with an optional remote that can be purchased separately.
Many digital video cameras come with an IR remote control and are capable of taking good quality
still photos onto an SD memory card, so you might find that a digital video camera is a good alternative
to using a regular digital camera.
If your camera came with a remote control, you can open it up and connect the “shutter” reed relay
output on the shield across the terminals of the appropriate button in a similar way to the Appliance
Remote Control project. A simple cable made using a 3.5mm stereo line plug and a mono shielded cable
is all you need (see Figure 3-7). This could allow you to trigger the shutter without modifying the camera
at all.
An alternative approach is to connect an IR LED to your Arduino and have it emulate a remote
control by sending the same code as a factory control. For Nikon cameras, the easiest way to do this is
using the nikonIrControl library that you can download from www.vonroth.com/Arduino/NikonIrControl.

42
CHAPTER 3  TIME-LAPSE CAMERA CONTROLLER

Figure 3-7. Automatic control cable for I/R remotes
Extract the library and install it into your Arduino environment by placing it into the
hardware/libraries directory in your Arduino directory and then clicking Sketch > Import Library and
choosing the library (see the section “Writing an Arduino Library” in Chapter 16 for more details). Then
plug the anode lead (the longer lead) of an IR LED into pin 13, and the cathode (shorter lead) into
ground. You can then trigger a Nikon camera with a very simple program, like the on that follows, which
takes one photo per second.

#include <nikonIrControl.h> // Use the nikonIrControl library
int CameraIrPin = 13; // IR LED connected to digital pin 13

void setup()
{
pinMode(CameraIrPin, OUTPUT); // Sets the digital pin as output
}
void loop()
{
cameraSnap(CameraIrPin); // Send the IR code to take a photo
delay(1000); // Wait 1 second
}

Other brands handle IR control in different ways, so if you have a different type of camera you might
need to do a bit of research to see if there is a similar library for your camera.
Modify Camera
If you’re not at all lucky, your camera has no built-in method for triggering it externally and no IR remote
control, so you’ll need to open it up and find the connections to the shutter release button so you can
solder wires onto them. Most cameras have a multistage shutter-release button that activates auto-focus
when pressed half way, then takes a photo when fully depressed.
Make up a cable similar to the one described previously for connection to an IR remote control, and
open up your camera to find the connections to the shutter release button. Obviously every camera is
different in this respect, so you’re pretty much on your own in terms of finding the appropriate
43
CHAPTER 3  TIME-LAPSE CAMERA CONTROLLER
connections. A bit of careful investigation with a multimeter and some tests with a short length of wire to
bridge the button connections should help you quickly find the correct connections.
Solder the end of the cable to the button connections and you should be ready to go.
Configure Camera
To keep your camera running for days, weeks, or even months, you will need to provide an external
power source and disable the camera’s power-saving mode so it doesn’t turn itself off.
Many cameras have an external power socket, so it may be as simple as finding an appropriate wall
wart power supply and plugging it in. Alternatively, you might be able to solder leads to the battery

connectors inside the camera and connect a socket to match a power supply that runs at the same
voltage as the batteries. For example, most digital cameras use two AA batteries in series to provide 3V,
so a 3V wall wart with the plug cut off might be just what you need. If you can’t get a soldering iron onto
the battery terminals, you can even use some old batteries and put tape over the ends so you can use
them to physically jam wires against the battery terminals while bypassing the batteries themselves.
Go into the settings for your camera and disable power-saving mode so that it doesn’t turn off after
a period of inactivity. If your camera doesn’t allow you to do that, set it to the longest setting possible
and make sure that you take photos more often than that interval to prevent it from going to sleep.
And while you’re adjusting the camera settings, remember to disable the audible feedback that the
camera makes when it takes a photo. Otherwise it will probably drive you insane after the first ten
minutes!
Calculate Photo Interval
It’s quite straightforward to calculate how often the camera needs to take a photo. Start by deciding how
long your final movie is likely to be and what framerate it will run at: a 2 minute movie running at 25
frames/second, for example, will require a total of 2 × 60 × 25 = 3000 frames. Then consider how long the
sequence you’re filming is likely to take and convert it to seconds: a tree losing its leaves over the space
of a week takes 7 × 24 × 60 × 60 = 604800 seconds. Divide the second number by the first to get the image
interval, which in this example is 604800 / 3000 = 201 seconds. That’s how often you need the camera to
take a photo to end up with a movie of the right length.
Keep in mind there might be periods you’ll need to cut out of your sequence, such as photos taken
at night, so remember to factor that into your calculation.
Configure and Load Sketch
The sketch for this project is incredibly simple. All it does is set up digital I/O lines 5 and 6 as outputs for
the “focus” and “shutter” reed relays, respectively, and make sure they are set low. It then does the same
for digital I/O line 13 to use the onboard LED as an activity indicator, and then goes into an infinite loop
that triggers both outputs for half a second at a configurable interval.
The only tunable parameter is the delay interval that needs to be set in seconds at the top of the
sketch.

int frameInterval = 300; // Delay between pictures (in seconds)

int focusPin = 5; // Reed relay on digital pin 5

int shutterPin = 6; // Reed relay on digital pin 6

44
CHAPTER 3  TIME-LAPSE CAMERA CONTROLLER
int ledPin = 13; // LED connected to digital pin 13

void setup()
{

pinMode(focusPin, OUTPUT); // Set the focus pin as an output
digitalWrite(focusPin, LOW);
pinMode(shutterPin, OUTPUT); // Set the shutter pin as an output
digitalWrite(shutterPin, LOW);
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
digitalWrite(ledPin, LOW);
}
void loop()
{
digitalWrite(ledPin, HIGH); // Turn on activity LED
digitalWrite(focusPin, HIGH); // Turn on focus relay
digitalWrite(shutterPin, HIGH); // Turn on shutter relay
delay(500); // Hold the button for 1/2 second
digitalWrite(ledPin, LOW); // Turn off activity LED
digitalWrite(shutterPin, LOW); // Turn off shutter relay
digitalWrite(focusPin, LOW); // Turn off focus relay
delay(1000 * frameInterval); // Wait the required interval before repeating
}


While this simple delay-loop approach works perfectly well for simple projects like this, it can
become a problem if you want to add a bit of intelligence to the system later. Whenever the Arduino is
waiting for the delay() function at the end of the loop to finish, it is effectively locked: it can’t process
other commands even though the CPU isn’t doing anything useful at the time.
It’s good practice to avoid unnecessary delay loops in your sketches if possible, so we’ve also
included another version of the same sketch that has been written using the Aiko application framework.
Aiko is an Arduino library that is designed to make it easy to write “event-driven” sketches consisting of
many subsections that may either run independently or interact with each other in a complex way.
Although it’s overkill for this little project, it’s a good exercise to compare the simple delay-loop
approach shown previously to a version of the same sketch written using Aiko.
To use Aiko in your sketches, you need to download and install it using the instructions shown at
the development repository at github.com/geekscape/Aiko.
Once it’s installed you’ll be able to compile the following “Aiko-ised” version of the sketch.

#include <AikoEvents.h>
using namespace Aiko;
int frameInterval = 300; // Delay between pictures (in seconds)
int focusPin = 5; // Reed relay on digital pin 5

int shutterPin = 6; // Reed relay on digital pin 6
int ledPin = 13; // LED connected to digital pin 13
void setup()
{

pinMode(focusPin, OUTPUT); // Set the focus pin as an output
digitalWrite(focusPin, LOW);
pinMode(shutterPin, OUTPUT); // Set the shutter pin as an output
45
CHAPTER 3  TIME-LAPSE CAMERA CONTROLLER
digitalWrite(shutterPin, LOW);

pinMode(ledPin, OUTPUT); // Set the LED pin as an output
digitalWrite(ledPin, LOW);

Events.addHandler(takePhoto, frameInterval * 1000); // Every 'frameInterval' seconds
}
void loop()
{
Events.loop();
}
void takePhoto()
{
digitalWrite(ledPin, HIGH); // Turn on activity LED
digitalWrite(focusPin, HIGH); // Turn on focus relay
digitalWrite(shutterPin, HIGH); // Turn on shutter relay
delay(500); // Hold the button for 1/2 second
digitalWrite(ledPin, LOW); // Turn off activity LED
digitalWrite(shutterPin, LOW); // Turn off shutter relay
digitalWrite(focusPin, LOW); // Turn off focus relay
}

All programs using the Aiko framework start by including the Aiko library itself and declaring the
Aiko namespace. This step allows the rest of the program to use the features provided by Aiko.
The program then uses the same configuration values as the original sketch, and also sets up the
digital outputs in the same way.
The differences begin on the last line of the setup() function where the program calls Aiko’s
Events.addHandler() function to define an event handler that needs to be called at a regular interval. In
this case, the event handler is the takePhoto() function and we declare that we need it to be executed
every “frameInterval” seconds. Because Aiko’s time-based event handler needs the delay to be defined
in milliseconds, we multiply the frameInterval value by 1000 so it will be set correctly.
You’ll notice that the loop() function in the Aiko version is dramatically simplified: instead of

performing the delay and controlling the digital outputs inside the main program loop, all we need to do
is have a call to Events.loop(), another Aiko function that ensures any event handlers due to be executed
are processed as required.
Finally, you can see that we have defined a new function called takePhoto(), which is the function
declared as the event handler earlier in the program using Events.addHandler(). Every time the event
handler comes due for execution (every 300 seconds, in this case) it will be invoked automatically by the
Events.loop() line inside the main loop.
This approach sounds more complicated, but it gains us a very important advantage: whenever the
Arduino is not actually executing the takePhoto() function it is free to do other things. The CPU is not
bound in a delay() function for the entire time between taking photos, so you can easily extend the
program to add other functionality independently of clicking the shutter on your camera.
The following trivial extension to the Aiko version of the program shows how easy this is.

#include <AikoEvents.h>
using namespace Aiko;
int frameInterval = 300; // Delay between pictures (in seconds)
int focusPin = 5; // Reed relay on digital pin 5

int shutterPin = 6; // Reed relay on digital pin 6
int ledPin = 13; // LED connected to digital pin 13
46
CHAPTER 3  TIME-LAPSE CAMERA CONTROLLER
void setup()
{

pinMode(focusPin, OUTPUT); // Set the focus pin as an output
digitalWrite(focusPin, LOW);
pinMode(shutterPin, OUTPUT); // Set the shutter pin as an output
digitalWrite(shutterPin, LOW);
pinMode(ledPin, OUTPUT); // Set the LED pin as an output

digitalWrite(ledPin, LOW);
Events.addHandler(takePhoto, frameInterval * 1000); // Every 'frameInterval' seconds
Events.addHandler(blinkLed, 5000); // Blink status LED every 5 seconds
}
void loop()
{
Events.loop();
}
void takePhoto()
{
digitalWrite(ledPin, HIGH); // Turn on activity LED
digitalWrite(focusPin, HIGH); // Turn on focus relay
digitalWrite(shutterPin, HIGH); // Turn on shutter relay
delay(500); // Hold the button for 1/2 second
digitalWrite(ledPin, LOW); // Turn off activity LED
digitalWrite(shutterPin, LOW); // Turn off shutter relay
digitalWrite(focusPin, LOW); // Turn off focus relay
}

void blinkLed()
{
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
}

All we’ve done in this example is add another event handler called blinkLed() that flickers the status
LED on for 1/10th of a second, and used another call to Events.addHandler() to set that handler to be
invoked every five seconds. With this sketch your Arduino time-lapse controller will now flicker its status
LED to show an “I’m alive” heartbeat every five seconds, no matter what your photo frequency is set to:

the two event handlers are totally independent and will be called automatically at the correct intervals
by Aiko—much neater than trying to manage the timing of two independent events within a single large
delay loop!
Another advantage which may not be so obvious at first glance is that it makes the photo interval
more consistent: it won’t “drift” due to time used to take the photo itself. If you go back to the first
version of the sketch, you will notice that it won’t actually take a photo every 300 seconds as intended
because there is a small amount of time spent taking the photo itself. What it actually does is take a
photo, wait 300 seconds, take another photo (which itself takes 1/2 a second), wait 300 seconds, and so
on. Each cycle will therefore take just over 300.5 seconds because the program doesn’t subtract the time
spent taking a photo from the delay between photos. The result is that photos will tend to drift over time
until they’re no longer on neat intervals of 300 seconds, 600 seconds, 900 seconds, and so on.
Sure, in this example you don’t really care about a 1/2 second drift, and even if you did care you
could just code around the inaccuracy by figuring out how long each photo takes and then have the
47
CHAPTER 3  TIME-LAPSE CAMERA CONTROLLER
program subtract that value from the frameInterval period, but with the Aiko version this problem
simply doesn’t exist. If you set an Aiko event to be executed every 300 seconds, then it will be executed
every 300 seconds, even if the event itself takes some time to complete.
Check Memory Capacity
Check that the memory in your camera is large enough to handle the number of photos you need to take
based on the previous calculation. SD memory cards are very cheap these days, so a 2GB or 4GB card
should give you enough storage for thousands of photos for just a few dollars. Try adjusting the image
quality/resolution setting to a medium level to dramatically increase the number of frames the camera
can store compared to a typical high-resolution setting, which is probably overkill anyway since the
resolution of the final movie will be much less than a typical still photo.
Set Up Your Shoot
When taking a series of photos over a long period of time, it’s critical that the camera remain as still as
possible. Even slight movement can ruin the effect, so mount the camera very firmly using a tripod or
other solid mount.
Process the Images

When it’s all over, transfer all the images from your camera to a computer either using the cable supplied
with the camera or by removing the memory card and connecting it to your computer using a card
reader. Once you have the images in a folder on your computer, you need some software to process
them all and convert them into a movie, but of course the exact process will depend on what operating
system you run and what end result you’re trying to achieve.
Once you’re done and have a cool time-lapse movie, make sure you share it with the world! Upload
it to a video-sharing web site like YouTube or Vimeo and tag it with “practicalarduino,” and then let us
know about it so we can link to it from the Practical Arduino site.
Linux
Most Linux distributions have a command-line image-processing suite called “convert” available, so the
simplest approach for creating a movie is to open a terminal, go into the folder containing your images,
and type the following:

convert *.jpg timelapse.mpg

This will take all images with a lowercase .jpg extension (adjust the command as necessary to suit
the naming scheme used by your camera) and create an MPEG movie called “timelapse.” You can then
play the movie with whatever media player is provided, such as Kino, MPlayer, Xine, or VLC.
Windows
A free program called PhotoLapse can take a sequence of JPEG image files and convert them into an AVI
movie file. You can download PhotoLapse free from the author’s web site at
48

×