Introduction: Automated Macro Focus Rail

About: I am an electronics engineer working in telecommunications in the UK. Generally I work on chip design for digital radio receivers. I like all things arty and consider chip design a bit arty :) Basically I h…

Hello community,

I would like to present my design for an automated macro focus rail. Ok, so the first question what the devil is a focus rail and what is it used for ? Macro or close up photography is the art of imaging the very small. This can be done at varying magnifications or ratios. For example an imaging ratio of 1:1 means the subject be photographed is projected onto the camera sensor at life size. An imaging ratio of 2:1 means the subject will be projected at twice life size onto the sensor and so on ...

A common artefact of macro photography is very shallow depth of field. Whether using dedicated macro lenses, taking standard lenses and reversing them or using bellows generally speaking the depth of field is shallow. Up until relatively recently this has been a creative issue with macro photography. However, it is now possible to create macro images with as much depth of field as you wish by a process called focus stacking.

Focus stacking involves taking a series or "stack" of images at different focal points from the closest subject point to the furthest subject point. The stack of images is then digitally combined to create a single image with much deeper depth of field. This a fantastic from a creative point of view as the photographer can choose how they wish their image to appear and how much should be in focus to achieve maximum impact. The stacking can be achieved is various ways - it is possible to use Photoshop to stack or a dedicated piece of software such as Helicon Focus.

Step 1: Focus Rail Principle and Design Criteria

The principle behind the focus rail is quite straight forward. We take our camera and lens and mount them on a high-resolution linear rail that permits the camera/lens combination to be moved closer or farther away from the subject. So, using this technique we are not touching the camera lens, other than maybe to achieve initial foreground focus, but are moving the camera and lens with respect to the subject. If we consider the lens depth of field to be shallow this technique generates focus slices at various points through the subject. If the focus slices are generated such that the depth of field slightly overlaps, they can be digitally combined to create an image with continuous focus depth across the subject.

Ok, so why move the big heavy camera and lens and not the relatively small and light subject of interest ? Well the subject might very well be alive, say an insect. Moving a living subject when you are trying to keep it still may not work too well. In addition, we are trying to keep consistent lighting from one shot to the next so moving the subject would mean moving all the lighting too to avoid moving shadow.

Moving the camera and lens is the best approach.

Step 2: My Focus Rail Main Design Features

The focus rail I have designed carries the camera and lens on a sturdy motor driven mechanical linear rail. The Camera can be easily attached and removed using a quick release dove tail mount.

The mechanical rail is driven in and out using a computer controller stepper motor and can provide a linear resolution of approximately 5um which I personally think is more than adequate most scenarios.

The control of the rail is achieved using a simple to use PC/Windows based user interface or GUI.

Position control of the rail can also be achieved manually using a rotary control with programmable resolution situated on the motor control board (although it could be positioned anywhere, say as a hand control).

The application firmware running on the control board microprocessor can be re-flashed via USB mitigating the need for a dedicated programmer.

Step 3: The Focus Rail in Action

Before getting into the detail of construction and build let us look at the focus rail in action. I have taken a series of videos details different aspects of the design - they may cover some aspects out of order.

Step 4: Focus Rail - the First Test Shot I Obtained From the Rail

At this stage I thought I would share a simple image obtained using the focus rail. This was essentially the first test shot I took once the rail was up and running. I simply grabbed a small flower from the garden and popped it on a piece of wire in order to support it infront of the lens.

The composite flower image was a composite of 39 separate images, 10 steps per slice across 400 steps. A couple of images were discarded prior to stacking.

I have attached three images.

  • The final focus stacked shot output from Helicon Focus
  • The Image on top of the stack - forground
  • The image on the bottom of the stack - background

Step 5: The Control Board Detail and Walk Through

In this section I present a video detailing the motor control board component parts and construction technique.

Step 6: The Control Board Manual Step Control

In this section I preset another short video detailing the manual control operation.

Step 7: Control Board Schematic Diagram

The image here shows the control control board schematic. We can see that by utilizing the powerful PIC microcontroller the schematic is relatively simple.

Here is a link to a high resolution schematic :

Step 8: PC Based User Interface Software or GUI

In this section I again use a video to demonstrate the PC based application control software often referred to as a GUI (Graphical User Interface).

Step 9: Principle and Operation of the Bootloader

Although not not related in any way to the focus rail operation the bootloader is an essential part of the project.

To reiterate - what is a bootloader ?

The purpose of a bootloader is to allow the user to reprogram or reflash the main application code (in this case the Focus Rail application) without the need for a dedicated specialized PIC programmer. If I were to distribute pre-programmed PIC microprocessors and needed to issue a firmware update the bootloader allows the user to reflash the new firmware without either having to buy a PIC programmer or return the PIC to me for a reflash.

A bootloader is simply a piece of software running on a computer. In this case the bootloader is running on the PIC microcontroller and I refer to this as firmware. The bootloader could be located anywhere in program memory but I find in more convenient to locate it right at the start of program memory within the first 0x1000 byte page.

When a microprocessor is powered up or reset it will start program execution from a reset vector. For the PIC microprocessor the reset vector is located at 0x0 and normally (without a bootloader) this would either be the start of the application code or a jump to the start depending on how the code is located by the compiler.

With a bootloader present following power up or reset it is the bootloader code that is executed and the actual application is located higher up in memory (termed relocated) from 0x1000 and above. The first thing the bootloader does is check the status of the bootloader hardware button. If this button is not pressed the bootloader automatically transfers program control to the main code in this case the Focus Rail application. From the users point of view this is seamless and the application code just appears to execute as expected.

However, if the bootloader hardware button is pressed during power up or reset the bootloader will attempt to establish communication with the host PC in our case via the radio serial interface. The PC bootloader application will detect and communicate with the PIC firmware and we are now ready to start a reflash procedure.

The procedure is straightforward and is conducted as follows:

  • The maunal focus button is depressed while hardware is powered up or reset.
  • PC application detects PIC bootloader and green status bar displays 100% plus PIC detected message is displayed.
  • User selects 'Open Hex File' and using the file chooser navigates to the new firmware HEX file.
  • User now selects 'Program/Verify' and the flashing process starts. First the new firmware is flashed by the PIC bootloader and then read back and verified. Progress is reported by the green progress bar at all stages.
  • Once program and verify is complete the user presses the 'Reset Device' button (bootloader button not pressed) and the new firmware begins execution.

Step 10: PIC18F2550 Microcontroller Overview

There is far too much detail to go into with regard to the PIC18F2550. Attached is the data sheet top level specification. If you are interested the entire datasheet can be downloaded from the MicroChip website or just google the device.

Step 11: AD4988 Stepper Motor Driver

The AD4988 is a fantastic module, perfect for driving any four wire bipolar stepper motor up to 1.5A.

Low RDS (On) Output
Automatic current decay mode detection / selection
Mix with slow current decay modes
Synchronous rectification for low power dissipation
Internal UVLO
Cross-current protection
3.3 V and 5 V compatible logic supply
Thermal shutdown circuitry
Ground fault protection
Load short-circuit protection
Optional step five models: full, 1/2, 1/4, 1/8 and 1/16

Step 12: Mechanical Rail Assembly

This rail was picked up from eBay for a great price. It is very robust and well made and came complete with stepper motor.

I found mine on eBay a good search line is : linear rail slide z

Step 13: Project Summary

I have very much enjoyed designing and building this project and have ended up with something I can actually use for my macro photography.

I tend to only build things that are of practical use and that I will personally use. I am more than happy to share far more design detail than has been covered in this article including programmed tested PIC controllers if you are interested in building a macro focus rail for yourself. Just leave acomment or private message me and I will get back to you. Many thanks for reading, I hope you enjoyed ! Best Regards, Dave