Note to the Picaxe forum members: I link here from a few other (non-electronics) forums so I explain some things you already know. Just skip that bits.
I love macro photography and as anyone who has ever tried it will know, extremely thin depth of field (here we are often talking about 0.1mm and below) can be a real problem as the object we are photographing is usually significantly larger. The higher the magnification you use the less of the photo ends up in sharp focus. This problem is typically solved by taking multiple photos with a slightly different focus point and later combining them to produce the final image where everything of importance is in focus. One such software is the excellent Zerene stacker that will do all the work for you. You still need to take a bunch of photos of the same object that differ only in the focus point - and by 'bunch' I mean sometimes more than 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
- memorise 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 head - the Picaxe 08m
- remote controlled to minimize the possibility of accidentally moving the camera or the subject
I actually managed to do this utilising 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 result:
First, watch the video!
(a bigger version).
IMPORTANT NOTE: I have made a mistake when drawing the diagram. The wire that connects the input pin 2 of the ULN2003A to the output pin 6 (veroboard strip 12 to 16) should be connected to ULN pin 1 (strip 11), not 2!
Ok, now forget my ugly scribble and download the excellent multi-layered Photoshop drawing that SteveD has made:
http://www.stevedargie.pwp.blueyonder.co.uk/Downloads/circuit.psd
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.
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
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 (free download from their site). 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.
Once everything is set, here is the code you need to send to your board:
The rail is completely controlled through an IR remote (any universal remote that can be programmed to use Sony TV codes). You simply 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 you use a Zerene stacker (or similar software) 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.
Here is just one example of a small 4mm speck of soil from a potted plant. This is how one frame looks like:
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:
I believe the difference is obvious
As an added bonus the exactly same setup can be used to automatically build a 360deg product rotation animations!
I love macro photography and as anyone who has ever tried it will know, extremely thin depth of field (here we are often talking about 0.1mm and below) can be a real problem as the object we are photographing is usually significantly larger. The higher the magnification you use the less of the photo ends up in sharp focus. This problem is typically solved by taking multiple photos with a slightly different focus point and later combining them to produce the final image where everything of importance is in focus. One such software is the excellent Zerene stacker that will do all the work for you. You still need to take a bunch of photos of the same object that differ only in the focus point - and by 'bunch' I mean sometimes more than 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
- memorise 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 head - the Picaxe 08m
- remote controlled to minimize the possibility of accidentally moving the camera or the subject
I actually managed to do this utilising 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 result:
First, watch the video!

(a bigger version).
IMPORTANT NOTE: I have made a mistake when drawing the diagram. The wire that connects the input pin 2 of the ULN2003A to the output pin 6 (veroboard strip 12 to 16) should be connected to ULN pin 1 (strip 11), not 2!
Ok, now forget my ugly scribble and download the excellent multi-layered Photoshop drawing that SteveD has made:
http://www.stevedargie.pwp.blueyonder.co.uk/Downloads/circuit.psd
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!
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.
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
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 (free download from their site). 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.
Once everything is set, here is the code you need to send to your board:
Code:
' 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
contflag=0
main:
infrain2
if infra<10 then
infra=infra+1
infra=infra//10
nsteps=infra*10
pause 500
infrain2
infra=infra+1
infra=infra//10
nsteps=nsteps+infra
write 0, nsteps
elseif infra=37 then
contflag=1
pause 500
infrain2
pause 500
endif
select infra
case 17 ' front focus
direction=2
gosub slide
case 16 ' back focus
direction=0
gosub slide
case 19 ' Vol- - mark focus start
nslide=0
irflag=0
pause 200
do
pause 200
direction=0
gosub slide
nslide=nslide+1
'pause 200
loop while irflag=0
pause 2000
for j=1 to nslide
direction=2
gosub slide
next j
gosub slide
direction=0
gosub slide
case 21 ' Power - start shooting run
gosub shoot
for j=1 to nslide
direction=0
gosub slide
gosub shoot
next j
endselect
'debug
pause 500
goto main
shoot:
pause 5000
high 4 ' shoot
pause 400
low 4
pause 3000 ' wait before the next slide (exposure time)
return
slide:
if direction<>olddir then ' remove sprocket slack
for i=1 to slack
gosub onestep
next i
endif
irflag=0
lup:
for i=1 to nsteps
gosub onestep
pause steppause
next i
if irflag=0 AND contflag=1 then goto lup
contflag=0
olddir=direction
return
onestep:
' s=direction*2
stepc=stepc+direction-1
let stepc=stepc // 4
lookup stepc, (%00000110, %00000100, %00000000, %00000010), s
let pins=s
if pin3=0 then
irflag=1
endif
'pause 1
return
Once all the photos have been taken you use a Zerene stacker (or similar software) 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
Here is just one example of a small 4mm speck of soil from a potted plant. This is how one frame looks like:

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:

I believe the difference is obvious
As an added bonus the exactly same setup can be used to automatically build a 360deg product rotation animations!
Last edited: