Focus stacking (or z-stacking) is a technique for extending the depth of field in macro and microphotography. This is accomplished by taking many shots at sequential focus points. These "slices" are combined into a single image using software. Automation of this process can reduce vibrations and tedium, and this is relatively easy to do with a stepper motor, some reed relays, and a controller. Here, I provide schematics, software, and notes for building your own focus stacking controller.
This project is of moderate difficulty. If you are new to electronics, you will probably want to start with simpler projects and progress from there, accumulating tools, components, and experience as you go. If you have not worked with microcontrollers before, you may wish to have a look at the SparkFun Inventor's Kit for Arduino (P/N KIT-10173 from sparkfun.com). It took two years of other DIY projects before I felt I was ready to tackle this one. Don't rush.
This stacking controller provides feedback via a monochrome LCD screen and uses an IR remote instead of a wired control panel. It operates in two main modes. In position mode, you can freely move the focusing rack back and forth, take pictures, and set the "start" and "end" positions which define where a stack will begin and end. In the stacking mode, you set various parameters that alter the stacking behaviour, and then begin the stack. The device proceeds to move to the first slice position, stop, take the shot(s), advance to the next position, and so on until the stack is complete.
In my build, I made several design decisions to keep the unit compact and tidy:
You do not need to do everything exactly as I have done. The choice of a different project box, for example, will lead you to make different layout and wiring decisions. If you change anything that affects how the controller functions, you will probably need to modify the software.
For my build, I purchased almost all components from SparkFun.com or DigiKey.com. I can't promise that the suggested parts will remain available in the future! Prices are only estimates current to 2012, and do not include shipping.
|Component||Suggested Part||Qty||Approx. Extended Price (USD)||Notes|
|12 V bipolar stepper motor||Portescap 44M100D2B worked for me, but has low torque and may not be powerful enough for all purposes||1||$27.00 for 44M100D2B, otherwise highly variable||Look for a 7-12V bipolar stepper that draws less than 600mA per winding. Do not exceed the voltage and current limits of the quad half-H-bridge. Using the least powerful motor that you can get away with will help keep heat down. For more information, see the stepper motor notes.|
|Quad half-H-bridge||L293D||1||$3.50||The L293D supports steppers with up to 600mA per winding (1200mA in total) and includes internal output clamp diodes. Other chips are available that are more powerful, such as the L293B and L298, but these often require external output clamp diodes which complicates the circuit design.|
|8 bit shift register||74HC595||1||$0.60|
|Arduino microcontroller||Arduino UNO||1||$30.00||You may use different Arduino boards or even just the ATmega chips, but if you do, small tweaks to the circuit and code may be needed.|
|LCD screen||Nokia 5110 monochrome LCD||1||$10.00||Available from sparkfun.com and eBay.|
|1kΩ Resistor (1/6 W)||Flexible||1||$0.05||Voltage dropping resistor for the LCD's SCE pin|
|10kΩ Resistor (1/6 W)||Flexible||1||$0.05||Voltage dropping resistor for the LCD's data pins|
|IR receiver||SEN-08554 Sparkfun IR receiver breakout board||1||$10.00||Available from sparkfun.com.|
|Reed Relays||Hamlin HE3621A0510||2||$6.00||Nominal 5 V coil voltage, coil current of 35 mA or less|
|IR remote||Kenwood RC-P400||1||$5.00||Available on eBay. See notes on the IR Remote.|
|7-12 V power supply||Flexible, TOL-09442 (from sparkfun.com) looks good for a 12V supply||1||$6.00||Match the voltage of the supply to the stepper motor's needs and make sure it's in the acceptable range for the Arduino as well. Needs a 2.1mm center-positive barrel connector for the Arduino UNO. A 650mA supply is probably plenty, but add up the current draw of all your parts to be sure (the stepper is the biggest draw).|
|Project Box||SparkFun Arduino Project Enclosure||1||$12.00||You can save money by using whatever containers you have lying around, or build your own enclosure.|
|2.5mm Stereo Headphone (TRS) Female Jack||CUI Inc SR-2501||1||$2.63||Used for connecting to camera shutter release cable. Panel mount or Free Hanging is easy to work with.|
|4-pin Female Mini-DIN connector||CUI Inc MD-40J||1||$2.00||Used for connecting to stepper motor. Other types of connectors can be used.|
|4-pin Male Mini-DIN connector||CUI Inc MD-40||1||$2.00||Used for connecting to stepper motor. Other types of connectors can be used. These can be cannibalized from standard S-video cables.|
|Shutter Release Cable||1||Some cameras use 2.5mm stereo headphone cables. Others have proprietary cables. See notes on the camera connection.|
|Breadboard||Stripboard||Enough to accommodate your circuit layout. If using my layout, enough to cut out two 17 hole x 13 hole pieces with longitudinal strips.||$5.00||If using stripboard, go with 0.1" pitch and be sure it actually has strips and not just pads!|
|Female headers, straight||PRT-00115 (from sparkfun.com)||A 1x40 pin row will do, or buy many specifically sized headers||$1.50||0.1" pitch, square pin. You can cut these to size with wire cutters, although you lose a pin in the process.|
|Break away male headers, straight||PRT 00116 (from sparkfun.com)||One row of 40 will do||$1.50||0.1" pitch, square pin.|
|Component||Suggested Part||Qty||Approx. Extended Price (USD)||Notes|
|9 position top actuated SPST DIP switch||CTS Electrocomponents 209-9MS||1||$1.30||May replace with multiple DIP switches or 10 position switch if 9 position switch cannot be sourced. See notes on DIP Switch Camera Configuration.|
|Heat sink for the Quad half-H-bridge||Aavid Thermalloy 501200B00000G||1||$1.00||Optional but recommended. Many products exist for attaching heat sinks to chips. I used thermal epoxy, but that has the drawback of being fairly permanent.|
|6 conductor ribbon cable||CAB-10648 (from sparkfun.com)||Length depends on need||$0.75||Useful for a tidy connection between the LCD and Arduino.|
|3-pin Polarized Connector (Male Header)||PRT-08232 (from sparkfun.com)||1||$0.45||Useful for a reversible connection to the 2.5mm stereo microphone jack.|
|3-pin Polarized Connector (Female Housing)||PRT-08096 (from sparkfun.com)||1||$0.45||Needs to mate with the above. Useful for a reversible connection to the 2.5mm stereo microphone jack.|
|4-pin Polarized Connector (Male Header)||PRT-08231 (from sparkfun.com)||1||$0.45||Useful for a reversible connection to the 4-pin mini-DIN jack.|
|4-pin Polarized Connector (Female Housing)||PRT-08097 (from sparkfun.com)||1||$0.45||Needs to mate with the above. Useful for a reversible connection to the 4-pin mini-DIN jack.|
|Crimp pins for the above polarized connector housings||PRT-08100 (from sparkfun.com)||1||$1.95||Useful for reversible connections between the circuitry and panel-mount jacks.|
|22 to 25 AWG hook-up wire, stranded and solid, different colors (at least red and black)||Length depends on need||I assume you already have wire, so have not included a price.|
|Heat-shrink tubing, various sizes||VERSAFIT-KIT-6-0 or SparkFun PRT-09353||Depends on need||$8.00 - $10.00||Optional, but good for insulating connections.|
When a button is pressed on the remote control, the Sparkfun IR receiver breakout board sends a digital signal through the Out pin to interrupt 0 (digital pin 2) on the Arduino UNO. These signals are interpreted and allow the user to interact with the stacking controller. A user interface is provided on the Nokia 5110 LCD screen, which is attached to the Arduino's digital pins 8 through 13 and receives power from the 5V and GND power pins. The Arduino controls the L293D (stepper motor controller) and Hamlin HE3621A0510 reed relays (for triggering the camera shutter) via an 8-bit shift register attached to digital pins 3 through 5.
The shift register's first two bits enable or disable the two pairs of half-bridges on the L293D. When the half-bridge pairs are disabled, the associated stepper motor coil is effectively disconnected from the circuit. The next four bits of the shift register set the polarity of power supplied to the stepper's coils. The stepper can be moved in either direction using full-step or half-step resolution. Full-step resolution provides more torque, but movements are coarser. While the L293D operates off of the 5V regulated supply from the Arduino (to the VSS pin), the Arduino's Vin supply is connected to the L293's VS pin for use by the stepper motor. Pins 4, 5, 12, and 13 of the L293D are connected together and act as a heat sink in addition to ground pins.
The last two bits of the shift register control the half-press and full-press reed relays, respectively. The half-press relay (Rel1) completes a circuit over the shutter release cable that triggers the half-press mode of the shutter release, which usually functions in auto focus and auto exposure and keeps the camera in a ready-to-shoot state. The full-press relay (Rel2) triggers the camera shutter.
To support different cameras, a 9 position DIP switch allows each reed relay to complete a circuit over any two of the shutter release cable's three conductors.
Prior to selecting an enclosure, it is possible to build this entire project using jumper wires and a solderless breadboard. I don't recommend this for a permanent installation, but it is a good way to test the design and make sure that all of the components are working correctly.
The SparkFun Arduino Project Enclosure showing the arrangement and fit of all components.
The SparkFun Arduino Project Enclosure is just about the smallest enclosure that this project will fit into, unless you use custom PCBs and surface mount components. There are many options here, including store-bought project boxes, plastic food containers, scavenged enclosures from deceased electronics, or even completely DIY approaches. If you use an opaque box, be sure to cut windows for the LCD screen and IR receiver. The size of the window needed for the Nokia 5110 LCD screen is 1.32" x 0.85".
The inside of the lid of the SparkFun Arduino Project Enclosure showing the LCD window, cabling for the stepper motor and camera connectors, and the male headers for the IR receiver.
The breadboard layout is entirely up to you. My choice to use the SparkFun Arduino Project Enclosure dictated a maximum size that required me to split the board into two halves, as shown below, using stackable headers to connect the two boards. This would not be necessary for more spacious enclosures.
Illustration showing the layout of the stepper motor and camera controller boards. These layouts are based around stripboard, and the gaps show where the copper traces must be cut or ground away. Note that this is for single-sided stripboard, with through-hole components on one side, soldered to copper traces on the other side. Test all trace breaks to ensure no conductivity across them.
A photo of the stepper motor and camera controller boards. Although difficult to see in these photos, the "top" board (the camera controller board) uses male headers that stick out below the board, which insert into the female headers on the bottom board. Note the "power supply splitter cable" from the stepper controller board. This cable provides 5V and GND to the LCD screen and IR receiver.
To save space in your enclosure, you can desolder the male headers that come installed onto the LCD breakout board, and use a ribbon cable terminating in male headers to connect the LCD to the Arduino. The LCD screen requires a logic voltage of 3.3V, but the Arduino UNO's pins are at 5V. Based on SparkFun's recommendations, I have used 10k voltage dropping resistors for all logic pins except for SCE, which uses a 1k resistor. These resistors are spliced into the ribbon cable.
The backlight (connected to the LED pin) draws 80mA, but I have had good luck powering it directly from the digital out pin of the Arduino (40mA max). Using a transistor to supply the full current to the backlight (don't forget a current limiting resistor) will likely get you a brighter backlight. The backlight is optional, and leaving it unconnected will not affect the functionality of the project.
The power supply cable for the LCD has a small board spliced into it which provides a female header for attaching the IR receiver breakout board, as well as a cable (in white above) that connects the IR receiver's output to the Arduino.
You can solder wires to the pads of the IR receiver, but in my build I soldered in a 3-pin male header so that the pins stick out behind the receiver. My IR receiver plugs into a small power supply splitter board that you can see in the photos above. The VCC, GND, and OUT pads on the IR receiver should be connected to the Arduino's 5V pin, GND pin, and digital pin 2 respectively.
Be sure that the IR receiver can "see" (don't put it inside an opaque project box). It either needs to be mounted on the outside of your project box (this is what I did) or given an IR-transparent window through which to see.
If you use a remote other than the Kenwood RC-P400, make sure it has at least 20 buttons. To minimize fuss, avoid universal remotes. A remote that uses a single protocol to control a single device is much easier to decode. Remotes for stereo systems, portable CD players, etc. are good bets. You will want buttons for digits 0 to 9, enter, left, right, up, down, start/stop, mode, shutter half-press, shutter full-press, and backlight toggle.
This graph is the signal from a Kenwood RC-P400 remote on pressing and holding the volume up button, measured using an Arduino UNO and SparkFun IR breakout board. Note that the "low" or "0" states are always the same duration in the button signal (highlighted in green), while the "high" or "1" states have two different durations. The short durations code for a 0, and the long durations code for a 1. The remote outputs a unique 32 bit integer for each button (other remotes may use a different number of bits). After the button signal is a sequence of much longer pulses that repeat as long as the button is held down.
IR remotes work by rapidly flashing an IR LED on and off. The timing of these flashes encodes a numerical value, with a different value for each button. I have only tested a small number of remotes, so other remotes may require tweaks to the code to support them. If you get other remotes to work with this project, please post your findings in the comments.
Decoding your own remote is often a matter of measuring the button signals through the IR receiver using the Arduino itself, or an oscilloscope, and trying to figure out the pattern. I have written an IR Remote Testing sketch that may help you to do this. To use it, attach the IR receiver breakout board's VCC and GND pads to the Arduino's 5V and GND pins respectively. Connect the IR receiver's OUT pad to the Arduino's digital pin 2 and digital pin 3. Then, upload the IR Remote Testing sketch. Run the sketch while your Arduino is connected to your PC. Open up the serial monitor with a baud rate set to 115200 and follow the instructions in the serial monitor. Good luck!
You will want a 7-12V bipolar stepper motor with four or six leads (for six lead steppers, simply insulate the common leads and leave them unused). I recommend also getting one with 3.6 or 1.8 degrees/step, to allow for very fine movements (useful when doing high magnification work). I've found eBay to be a good source of steppers, but always consult the data sheets before you buy. Pay close attention to torque, degrees per step, and resistance per winding (determines current). It should not exceed the voltage and current limits of the quad half-H-bridge. To avoid heat issues, use the least powerful motor that still has the torque and speed that you want. The stepper motor I used is the Portescap 44M100D2B. This is a 3.6 degree/step, low-power motor that may not have enough torque for especially stiff fine focus knobs. But if your fine focus knob turns easily, it (or a similarly-powered motor) will do just fine. This part is pretty flexible, so it can't hurt to look at other brands and models.
Current draw is an important consideration. The more powerful the stepper motor, the more your L293D chip will heat up. If a powerful stepper is used, putting the L293D inside a closed container with no air flow could lead to overheated and blown components. The Portescap 44M100D2B draws 170mA per winding at 12V, and the L293D chip gets warm, but not hot. A more powerful stepper motor is going to warrant a different case, possibly with air flow or external heat sinks. Connecting the heat sink pins of the L293D to the outside of a heat-conductive case would be ideal. You can make an impromptu heat sink by placing thick flows of solder along the PCB traces connected to the L293D's grounding pins, and even solder a thick copper wire from the grounding pins to a heat sink on the outside of the case. You can also attach a heat sink to the top of the chip, but this will be less effective than drawing heat away from the heat sink / ground pins.
To figure out how much current your stepper will use per winding, just use Ohm's law: current (in amps) = voltage (in volts) divided by winding resistance (in ohms). For the Portescap 44M100D2B, that is 12V divided by 70 Ohms, giving a current draw of 171 mA per winding and 342 mA peak (when both windings are on).
One way to put a weak motor to good use is by gearing it down. You can do this with gears, or with timing pulleys and belts. Put a smaller gear or pulley (fewer teeth) on the motor, and a larger gear or pulley on whatever you want to turn. This will increase the motor's effective torque and decrease its step size. If the gear on the stepper motor has half the number of teeth as the gear on the focus knob, this will approximately double the torque and exactly halve the step size. You can even get stepper motors with built-in gear trains.
I recommend wiring your stepper motor to a 4-pin male Mini-DIN cable. You can find these cables being sold in stores as S-video cables. Use an ohmmeter across the stepper motor's wires to determine which wires are paired. The paired wires should register a winding resistance close to the value mentioned in the stepper motor data sheet. Figure out the wire pairs and arbitrarily call one pair "A" and the other pair "B". Next, figure out the polarity of each wire. This might require trial and error until the motor moves properly, so use temporary wiring until you figure it out for sure. The wires may be coloured to make this easy. Regardless of how you figure this out, label one pair of like-polarity wires "1" and the other "2". You should now have four leads labelled: A1, A2, B1, and B2. Wire these to the Mini-DIN cable such that, once connected to the female jack, they will complete a circuit with the matching outputs from the L293D.
Attaching the stepper motor to the knob of your focussing block, microscope, or linear stage can be done in many ways. You can use sprockets and timing belts, or direct couplings. In my rig, I have used a flexible coupling which is attached to a plastic thing that friction-fits to the fine focus knob on my focussing block.
Whatever stepper motor you use, feel free to post your findings in the comments!
To connect to your camera, you will need a cable with a 2.5mm stereo TRS ("headphone") plug on one end, and on the other end, whatever type of plug/jack that attaches to your camera's shutter release port. Many cameras use standard TRS jacks, allowing you to use audio cables from electronics retailers. Other cameras use proprietary connectors, requiring that you assemble your own cable. In the latter case, you can get inexpensive third party camera remotes with the necessary cables from eBay, snip the cables off, and splice them to 2.5mm stereo headphone cables. If you do this, snip the cable from the remote a few inches away so that you can splice a 2.5mm stereo female jack to the remote. This will allow you to use the remote as originally intended by plugging your spliced-up cable into it.
A DIP switch made my life easy because I have different devices which use the same shutter release cables, and while I've included it in all of the schematics, you can omit the DIP switch if you wish. To do this, directly connect pin 4 from both relays to the "sleeve" of the 2.5mm headphone jack, pin 1 of Rel1 to the "ring", and pin 1 of Rel2 to the "tip". You can further "configure" the circuit for different cameras by splicing up the shutter release cable in different arrangements until it works.
If you do use the DIP switch, configuring your project for different cameras is just a matter of flipping switches. There are only 6 possible combinations that make sense. Use the shutter half-press and full-press buttons on your remote when in the "Position" mode to test each of the following combinations until you find the one that works for your camera. Look in the brands/models column to see which configurations are the most common for various camera brands and models. If your brand/model is not listed here, feel free to post your findings in the comments so that I can add to these notes over time.
|Configuration||P. 1||P. 2||P. 3||P. 4||P. 5||P. 6||P. 7||P. 8||P. 9||Brands/Models|
Download the software below:
Main Stacking Software Rev. Feb 23 2014
IR Remote Testing Software Rev. Dec 11 2012
Using the Kenwood RC-P400 remote, the buttons are:
Digits 0-9: digits 0-9
Fast Forward: Right
P. Mode: Mode
AI Auto: toggle half-press shutter mode
AI Timer: take a picture (hold to do bulb exposures)
Presence: backlight toggle
Volume up: Up
Volume down: Down
The mode select menu is accessed by pressing the "mode" button on the remote. Then, press the digit which corresponds to the desired mode.
In position mode, press and hold the up and down buttons to move the stepper back and forth. Press start to set the start position, and press stop to set the end position. You can then move back and forth between the start and end positions by pressing the left and right buttons, respectively.
If you've properly configured the steps per revolution and micrometers per revolution in the config menu, you will get a readout of the distance (in micrometers) between your start and end positions.
Stack mode allows you to set various parameters for automated stacking. Use the up and down buttons to select the desired parameter, and change the values using the digits on the remote. If you make a mistake, use the left button to erase digits from the end. Slices determines the number of photograph positions between the start and end of the stack, while bracket determines how many photographs to take per slice. Int (interval) sets the delay between each exposure. Exp determines how long the shutter button is closed. If the camera is placed in bulb mode, this value will also determine the exposure time. Otherwise, it has no effect on the photograph - it merely needs to be long enough to trigger the shutter.
Once you are happy with the stacking parameters, press Start to begin the stack. During the stack, you may press Stop to end the stack, although there will be a delay (determined by the interval you set in the stacking configuration) before the device responds to the Stop button being pressed.
In config mode, you can set various parameters that will be saved into non-volatile memory and remain as set even after the device is powered down. Use the up and down buttons to select the desired parameters, and use the digits on the remote to modify them. Half-step (0 or 1) enables or disables half-step mode. If disabled, the motor is driven in full-step mode. Reverse (0 or 1) reverses the motor direction, which may be useful for ensuring that the up and down buttons on the remote move the camera in the expected direction. St/rev (steps per revolution) should be based on your motor's step size. 3.6 deg/step motors have 100 steps per revolution, 1.8 deg/step motors have 200 steps per revolution, etc. um/rev (micrometers per revolution) needs to be measured with calipers or a ruler - it should be set to the number of micrometers that your camera/platform/focus block moves for each full revolution of the stepper motor. Setting the previous two parameters properly is required for getting an accurate readout of the stack depth. Finally, max RPM sets the maximum speed of the stepper motor. Speeds no higher than 120rpm are recommended.
In the future, I plan to expand this project with features for doing timelapse photography with a moving camera platform, as well as the control of a barn-door equatorial mount for astrophotography. These future expansions should not require circuitry changes, but only new versions of the software. You can also alter the software as you see fit, giving you the power to make this device accomplish many things beyond its original intent.
I owe the community at photomacrography.net many thanks. The members of this web forum have been an amazing source of feedback and inspiration on numerous projects that I have undertaken. The photomacrography.net community has greatly increased my enthusiasm for macrophotography, and without this enthusiasm, this project likely would not have happened.
I must also thank the Arduino community for its dedication to open-source, DIY culture, and information sharing. Similar remarks are directed at the SparkFun community, and the DIY community as a whole. The internet is absolutely full of articles on microcontrollers, stepper motors, and the like. Without this information to help me, I would not have succeeded with this project.