Stepper motor problem

Hi guys!, I´m new in this forum and i´m making my first proyect with arduino. It consist simply to make run a stepper motor.
I have Arduino UNO card and a A4988 Stepper Motor Driver Carrier connecting to Arduino card pins 2, 3 and 4 with Step, Dir and -Enable of A4988, respetively.
Everything fine until I set up the ports as OUTPUT pins 2,3 and 4. Meantime the code of this configuration is executing, the motors starts to move in erratic way that make it out of initial position. After the code is executed, the motor runs well with the next instructions. Code that a use to set up the port is:

void ConfigurarPuertos(){
pinMode(EnablePinMotor, OUTPUT);
digitalWrite (EnablePinMotor, HIGH);

pinMode(StepPinMotor, OUTPUT);
pinMode(DirPinMotor, OUTPUT);
}

have any one any idea what could be happens?
Sorry my english, I´m from spanish country.

CPW.

/* 
 Stepper Motor Control - one revolution
 
 This program drives a unipolar or bipolar stepper motor. 
 The motor is attached to digital pins 8 - 11 of the Arduino.
 
 The motor should revolve one revolution in one direction, then
 one revolution in the other direction.  
 
  
 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe
 
 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
                                     // for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);            

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
   Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);
  
   // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500); 
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

The attached demonstration code works with an A4988 stepper driver. It doesn't use any library so that you can easily see how it works. You can change the speed by changing the value of millisbetweenSteps.

// testing a stepper motor with a Pololu A4988 driver board
// on an Uno the onboard led will flash with each step
// as posted on Arduino Forum at http://forum.arduino.cc/index.php?topic=208905.0

byte directionPin = 9;
byte stepPin = 8;
int numberOfSteps = 50;
byte ledPin = 13;
int pulseWidthMicros = 50;  // microseconds
int millisbetweenSteps = 50; // milliseconds

void setup() 
{ 

  Serial.begin(9600);
  Serial.println("Starting StepperTest");
  digitalWrite(ledPin, LOW);
  
  delay(2000);

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
 
  digitalWrite(directionPin, HIGH);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros);
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
  
  delay(3000);
  

  digitalWrite(directionPin, LOW);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros);
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
  
}

void loop() 
{ 

}

...R

Thank you guys, I'll try what you to show me, but a have a question: Is allways necesary to use pins from 8 to above?, Can I use pins from 2 to 7?

You can use any pins, but make them outputs in setup() - until they are outputs
the signals will be floating, explaining the erratic behaviour you describe. Pull-down
and pull-up resistors can be used to stop them floating completely if you want. 10k
or similar value will work I think.

That´s it MarkT !!! Now it works perfectly with pull'up resistor. Thank you a lot! :grin: