Arduino 连接步进电机
Arduino and stepper motors

将 NEMA 17 步进电机与 A4988 步进驱动器连接,由 Arduino Uno 控制。 其他元件包括 12V 电源、面包板、连接引脚和电容器。Connects a NEMA 17 stepper motor with an A4988 stepper drive, controlled by an Arduino Uno. Other components include a 12V Power supplier, a breadboard, connect pins and capacitors. NEMA17 stepper motor has 200 steps, or 1.8 degrees per step resolution, 2 phases.

Wiring Tutorial Reference
  1. Breadboard, the middle component area is for drivers and other components, connected half of the row
  2. Both sides are power supply columns, sections all connected, not in between
  3. Connection power supply (with ground and the capacitor) on the side of the breadboard
  4. Connect group and 5V of the driver to the Arduino to power the Arduino
  5. Connection four phases of the step motor to the driver
  6. Connect the control (enable, direction and step) to the Arduino control
Arduino programming (sample code)
 // Step the motor
// define the pins
#define EN_PIN    7 //enable
#define STEP_PIN  8 //step
#define DIR_PIN   9 //direction

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  //set pin modes
  pinMode(EN_PIN, OUTPUT); // set the EN_PIN as an output
  digitalWrite(EN_PIN, HIGH); // deactivate driver (LOW active)
  pinMode(DIR_PIN, OUTPUT); // set the DIR_PIN as an output
  digitalWrite(DIR_PIN, LOW); // set the direction pin to low
  pinMode(STEP_PIN, OUTPUT); // set the STEP_PIN as an output
  digitalWrite(STEP_PIN, LOW); // set the step pin to low

  digitalWrite(EN_PIN, LOW); // activate driver
}

// This function sets the number of steps, the direction and the speed
// steps: a full rotation requires 1600 steps
// direction: 1 = clockwise, 0 = anticlockwise
// speed: number of microseconds between each step, min 100
void rotateMotor(int steps, bool direction, int speed) {
  // Set the motor direction
  digitalWrite(DIR_PIN, direction);
 
  // Step the motor
  for (int i = 0; i < steps; i++) {
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(speed);
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(speed);
  }
}

void loop()
{
  //make steps
  //rotateMotor(1600,1,100); // full fast rotation clockwise
  //digitalWrite(LED_BUILTIN, HIGH);
  delay(1000); // one second delay
  //rotateMotor(1600,0,1000); // full slow rotation anticlockwise
  // digitalWrite(LED_BUILTIN, LOW);
  //delay(1000); // one second delay
  //rotateMotor(400,1,500); // quarter rotation clockwise
  //delay(1000); // one second delay
  //rotateMotor(400,0,500); // quarter rotation anticlockwise
  //delay(1000); // one second delay
}