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
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 shoots20>
{
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!