In the gallery are three images of a fly (by Muhammad Mahdi Karim - own work, CC BY-SA 3.0). The first is a single image, focused close, and the second is focused far. The third is a "stacked" set of six images, taken at different focal points and combined in software to create an image with a deep depth of field that shows the entire fly in focus.

Some modern (and generally expensive) cameras have a feature called "focus bracketing", where they can automatically take a series of images at different focal points over a specified range. A few of these cameras can create the stacked (combined) image in-camera, but many require that the stacking be done by a program on an external device (computer or smartphone app)—and the external method is generally better in either case. This in-camera focus bracketing has some limitations, and is best used with images no more magnified than about 1x ("life size"). For higher magnifications, especially when using microscope lenses (which may reach 50x or more), it is far better to keep the camera's focus fixed and moved the subject relative to the camera to select the focal point of each individual image that will be stacked.

Under perfect conditions (lots of light for short exposures, and a steady hand), it may be possible to take adequate source images with a handheld camera, especially if only a few are needed (low magnification). But it's better to mount the camera (or subject—there are advantages to each way) on a rail with a moveable sled that can be precisely positioned, and this is absolutely essential at higher magnifications where the movement distance may be on the order of millimeters all the way down to micrometers (millionths of a meter, commonly called "microns"). These devices are called "focus rails".

A focus rail may be either manual (with the positioning done by turning a knob, and the camera shutter triggered by hand for each shot) or powered (where the sled is positioned using a stepper motor, and the camera is automatically triggered by the same device that powers the stepper—called the "controller"). A reasonably-good manual focus rail might cost around US$100, whereas a powered one will cost at least a few hundred, and in some cases thousands, of dollars. Generally, the automatic one will have its software and features locked down—something that everyone here loathes, of course. But with some effort—a little or a lot, depending on how ambitious you are about the cost and the results—anyone can build their own automatic focus rail.

Before I go any farther about the hardware, a note on the software. When one stacks (combines) multiple images into one, it is absolutely essential that the source images be precisely aligned with each other and that details like exposure be consistent. Otherwise, the result will just be a blurry mess that is even worse than the worst source image. If you're feeling crazy you could try adjusting & combining the images manually using pretty much any image editing software, but if there are more than two or three source images that is going to get old very quickly. But Photoshop (for example) has a built-in stacking function, and you may find that's all that you need. However, in more complex situations (especially if you're combining hundreds of source images, which is likely the case with macro photography at higher magnifications), you may be happier using a dedicated, more sophisticated program like Helicon Focus or Zerene Stacker (I'll leave the discussion about which is better to someone else).

One important consideration for the hardware is whether you're going to mount the camera, or the subject, on the moveable sled, while keeping the other in a fixed position. For a large subject, especially one that you can't move (say, something in a natural setting), your only option is to put the camera on the sled. But for small, portable objects you have a choice, and, in general, I recommend moving the subject rather than the camera. Vibration is enemy #1 in macro photography, and after each movement you need to make sure that it has died down before exposing the shot, which could potentially involve a waiting time of a few seconds or longer. Your massive camera has a lot more inertia than a tiny bug on a toothpick, and may take longer to completely settle down. Also, moving the camera introduces a slight risk of shifting it on its mount, ruining its alignment with the subject. But the choice really depends on the type of subject you're photographing, and the nature of your camera rig, so you may need to do some experimenting to see which way works better for a particular shot.

Now, to the hardware for my project. First off, I want to say that while there are ways to construct an automated focus rail at (relatively) low cost, that's not what I'm going for here. Instead, I'm using the same heavy-duty components that I use for my CNC machine builds, because I have lots of them on hand and I'm familiar with them. In particular, I'm using a general-purpose multi-axis controller to drive the stepper motor, when only a single axis is actually needed for this project. Also, rather than using a program dedicated to this focus rail function, my plan, which I haven't yet developed very far, is to generate G-code (the language used by most CNC machines) for the focus rail movement and camera triggering using another program, and feed it to an unmodified CNC controller. This has one big potential benefit: if you already have some sort of CNC machine that can handle the weight of either your camera or the subject you're photographing (the latter is trivial for a lightweight subject like an insect!), you can use it as-is. That means that this is potentially a low-effort, zero-cost project, since only software might be needed.

However, in this case I plan to use hardware dedicated to this project since I expect to use it for a lot of macro photography and I don't want to tie up one of my existing CNC machines. My main purpose for this project is to photograph my many mineral specimens, some of which are quite large and heavy. So, I need a heavy-duty focus rail, and I've chosen to use a beefy "linear actuator" that's designed for powerful CNC machines, specifically the "C-Beam Double Wide Gantry Actuator" from OpenBuilds. This device is available in lengths of 250, 500, and 1000mm, with a travel of 80, 330, and 830mm respectively, and uses a powerful NEMA 23 stepper motor. For pretty much any macro photography application, the 80mm travel of the smallest unit will be way more than enough. However, I have gone with the 500mm version (330mm travel) because the cost difference is small ($198 vs. $178) and I have space to set up the larger rig. OpenBuilds sells a large variety of linear actuators, each of which may have advantages for various types of CNC projects (and this focus rail system is, fundamentally, a CNC project). But the price differences between them are relatively small, so I've opted to go for one of the heavy-duty models, especially since in this case the size and weight of the rail system isn't an issue.,

As for the controller, here I am going with even more overkill—like, 100x. One *could* use a little Arduino-type board that includes a single stepper motor driver and costs only a few dollars—these Chinese imports are dirt-cheap and ubiquitous. However, I am opting for the "BlackBox Motion Controller x32" with the (not strictly necessary) "INTERFACE CNC Touch", both also from OpenBuilds. The total cost of this controller setup, including the required 24V power supply (but not the linear actuator described above) , is about $500. The reason for this choice (and you could make a different one, with little or no change to the software) is that I already have a few of these controller combos from my other CNC projects and I'm very familiar with using them. But even if I didn't already have the controller I would make the same choice, because this is a versatile, very-high-quality system that could be used for just about any other motion-control project when not being used for photography.

So, the total hardware cost for my project is just about $700. This might seem extreme, but it is still lower, in some cases *far* lower, than the cost of most commercial focus rail systems that are nowhere near as powerful and versatile. And again, you could do this project using much lower-cost components, keeping the hardware cost well under $300. The software approach I'm taking makes the specific choice of hardware pretty much irrelevant.

Now, the software. It has two outputs: a step trigger for the rail, and a shutter trigger for the camera (the actual physical connections for these functions are made by the controller discussed above). Calculating the amount of movement for each shot can be a bit complicated. You need to take into account the total depth of field that you want after the shots are stacked (which depends on the type of subject, plus an aesthetic judgment) and the individual depth of field for each individual shot (which depends mostly on the camera lens that you are using). There are charts freely available online to help you make these calculations, and they can serve as the basis for writing a program to do the calculations for you. Additionally, depending on your hardware, you may need to factor in a few other things like backlash compensation for the drive system. In my case, rather than writing a dedicated program from scratch, I plan to use a spreadsheet program (e.g., Excel) to do the calculations and spit out the G-code commands (yes, a spreadsheet really can do that!) to be sent to the controller hardware. Easy-peasy, and doable for even non-programmers who have a basic understanding of spreadsheet programs. But I plan to create the spreadsheet (I mean, unless some other enthusiastic person wants to take it on before I get to it), so there need be no programming effort at all for you if you decide to duplicate this project.

By the way, you may find that this idea for creating a CNC-type photography rig whets your appetite for more sophisticated camera motion control systems. For example, if you mounted your camera on the Z-axis of a standard XYZ CNC machine, you could make cinema-quality fly-through videos with three-dimensional movement (something I plan to try eventually)—and still program it using just a spreadsheet program. So, if you have the table space, budget, and enthusiasm, go wild and get a full CNC machine instead of just a single-axis linear rail (you won't need the usual router, laser, or other cutting device, unless you also plan to do CNC cutting and/or engraving). That setup could be used as just a macro focus rail, then later programmed for any other sort of camera movement. For a focus-stacking system, which uses just linear movement, either the camera or the subject could be mounted on the moveable part (as described above). But for videos with movement in more than one dimension, the camera would have to be the moving component. In that case, keep in mind that the machine needs to be powerful enough to carry the weight of your camera system (which in many cases will be heavier than a photographic subject).

But, if you're only using a lightweight camera (say, a GoPro), then it really doesn't matter how beefy your motion control system is and you could use one of the very common, very cheap (several hundred dollars) CNC machines designed to carry a small device like an engraving laser.

That's it for now. As I said, this is a work in progress, and at the moment (11/28/2023) I'm waiting for the linear actuator that I have on order from OpenBuilds. This is my first attempt at constructing a focus rail system, and if someone more experienced wants to jump in with suggestions/corrections I'm all ears (er, eyes).