Example Code for Arduino-Stepper Motor Control

Explore how to control a stepper motor using Arduino with detailed hardware and software setup, wiring diagram, and example code. Ideal for DIY electronic projects, this guide walks you through the process of using a bipolar stepper motor with a TB6600 driver, offering clear instructions for seamless motor operation.

Hardware Preparation

Software Preparation

Wiring Diagram

DRI0043 Arduino TB6600 Stepper Motor

Up: OFF; Down: "ON"

Other Preparation Work

  1. In this tutorial, we'll use a bipolar stepper motor, 1.8 step angle, 1.7A
  2. Set current: 1.7A
  3. Set Micro Step: 32
  4. Connect a 9~42V DC power supply

Sample Code

/*!
 * @file  DRI0043.ino
 * @brief TB6600 arduino Stepper Motor Driver is an easy-to-use professional stepper motor driver, which could control a two-phase stepping motor.
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license  The MIT License (MIT)
 * @author  DFRobot
 * @version  V1.0
 * @date  2023-08-03
 */

int PUL=7; //define Pulse pin
int DIR=6; //define Direction pin
int ENA=5; //define Enable Pin
void setup() {
  pinMode (PUL, OUTPUT);
  pinMode (DIR, OUTPUT);
  pinMode (ENA, OUTPUT);

}

void loop() {
  for (int i=0; i<6400; i++)    //Forward 5000 steps
  {
    digitalWrite(DIR,LOW);
    digitalWrite(ENA,HIGH);
    digitalWrite(PUL,HIGH);
    delayMicroseconds(50);
    digitalWrite(PUL,LOW);
    delayMicroseconds(50);
  }
  for (int i=0; i<6400; i++)   //Backward 5000 steps
  {
    digitalWrite(DIR,HIGH);
    digitalWrite(ENA,HIGH);
    digitalWrite(PUL,HIGH);
    delayMicroseconds(50);
    digitalWrite(PUL,LOW);
    delayMicroseconds(50);
  }
}

Result

The stepper motor rotates 6400 steps (a cycle), and reverses 6400 steps (a cycle).

  • When “EN” is Low, the motor is in a free states (Off-line mode). In this mode, you can adjust the motor shaft position manually; when “EN” is High (Vacant), the motor will be in an automatic control mode.
  • "Direction" is the motor direction signal pin,
  • "PULSE" is the motor pulse signal pin. Once the driver get a pulse, the motor move a step.

Other Supplementary Information

User Guide V1.2

Was this article helpful?

TOP