Create An Automated Macro Rail For Image Stacking

Create An Automated Macro Rails For Image StackingDo you find that producing sharp macro photographs is hard due to the super limited depth of field? The way to get around this is to take multiple images with different focus points and stack them together. A tidies work, no doubt.

Macro photographer Domjan Svilkovic was kind enough to share a setup that needs nothing more than a camera, a micro controller, a bunch of cables, a computer, an old DVD an old floppy drive and some basic electronics. OK, that’s a lot of stuff, but considering that the alternative for an automated rails are in the high $100s range, it may very well be worth the extra effort.

Now, just before I hand the floor over to Domjan, beware, this is going to get geeky. And by geeky I mean that you need to have some previous electronics know-how (or better yet, ask a friend).

The Challenge

Anyone who has ever tried it will know, extremely thin depth of field can be a real problem as the object we are photographing is usually significantly larger. (here we are often talking about 0.1mm and below).

The higher the magnification you use the less of the photo ends up inside the depth of field area. This problem is typically solved by a process called focus stacking. In this process you take several shots of the same object, each shot has a slightly different focus point. Once you have all the images, you “stack” them together and combine them into a single image where everything of importance is in focus.

While the stacking process can be done manually, it is easier to use software, for my projects I use the excellent Zerene stacker.

Remember that we said you need to take several shots, each has a different part in focus? OK, by several we meant like 50. For that reason I went to design an automatic macro rail that can very precisely move a small object back and forward a few centimeters. The macro rail had to be able to:

  • move in user selectable steps from 0.01mm to about 1mm
  • continuously slide (fast) for the initial focusing
  • memorize the start and the end positions for the focus stack
  • compensate for the ‘slack’ in sprockets when changing the direction
  • control my Canon dSLR and automatically take the photos once everything has been set
  • remember some settings between the runs (step size)
  • I had to build it from easily available ‘junk’ as I wasn’t prepared to pay more than $20
  • simple and quick to build – no heavy machinery required
  • controlled by a smallest and cheapest micro-controller I had – the Picaxe 08m
  • remote controlled to minimize the possibility of accidentally moving the camera or the subject

[UT: Domjan is a Picaxe master, if you want some background on how to use a Picaxe, check his previous tut – Control Your Flash Settings With a TV Remote Control]

The Build

I actually managed to do this utilizing only the parts I already had in the house (so it didn’t cost me a penny while similar rails can be bought for close to $500!). All I needed was an old, broken DVD-ROM drive (for a very nice rail it contains), a unipolar stepper motor (from an ancient 5.25″ floppy drive – remember those?), the Picaxe 08m, and a few standard electronic components.

Here is the final result, followed by a diagram:

Construction image and diagram (click for bigger version):

Create An Automated Macro Rails For Image Stacking


Putting together the mechanical part of the project couldn’t have been simpler. Apart from dissembling the CD-ROM all I had to do was superglue a plastic bottle cap to the first sprocket (that would normally be rotated by a small motor which I have actually left in place and simply not use) and add a unipolar stepper motor. These two are connected by a rubber band, and that’s it! :) If you use a bipolar stepper you’ll have to use a larger Picaxe and also rewrite the code so I don’t recommend it.


The electronics are also quite simple and should provide little challenge to someone who knows how to solder. Buy a veroboard (stripboard), cut to size and just follow the circuit diagram. (the diagram can be obtained by clicking the image above)

Where there is an ‘X’ in the diagram cut the copper strip.

The IR receiver (that thing to the right/below the 08m) used is the TSOP1138. Wiring the stepper is a bit tricky as you have to guess the correct order of the wires. This is how you can do it:

  1. Use a multimeter to measure the resistance between different pairs of wires. Two numbers should keep appearing – one resistance will be twice smaller than the other. If you have a 5 wire motor then there will be one wire that will give this ‘half-resistance’ to any other wire. Mark this wire. If you have a 6 wire stepper you will find two such wires with the difference that not all the wire pairs will be internally connected (there will be two sets of wires that are connected). Mark both these wires.
  2. Glue a long paper ‘pointer’ to the stepper (so you can easily see in which direction it turns). Connect the previously marked wire(s) to the battery + side (4AA battery pack should work fine). Experiment connecting the 4 wires that are left to the battery – side, one at the time. The goal is to find the right sequence (1,2,3,4,1,2,3,4….) that will make the motor turn. Mark the wires 1,2,3 and 4.
  3. When connecting the stepper to the board connect in order (from left to right): 1,3,2,4 (yes, this is 1,3,2,4)

Hopefully the sequence will be correct.

After you have put all of it together, it’s time to program the micro-controller. How to do this is better explained in the Picaxe manual [pdf free download]. Basically all you need is a serial (or USB to serial) cable and a diagram from the manual describing which wire to connect where.

(You need to connect three wires to the bottom left of the board, marked -, RX and TX). You also need to connect a 5V power supply (4 AA NiMH batteries work great). If you want you can also connect a small switch on the ‘+’ wire from the battery.

Components list

  • 1X PICAXE 08m
  • 1X TSOP1138 (IR receiver)
  • 1X ULN2003A
  • 1X 4N25 (optocoupler)
  • 1X 8-pin DIL IC socket (for beneath the 08m)
  • 1X 6-pin DIL IC socket (for beneath the 4N25)
  • 1X 16-pin DIL IC socket (for the ULN2003A)
  • 1X 10k resistor
  • 1X 22k resistor
  • 4X 1k resistor
  • 2X 470 resistor
  • 1X >0.1uF electrolytic capacitor
  • 1X 4xAA battery holder (with wires)
  • 4X NiMH rechargable AA battery
  • 1X a small switch
  • 1X 5 or 6 wire unipolar stepper motor
  • 1X male 2.5mm stereo audio connector (if you are using a Canon dSLR)
  • 1X PICAXE programming cable (almost any serial cable will do)
  • 1X small stripboard (veroboard)
  • a bunch of wires


Once everything is set, here is the code you need to send to your board:

‘ Macro focus rail driver
‘ Unipolar stepper (12V 1.8deg) connected to ULN2003A:
‘ green, white, red, brown -> Out1-Out4
‘ 08m 2 -> ULN In2
‘ 08m 1 -> ULN In3
‘ Out4 -> optocoupler for shutter triggering (tip+, sleave-)
‘ IR detector codes:
‘ CH+ back focus
‘ CH- front focus
‘ NN steps in one slide (in 0.01mm)
‘ AV/TV the next slide will be continuous (any key to interrupt)
‘ Vol- mark focus (front) and start moving focus back; mark focus end by any key
‘ slide to the front of the stack
‘ Power start shooting
symbol slack = 5 ‘ how many micro steps to add when changing direction
symbol steppause = 1 ‘ larger number will make it go slower
‘ – useful if you have a stepper with very few steps per revolution
symbol stepc = b0
symbol s = b1
symbol i = b2
symbol direction = b3
symbol nsteps = b4
symbol nframes = b5
symbol nslide = b6
symbol j = b7
symbol irflag = b8
symbol olddir = b9
symbol contflag = b10
high 1
high 2
low 4
‘ (nsteps)
eeprom 0, (20)
read 0, nsteps
if infra<10 then
pause 500
write 0, nsteps
elseif infra=37 then
pause 500
pause 500
select infra
case 17 ‘ front focus
gosub slide
case 16 ‘ back focus
gosub slide
case 19 ‘ Vol- – mark focus start
pause 200
pause 200
gosub slide
‘pause 200
loop while irflag=0
pause 2000
for j=1 to nslide
gosub slide
next j
gosub slide
gosub slide
case 21 ‘ Power – start shooting run
gosub shoot
for j=1 to nslide
gosub slide
gosub shoot
next j
pause 500
goto main
pause 5000
high 4 ‘ shoot
pause 400
low 4
pause 3000 ‘ wait before the next slide (exposure time)
if direction<>olddir then ‘ remove sprocket slack
for i=1 to slack
gosub onestep
next i
for i=1 to nsteps
gosub onestep
pause steppause
next i
if irflag=0 AND contflag=1 then goto lup
‘ s=direction*2
let stepc=stepc // 4
lookup stepc, (%00000110, %00000100, %00000000, %00000010), s
let pins=s
if pin3=0 then
‘pause 1


The rail is completely controlled through an IR remote (any universal remote that can be programmed to use Sony TV codes).

  • Type in a two digit number to change the size of one slide (as it happens, almost exactly in 0.01mm units in my case).
  • CH+/- will make one forward/backward slide.
  • If you press the AV/TV key before the CH then the slide is continuous in that direction until any key is pressed.
  • Once you have moved your object to a starting position of a focus stack you press the Vol-. That starts the gradual slide which you are supposed to interrupt (any key press) when the end point of the focus stack is reached. The rail will then reset to the starting position remembering the number of steps needed.
  • The Power button starts the take-the-shot, move, take-the-shot… loop.


Once all the photos have been taken use a stacking software such as Zerene stacker to make the focus stack. These programs are usually designed to be used even with hand-held photos so giving them such a perfect series usually results in a perfect stack even on ‘full automatic’.

There you have it :) A useful project for macro photographers with an extremely easy to put together mechanics (old CD/DVD-ROMS or writers are easily obtainable) and a few cheap electronic parts. Once you have all the parts the whole thing can be put together over the weekend.

Even though it’s winter and there are very few worthy subjects for macro photography, I have tested the rail and it’s working wonderfully! Here is just one example of a small 4mm speck of soil from a potted plant. This is how one frame looks like:

Create An Automated Macro Rails For Image Stacking

Notice the extremely thin ‘slice’ that is in focus (about 0.12mm wide)! And here is a result from a stack of 35 photos taken 0.09mm apart:

Create An Automated Macro Rails For Image Stacking

I believe the difference is obvious :)


Create An Automated Macro Rails For Image Stacking

Create An Automated Macro Rails For Image Stacking

Create An Automated Macro Rails For Image Stacking

As an added bonus the exactly same setup can be used to automatically build a 360deg product rotation animations! :)

If you still have questions, the Picaxe forum has some great pointers. And if you want to see more of those, stay tuned to Domjan’s Picasa – it will build up as more images are made.

About the author: Domjan Svilkovic is
an amateur photographer for about 10 years. His primary passion is the macro photography and he often DIY stuff to save a few (or more than a few) bucks. See more of Domjan’s work here.

P.S – If you are into macro and have already killed a DVD player, check our Super Macro Your Cellphone Camera With A DVD Lens tutorial