Build it step by step
0 of 7 doneBuild the rail
Mount the lead screw and the camera carriage.
Test the motor
Move one turn and measure 8 mm.
Wire the driver
Add the capacitor first.
Test the step sizes
Move 50 micrometres, check with a dial gauge if you have one.
Wire the shutter
Optocoupler to the camera's release cable.
Run a short stack
Ten shots of a small object.
Merge
Combine the photos in stacking software.
How do you build a macro focus stacking rail?
A stepper moves a camera a tiny distance between shots to build sharp macro images. You need Arduino Nano, Stepper motor 35 mm (NEMA 14), TMC2208 stepper driver, Lead screw T8 with nut (300 mm), Tactile push button 6 mm × 2, 16x2 LCD with I2C backpack (PCF8574), 12 V 2 A DC adapter, Optocoupler PC817, Resistor 1 kΩ and Electrolytic Capacitor 100 µF. It takes 7 steps and about 240 minutes.
- Level
- Intermediate
- Time
- About 240 min
- Parts
- 10
Wiring
| From | To | Why |
|---|---|---|
| TMC2208 VM and GND | 12 V adapter plus and minus | Motor supply |
| 100 µF capacitor | Across VM and GND at the driver | Absorbs spikes |
| TMC2208 VIO and GND | Nano 5V and GND | Logic supply |
| TMC2208 STEP | Nano D3 | Step pulses |
| TMC2208 DIR | Nano D4 | Direction |
| TMC2208 EN | Nano D8 | Enable (LOW = on) |
| TMC2208 MS1 and MS2 | GND | 1/8 microsteps in standalone mode |
| TMC2208 coil outputs | NEMA 14 motor | Check the coil pairs with a multimeter |
| Start button | Nano D2 and GND | Start and stop |
| Step size button | Nano D5 and GND | Cycle the step size |
| Nano D6 | 1 kΩ to PC817 LED anode, cathode to GND | Shutter signal |
| PC817 transistor side | Camera's remote release cable (shutter and ground) | Isolated shutter trigger (check the camera's pinout) |
| LCD SDA and SCL | Nano A4 and A5 | Status |
Code
Focus stacking takes many photos with the focus point moved a tiny distance each time, then merges the sharp parts in free software such as Helicon Focus, Zerene or the open-source Enfuse. A rail moves the whole camera forward in very small steps. A T8 lead screw moves 8 mm per turn; with the driver's standard 1/8 microstepping, one microstep is 5 micrometres, so a 25 micrometre step is 5 microsteps. Pick a step size from the table by the depth of field of your lens, usually smaller for higher magnification. The camera is triggered through an optocoupler, which keeps the Arduino electrically isolated from the camera; connect the PC817's output to the camera's remote release cable as its manual describes, and never put 5 V on a camera's own socket. Remote release pinouts differ by make and model, so check yours. Keep the camera on a firm rail, shield the lens from vibration, and never connect or disconnect the motor while the supply is on. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Focus stacking rail. Libraries: LiquidCrystal_I2C. Board: Arduino Nano. No stepper library needed.
// T8 lead screw: 8 mm per turn. 200 full steps x 8 microsteps = 1600 steps per turn, so one microstep = 5 micrometres.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int STEP = 3, DIR = 4, EN = 8, BTN_GO = 2, BTN_SZ = 5, SHUTTER = 6;
const int SIZES_UM[5] = {10, 25, 50, 100, 250}; // step size choices in micrometres
const int SHOTS = 40;
int sizeIdx = 1; bool running = false; int shot = 0; unsigned long lastBtn = 0;
void stepMotor(long steps, bool forward) {
digitalWrite(DIR, forward ? HIGH : LOW);
for (long i = 0; i < steps; i++) { digitalWrite(STEP, HIGH); delayMicroseconds(400); digitalWrite(STEP, LOW); delayMicroseconds(400); }
}
void showStatus() {
lcd.clear(); lcd.print("Step "); lcd.print(SIZES_UM[sizeIdx]); lcd.print(" um");
lcd.setCursor(0, 1); lcd.print(running ? "Shot " : "Ready "); if (running) { lcd.print(shot); lcd.print("/"); lcd.print(SHOTS); }
}
void setup() {
pinMode(STEP, OUTPUT); pinMode(DIR, OUTPUT); pinMode(EN, OUTPUT); pinMode(SHUTTER, OUTPUT);
pinMode(BTN_GO, INPUT_PULLUP); pinMode(BTN_SZ, INPUT_PULLUP);
digitalWrite(EN, LOW);
lcd.init(); lcd.backlight(); showStatus();
}
void loop() {
if (!digitalRead(BTN_SZ) && !running && millis() - lastBtn > 300) { sizeIdx = (sizeIdx + 1) % 5; lastBtn = millis(); showStatus(); }
if (!digitalRead(BTN_GO) && millis() - lastBtn > 400) { running = !running; shot = 0; lastBtn = millis(); showStatus(); }
if (running && shot < SHOTS) {
delay(1500); // let the vibration settle before the exposure
digitalWrite(SHUTTER, HIGH); delay(200); digitalWrite(SHUTTER, LOW);
delay(1500); // exposure and saving time
stepMotor(SIZES_UM[sizeIdx] / 5, true); // move the camera forward one step
shot++; showStatus();
if (shot >= SHOTS) { running = false; stepMotor(SIZES_UM[sizeIdx] / 5 * SHOTS, false); showStatus(); } // return to the start
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| One turn | Carriage moves 8 mm |
| Press start | Shutter fires, camera moves, repeat |
| 40 shots done | Camera returns to the start |
| Merge | Sharp image from front to back |
If it does not work
Shutter does not fire
Check the optocoupler side and the camera's pinout.
Steps are uneven
Remove backlash by always approaching from one direction.
Motor buzzes
Check the coil pairs and the supply.
Photos blur
Wait longer for vibration to settle.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the macro focus stacking rail?
Arduino Nano, Stepper motor 35 mm (NEMA 14), TMC2208 stepper driver, Lead screw T8 with nut (300 mm), Tactile push button 6 mm × 2, 16x2 LCD with I2C backpack (PCF8574), 12 V 2 A DC adapter, Optocoupler PC817, Resistor 1 kΩ and Electrolytic Capacitor 100 µF.
How long does the macro focus stacking rail take to build?
About 240 minutes for a intermediate maker, in 7 steps.
How do I know the macro focus stacking rail works?
One turn: Carriage moves 8 mm. Press start: Shutter fires, camera moves, repeat. 40 shots done: Camera returns to the start.
Shutter does not fire: what should I check?
Check the optocoupler side and the camera's pinout.
Steps are uneven: what should I check?
Remove backlash by always approaching from one direction.
Motor buzzes: what should I check?
Check the coil pairs and the supply.
Photos blur: what should I check?
Wait longer for vibration to settle.
