Macro Pi – Focus Stacking using Raspberry Pi

Here’s another in the series of articles of photographic uses for the Raspberry Pi SBC (Single Board Computer). This time, it’s re-purposing an old flatbed scanner as a macro rail for focus stacking images in macro photography.

Focus Stacking using Raspberry Pi

The Plan

There’s a common issue with shooting macro photography, and that’s the limitation in depth of field (or depth of focus). It can be as little as 0.5mm, depending on the camera settings and magnification you’re trying to achieve. A solution to this is to take several images, each one moving closer to the subject by a tiny amount. Each image will have a different part of the subject in focus. You can then combine all the sharp parts of the images together using some free software called CombineZM, and end up with a completely sharp image from front to back of the subject. The difficulty with this is moving the camera accurately at such small distances. Once solution is to turn the zoom ring on your lens slightly, but that’s still hit-and-miss on highly magnified images (1:1 and smaller). Commercial solutions can cost as much as $600, including controller.

So, I came up with the idea of re-purposing an old flat-bed scanner that I had in the attic gathering dust. It’s so old, that the most recent drivers available for it are for Windows XP. So it hasn’t been used in a couple of years. Being a scanner capable of 2400 dpi, it was definitely accurate enough, but would it be able to move a 3KG camera and lens? If I could get at the stepper motor to drive the scan element, maybe I could attach a camera to it and move it in very fine increments, ideal for macro photography.

Once I took the cover and the glass off, I was left with a nice flat platform on which to place my camera, and a 4-wire connection to the stepper motor to drive the platform forward and back. The stepper motor drive can be seen in the above image in the bottom right.

Mounting the camera is simply a case of placing it on the platform. The drive is slow enough that it does not dislodge the camera while it moves. The camera shown is a Canon EOS 5D Mark II, but it should work with Nikon, Sony, etc. Any camera with a shutter release mechanism.

The Circuits

This project was made much easier by using an off-the-shelf stepper motor drive unit (see pic). It’s a dual H-Bridge motor drive unit. I had it off eBay for €9. It has the capability to drive 2 DC motors, OR 1 stepper motor with 2 coils. In this project, we’re driving one stepper motor with two coils.

So, connecting in the 4-wire stepper motor interface into the MOTORA and MOTORB connections, and adding in a PSU in the form of  3 x AA  batteries to drive the motor, I then connected 4 GPIO outputs of the Raspberry Pi directly onto the 4 inputs to the stepper motor driver board. I wired the EnableA and EnableB pins to the +5V, as I want both coils in the motor to be enabled, and saved myself a few GPIO pins in the process.

The sequence I’m using to drive forward is 1000, 0100, 0010, 0001 (repeating), and backwards, is 0001, 0010, 0100, 1000 (repeating). You can see in the sequence that it’s enabling one of the coils on the motor at a time, causing it to rotate.

Focus Stacking using Raspberry Pi schemetic

With a small bit of code, I was then able to drive the platform in the scanner forward and backwards ad various speeds, depending on how fast I ran the sequence to the motor (see below for the code)

The next step was to drive the shutter of the camera between each move of the platform. This was done using the following circuit.

So, now I can move the camera towards the subject in increments as little as 0.02 mm, taking an image each increment. That’s incredible resolution for something this easy to build!

The Code

Here’e a snippet of python code to move the motor a number of steps, and trigger the shutter.  Run this script for each frame in your stack. I guess you could stick a loop around it if you know how many frames you want to take, but I wanted to keep it simple for this article.

import sys
import wiringpi
from time import sleep

# Parameters:
#     direction (0 or 1)
#     number of steps (int)
#     delay between steps (float)

gpio = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_GPIO)  

motor_pin_a = 0
motor_pin_b = 1
motor_pin_c = 4
motor_pin_d = 14
shutterpin = 17

gpio.pinMode(motor_pin_a,gpio.OUTPUT)
gpio.pinMode(motor_pin_b,gpio.OUTPUT)
gpio.pinMode(motor_pin_c,gpio.OUTPUT)
gpio.pinMode(motor_pin_d,gpio.OUTPUT)
gpio.pinMode(shutterpin,gpio.OUTPUT)

For more detail: Macro Pi – Focus Stacking using Raspberry Pi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top