Saturday, January 18, 2014

Arduino Time-Lapse Dolly Project Part 3

Arduino Time-Lapse Dolly Project Part 3

Stepper Into Motion

Now that I have tried the relay hooked up to the camera, got a button to fire it, and now its time to test the stepper motor.

First I should briefly explain what a stepper motor is and why I choose to use one.  They are called stepper motors because they move in small incremental steps which is generally measured in degrees.  So a 1.8 degree motor will take 400 little steps per revolution.  Because the motor can move in these small increments, it can (depending on the quality of the motor) make some very precise movements.  That is why I chose such motor for my application since I need the camera to move in consistent, accurate movements between shots.

I am using the Adafruit motor shield.  After I bought it, and started to figure out more about these crazy motor shields they can only handle so much power which means the motors can't draw to much power itself.  The Adafruit ones can handle 1.2A and I found it difficult to find a motor that was less than that. I eventually got one that draws .4A but I am not sure it will be powerful enough to move the camera... we will see!  Regardless it was only $15 so it if doesn't work I can at least use it to test everything out.

The Adafruit website is actually pretty handy and they have a lot of tutorials which is super awesome (see this link here) - though the one for this motor shield was slightly wrong at points, it was right enough that you can figure it out.  First thing first was to download the motor shield library. For instance, the tutorial said once the library is installed it should be accessible through the File/Examples/AFMotor, but that never happened for me.  It does show up under Sketch/Import Library/AFMotor and when you select it you get a "#include " added to the code - I assume always to the top but I haven't done it when there was code present.  Don't let this bother you, keep following the Adafruit tutorial and it has a lot of useful information and some example programming.  I used that example to help with my code below.  

Next step was wiring the sucker up.  For a test, and test only, I used a 9v battery to power the motor.  In real life the battery probably doesn't have enough juice to fully power the stepper motor I am using nor will it have the longevity that I need - 600+ cycles.  I bought a 9v battery hookup and wired it to the board at the Ext. PWR block.  Next to that, there is a jumper. Per the Adafruit tutorial if you want to use an external power supply to run the motor that is separate from the Arduino board, then you remove the jumper.  If you Arduino power supply (ie USB cable) is going to power both then leave the jumper in place.  As a side note, my experience is that even with the jumper removed, when I plugged the Arduino in to the USB to update the program it moved the motor.

Blur block next to the EXT PWR is where you can hook up the power.  The red circle on the right is where the jumper is located.  In this photo, I have already removed the jumper. 

I got a Primopal motor because it was cheap (~$15 shipped), and it was nice that Primopal had data sheets available for all their motors.  Without some sort of tech sheet you will have to figure out how to wire the motor to the board.  Now I am not expert on stepper motors but some are uni-polar and some are bi-polar.  Uni-polar motors have 5 wires and bi-polar have 4.  Either one will work, the shield allowed for both to be hooked up.  There are online resources that will show you how to wire the motor if it doesn't have a data sheet.

To test the motor I used Adafruit's tutorial code with some simple changes.  The motor I have is 1.8 degrees per step, and Adafruit is using a 7.5 degrees per step.  So I changed the steps from 100 to 400 (see red text below).  I also changed the rpm from 10 to 60 just because.

#include


AF_Stepper motor(48, 2);


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motor.setSpeed(10);  // 10 rpm

  motor.step(100, FORWARD, SINGLE);
  motor.release();
  delay(1000);
}

void loop() {
  motor.step(400, FORWARD, SINGLE);
  motor.step(400, BACKWARD, SINGLE);

  motor.step(400, FORWARD, DOUBLE);
  motor.step(400, BACKWARD, DOUBLE);

  motor.step(400, FORWARD, INTERLEAVE);
  motor.step(400, BACKWARD, INTERLEAVE);

  motor.step(400, FORWARD, MICROSTEP);
  motor.step(400, BACKWARD, MICROSTEP);
}



Now - to tie my camera shutter, and stepper motor together I wrote this code.  A button sets the whole thing in motion.  It will take 20 photos and advance the camera on the track 20 times (see the yellow highlighted section of the code in which this number is set). Keep in mind I have a MEGA board and not the regular Uno board, so I have more pins.  The button and relay are hooked to ports 52 and 53.


#include

AF_Stepper motor(200, 2);  //number of steps per revolution and indicates which port the motor is wired to.

const int buttonpin = 53;  //number of the pin the button is connected too
const int camerapin = 52; //number of the pin the relay to trigger the camera is connected too

int buttonstate = 0;

void setup() {

 pinMode(camerapin, OUTPUT);
 pinMode(buttonpin, INPUT);

 // Setting up the stepper motor
 Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");  //probably not a necessary line....

  motor.setSpeed(60);  // 60 rpm
 }

void loop(){
  buttonstate = digitalRead(buttonpin);

  if (buttonstate == HIGH){
 
    for (int i=0; i<20 span="">; i++) //this will be the stepper motor camera trigger loop, i dictates the number of times the camera shoots
  {
    digitalWrite(camerapin, HIGH);
    delay(3000);
    digitalWrite(camerapin, LOW);
    delay(500);
    motor.step(400, FORWARD, SINGLE);
    delay(1000);
  } //ends the FOR statement
 }  //end IF statement
} //ends LOOP



Next Steps - make a track and dolly for the camera!

Saturday, January 4, 2014

Arduino Time-Lapse Dolly Project Part 2

Arduino Time-Lapse Dolly Project Part 2

Buttons make it go!


For those who have not had much experience with the Arduino, basically what happens is once the board if plugged into a power source the program starts to run - so in this case the relay will start clicking away until you unplug it or upload another program.  So its kind of nice to update the program so that the relay, and therefore the camera will only fire once a button is pressed.

Now I add a button that will trigger the relay and therefore the camera.  The basic button wiring can be found on the Arduino site but I have one here as well:


So I ran a wire from the Arduino +5v pin to the bread board.  Hooked one side to the switch, on the other side of the switch is connected to a digital pin on the Arduino (Pin X), plus a 10K Ohm resistor connected in parallel to ground.

Here is my set up:

Button is the little back square on the white bread board.  The purple wires connect to the relay.  The red, yellow and black wires on the left side go to the camera.  

One thing to consider is that buttons aren't perfect and may "bounce" - meaning they don't make smooth contact and may chatter a bit.  I added a simple debounce code - very simple, maybe even laughably so - but I am a simple guy and I didn't understand the debounce code available online.

Here is my code:

const int buttonpin = 53;  //number of the pin the button is connected too
const int camerapin = 52; //number of the pin the relay to trigger the camera is connected too

int buttonstate = 0;

void setup() {

 pinMode(camerapin, OUTPUT);  //sets pin 52 to output mode
 pinMode(buttonpin, INPUT);  //sets pin 52 to input mode
}

void loop(){
  buttonstate = digitalRead(buttonpin);

  if (buttonstate == HIGH){
    digitalWrite(camerapin, HIGH);  // since I am not sure how much time the relay needs to be on for the camera to register it will keep the relay "on" for .1 seconds. 
    delay(100);
    digitalWrite(camerapin, LOW);
    delay(500); //to help prevent the button from triggering the relay multiple times by accident, this delay prevents the button from being pushed again for .5s
  }
  else{
    digitalWrite(camerapin, LOW);  //in hindsight, I probably don't need an "ELSE" statement
  }
}


Next: Stepper it up!