Example Code for Arduino-Stepper Motor Drive

FireBeetle Covers-DC Motor&Stepper Driver can control two-channel two-phase four-wire stepper motors simultaneously. It adopts DC motor interface and control via IIC interface. The test explains the way demonstrates how to control a two-channel two-phase four-wire stepper motor by Arduino main board. Program Function: group A stepper motor (M1A(A+), M1B(A-), M2A(B+), M2B(B-)) reverse the rotation direction in every 2s.

Hardware Preparation

Software Preparation

Wiring Diagram

Connect the stepper motor to STEPPER1 and the power supply of motor should be 4~12V, shown as below.

stepper motor

Other Preparation Work

Connect the stepper motor to STEPPER1 and the power supply of motor should be 4~12V.

Sample Code

/*!
 * @file Stepper.ino
 * @brief DFRobot's Motor Drive
 * @n The example demonstrates two groups of stepping motors (Group A and Group B) work together
      at the same time, including operations such as precision control of rotation angles, rotation
      directions etc.

 * @copyright  [DFRobot](https://www.dfrobot.com), 2016
 * @copyright GNU Lesser General Public License

 * @author [LiXin]
 * @version  V1.0
 * @date  2017-7-31
 * @https://github.com/DFRobot/DFRobot_MotorStepper
 */
#include "Arduino.h"
#include "Wire.h"
#include "DFRobot_MotorStepper.h"

/*****************Keywords instruction*****************/
//SA--->Stepper_Group_A--->[M1A(A+),M1B(A-),M2A(B+),M2B(B-)]
//SB--->Stepper_Group_B--->[M3A(A+),M3B(A-),M4A(B+),M4B(B-)]
//CW: rotate in positive direction
//CCW: rotate in reverse
//A0: Chip Selection Address 1
//A1: Chip Selection Address 2
//A2: Chip Selection Address 3
//A3: Chip Selection Address 4
/*****************Function instruction*****************/
//void start(float angle, uint16_t speed, uint8_t dir)
  //*This function can be used to start the motor
  //*angle: Set the Angle       Min:0°(If the Angle is equal to 0°,The motor will not stop spinning)
  //*speed: Set the speed       Min:8
  //*dir: Set Orientation       CW or CCW
//void getDir()
  //*This function can get the current rotation direction of the motor

DFRobot_Stepper stepperA(SA,A0);

void reverse_SA()
{
  stepperA.start(0, 12, !stepperA.getDir());
}

void setup() {
  //initialize serial communication at 9600 bits per second:
  Serial.begin(115200);
  //join i2c bus (address optional for master)
  Wire.begin();
  //Initialize drive chips of stepping motors (Group A and Group B)
  stepperA.init();
  //Setting stepping motor Group A rotate in positive direction
  stepperA.start(0, 20, CW);
}

void loop()
{
  //Stepping motor (Group A) rotate in reverse in every 2 seconds
  reverse_SA();
  delay(2000);
}

Result

Group A stepper motor (M1A(A+), M1B(A-), M2A(B+), M2B(B-)) reverse the rotation direction in every 2s.

Additional Information

Functions:

Create a stepper motor object.

DFRobot_Stepper stepperA(SA) SA represents group A stepper motors (M1A(A+), M1B(A-), M2A(B+), M2B(B-))

Initiate the motor drive, read Product ID and Version ID, and print to the serial port.

init()

Set angle, speed, direction of the stepper motor rotation and start to rotate.

start(angle, speed, dir)
angle: set the rotation resolution.
The maximum resolution is  0.9°, which means the minimum rotate angle is 0.9°.
So that the motor won’t rotate if the resolution set to be 0.1° and only rotate to 0.9° if the resolution set to be 1°.
Min: 0°
Min: 0° (if the rotate angel set to be 0, the motor will keep rotating).
speed: set speed. The speed range is 0~1023. Set it to 0 and the rotate speed is 0r/min; set it to maximum 1023 and the rotate speed is 200r/min.
dir: set direction. dir=CW: rotate in C.W.(Clockwise); dir=CCW: rotate in anti-clockwise

Reverse the function of group A stepper motor

reverse_SA()

Was this article helpful?

TOP