Focus Stacking with the Raspberry Pi

If you are into photography, a flatbed scanner and the popular single board computer, the Raspberry Pi or RBPi, can help you to focus stacking images in macro photography. After re-purposing an old flatbed scanner, David Hunt is using it as a macro-rail controlled by the SBC, RBPi.

Those who shoot macro photography are aware of the common issue of depth of focus limitation that shows up as the depth of field limitation in the photograph. Depending on the magnification you are trying to achieve and the camera settings, the depth of focus can be as small as 0.5mm. One solution is to stack together several images of a subject, with each image focusing on a different part of the object.

To do this with commercial solutions may set you back by as much as $600. The difficulty lies in moving the camera closer to the subject in extremely small increments, but with great accuracy. The sharp parts of the images are combined together using free software such as CombineZM, resulting in a completely sharp image of the subject right from front to back.

David Hunt decided to solve the problem with an old flatbed scanner that was lying in his attic gathering dust. Capable of 2400 dpi, the scanner had not been used for over a couple of years.

Even the drivers available for it worked only on Windows XP. Although accurate enough, David was doubtful if the machine would be capable of moving a 3Kg camera and lens combination. He decided to use the stepper motor and drive the scan element in very small increments, with the camera attached to it – it would be ideal for macro photography.

Scanners typically come with a nice flat platform on which a camera can be placed. Driving the platform forward and back requires a stepper motor that has its own drive electronics and has to be driven externally. The drive is slow, so it will let the camera remain steady while it moves. A camera with a shutter release mechanism will be useful, as you will have to take a number of snaps.

H-bridge stepper motor drives are efficient and easy to use. David used a drive capable of handling 2 DC motors or 1 stepper motor with two coils. For powering the motors and the drive, David used 3x AA type batteries. Therefore, he was able to connect four GPIO pins from the RBPi to control the drive and the motor. However, driving the motor through opto-couplers would have provided more safety for the RBPi.

The binary sequence of 1000, 0100, 0010 and 0001, when repeated, will drive the motor forward one-step at a time. The same sequence, repeated in reverse, will allow the motor to move back one-step at a time. David programmed the RBPi to generate these sequences repeatedly while he added an additional circuit for releasing the camera shutter between each movement of the platform.

With the above contraption, David can move his camera forward towards the subject in the smallest increments of 0.02mm, and take images at each increment.