Jennifer Deegan
Published © GPL3+

Driving a Focus Stacking Rail Using an Arduino

Driving my focus stacking rail using an Arduino to achieve micron steps.

IntermediateFull instructions provided2 hours8,911
Driving a Focus Stacking Rail Using an Arduino

Things used in this project

Story

Read more

Code

Fast Arduino code

C/C++
This code enables the Arduino to drive the focus rail with 17 micron increments and rapid movement of the rail.
// Number of pictures to take
#define shots 100
// How far to advance between shots
// 128 steps == 1 mark on the fine control == 1 micron
#define MICRON 128L
#define distance (17 * MICRON)

//Declare pin functions on Arduino
#define stp 2
#define dir 3
#define fire 13

void setup() {
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(fire, OUTPUT);
  digitalWrite(stp, LOW);
  digitalWrite(dir, HIGH);
  Serial.begin(9600); //Open Serial connection for debugging
}

//Main loop
void loop() {
    long int i, j;
    char input;

    Serial.println("Press Enter to start");
    do {
        input = Serial.read();
    } while (input != '\n');
    
    for (i = 0; i < shots; i++) {
        trigger();
        for (j = 0; j < distance; j++) {
            step();
        }
    }
}

// Power on the IR trigger circuit to fire the camera
void trigger()
{
    delay(1000); // wait for vibration to settle
    digitalWrite(fire, HIGH); // take the picture
    delay(1000); // wait for that to happen
    digitalWrite(fire, LOW); // disable the IR circuit again
}

// Advance the motor by one step
void step()
{
    digitalWrite(stp, HIGH); //Trigger one step forward
    delayMicroseconds(25);
    digitalWrite(stp, LOW); //Pull step pin low so it can be triggered again
    delayMicroseconds(25);  
}

Slow Arduino code

C/C++
This code enables the Arduino to drive the focus rail with small micron steps, and slow movement to allow settling between movements.
// Number of pictures to take 
#define shots 100 
// How far to advance between shots 
// 128 steps == 1 mark on the fine control == 1 micron 
#define MICRON 128L 
#define distance (17 * MICRON) 
//Declare pin functions on Arduino 
#define stp 2 
#define dir 3 
#define fire 13 
void setup() { 
pinMode(stp, OUTPUT); 
pinMode(dir, OUTPUT); 
pinMode(fire, OUTPUT); 
digitalWrite(stp, LOW); 
digitalWrite(dir, HIGH); 
Serial.begin(9600); //Open Serial connection for debugging 
} 
//Main loop 
void loop() { 
  long int i, j; 
  char input; 
  Serial.println("Press Enter to start"); 
  do { 
      input = Serial.read(); 
  } while (input != '\n'); 
  for (i = 0; i < shots; i++) { 
      trigger(); 
      for (j = 0; j < distance; j++) { 
          step(); 
      } 
  } 
} 
// Power on the IR trigger circuit to fire the camera 
void trigger() 
{ 
  delay(1000); // wait for vibration to settle 
  digitalWrite(fire, HIGH); // take the picture 
  delay(2000); // wait for that to happen 
  digitalWrite(fire, LOW); // disable the IR circuit again 
} 
// Advance the motor by one step 
void step() 
{ 
  digitalWrite(stp, HIGH); //Trigger one step forward 
  delay(1); 
  digitalWrite(stp, LOW); //Pull step pin low so it can be triggered again 
  delay(1);   
} 

Github

https://github.com/BioMakers/23_Focus-stacking-system-for-gametophyte-ferns

Credits

Jennifer Deegan

Jennifer Deegan

16 projects • 21 followers
I am working on deep focus photography of specimens from 0.5mm to 2.5mm tall. I would be interested in receiving commissions.

Comments